我刚开始使用 AWS 的 S3,我必须使用 putObject(bucket name, key , string that will be the file)
方法上传文件(让我的生活变得更轻松) 。我想知道是否可以更改扩展名,因为当我使用此方法时,上传的文件始终具有“.txt”扩展名,但我需要它是“.csv”。
代码非常简单:
public void uploadStringFile(String bucket,String username, String fileString) {
s3Client.putObject(bucket, username, fileString);
}
awsS3Service.uploadStringFile("ati-teste",fileName, sb.toString());
最佳答案
这是您的帮助器 uploadStringFile
方法的签名:
public void uploadStringFile(String bucket,String username, String fileString)
参数名称可能会产生误导。我们看到代码委托(delegate)给 AmazonS3#putObject(String, String, String)
:
s3Client.putObject(bucket, username, fileString);
根据其JavaDocs,参数为:
Parameters:
bucketName - The name of the bucket to place the new object in.
key - The key of the object to create.
content - The String to encode
请注意,没有用户名
。相反,第二个参数是目标key
。因此,您可以简单地在第二个参数中传递您想要的任何键,这就是它在 S3 中的位置。如果您已经有一个基本文件名,例如“test1.txt”,那么您可以使用基本 String
在调用 S3 方法之前,将其更改为“test1.csv”。
请注意,您可能还会发现使用 S3 API 的其他变体更方便,例如 AmazonS3#putObject(String, String, File)
或AmazonS3#putObject(PutObjectRequest)
以这样的形式:
s3Client.putObject(new PutObjectRequest()
.withBucketName(bucket)
.withKey(key)
.withFile(inputFileObject));
关于java - AWS S3 使用 putObject(String,String,String) 时更改文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45105357/