1
votes

I am trying to upload a csv file to S3 bucket. The code runs successfully but when I check the bucket, the file with Access Key as its name is uploaded. I have to rename the file manually to check its content.

Is there a way where I could rename the file programmatically only or may be the file name does not change automatically while uploading?

Please check the code below:

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();
    }
}

}

Ideally the file in bucket should be with the name compression_score_09-04-2015.csv but instead its AKAJI3EBMILBCWENUSA. Could somebody guide as to what should be done?

1
I took the liberty of deleting the access and secret keys from your post as they looked real. Wanted to let you know in case my edit doesn't get accepted or you want to change it on your own. If they weren't the real keys please ignore this. - Volkan Paksoy
Though they were not real but still I got your point. Thanks. - roger_that

1 Answers

4
votes

In PutObjectRequest constructor the key parameter is actually the name of the uploaded file, not the access key.

From SDK documentation:

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.

Source: PutObjectRequest constructor detail

You don't have to specify accessKey here because you already instantiate the AmazonS3Client object with the credentials which include the access and secret keys.