gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:04:46 25 4
gpt4 key购买 nike

我正在尝试将 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的构造函数中,关键参数实际上是上传文件的名称,而不是访问 key 。

来自 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 databeing uploaded.

来源:PutObjectRequest constructor detail

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

关于java - 上传到 AWS S3 存储桶时更改文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32646587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com