Reputation: 181
I'm new to using AWS's S3 and I have to upload a file using the putObject(bucket name, key , string that will become the file)
method (makes my life a lot easier). I was wondering if it's possible to change the extension because when I use this method the file uploaded always has a '.txt' extension, but I need it to be '.csv'.
The code is quite simple:
public void uploadStringFile(String bucket,String username, String fileString) {
s3Client.putObject(bucket, username, fileString);
}
awsS3Service.uploadStringFile("ati-teste",fileName, sb.toString());
Upvotes: 1
Views: 3929
Reputation: 9844
This is the signature for your helper uploadStringFile
method:
public void uploadStringFile(String bucket,String username, String fileString)
The parameter names are potentially misleading. We see that the code delegates to AmazonS3#putObject(String, String, String)
:
s3Client.putObject(bucket, username, fileString);
According to its JavaDocs, the parameters are:
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
Notice there is no username
. Instead, the second parameter is the destination key
. Therefore, you can simply pass whatever key you want in the second parameter, and that's where it will go in S3. If you already have a base file name like "test1.txt", then you can use basic String
operations to change it to "test1.csv" before calling the S3 method.
Note that you also might find it more convenient to use other variants of the S3 API, such as AmazonS3#putObject(String, String, File)
or AmazonS3#putObject(PutObjectRequest)
in a form like this:
s3Client.putObject(new PutObjectRequest()
.withBucketName(bucket)
.withKey(key)
.withFile(inputFileObject));
Upvotes: 2