上传到 AWS S3 存储桶时更改文件名

Changing file name while uploading to AWS S3 bucket

我正在尝试将 csv 文件上传到 S3 存储桶。代码运行成功,但是当我检查存储桶时,上传了名称为 Access Key 的文件。我必须手动重命名文件以检查其内容。

有没有一种方法可以让我仅以编程方式重命名文件,或者文件名可能不会在上传时自动更改?

请检查以下代码:

public class AwsFileUploader {

private static String bucketName = "mybucket";
private static String accessKey = "my-access-key";
private static String secretKey = "my-secret-key";
private static String uploadFileName = "CompressionScore/compression_score_09-04-2015.csv";

public static void main(String[] args) throws IOException {

    AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

    AmazonS3 s3client = new AmazonS3Client(credentials);

    try {
        System.out.println("Uploading a new object to S3 from a file\n");
        File file = new File(uploadFileName);
        System.out.println(file.getName());   //prints - compression_score_09-04-2015.csv
        s3client.putObject(new PutObjectRequest(bucketName, accessKey, file));
        System.out.println("File successfully uploaded");
    } catch (AmazonServiceException ase) {
        ase.printStackTrace();
    } catch (AmazonClientException ace) {
        ace.printStackTrace();
    }
}

}

理想情况下,存储桶中的文件名称应为 compression_score_09-04-2015.csv,而不是 AKAJI3EBMILBCWENUSA。 有人可以指导应该做什么吗?

在PutObjectRequest的构造函数中,关键参数实际上是上传文件的名称,而不是访问密钥。

来自 SDK 文档:

Parameters: bucketName - The name of an existing bucket to which the

new object will be uploaded.

key - The key under which to store the new object.

input - The stream of data to upload to Amazon S3. metadata - The object metadata.

At minimum this specifies the content length for the stream of data being uploaded.

来源:PutObjectRequest constructor detail

您不必在此处指定 accessKey,因为您已经使用包含访问密钥和秘密密钥的凭据实例化了 AmazonS3Client 对象。