Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload a file to S3 using the AWS SDK

I'm trying to upload a file to an Amazon S3 bucket using the AWS SDK.

class LogToS3Bucket extends Thread{

    public void run() {
        super.run();
        Gdx.app.log("msg", "secondThreadRunning yeahhhhh!");
        File fileToPut = new File("../../../MyProject", "settings.txt");
        String accessKey = "<accessKey>";
        String secretKey = "<secretKey>";
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        AmazonS3 conn = new AmazonS3Client(credentials);

        conn.putObject("bucketname", "filename.txt", fileToPut);
    }

}

It doesn't work though, it throws an error on the call to .putObject()

Exception in thread "Thread-1" Status Code: 400, AWS Service: Amazon S3, AWS Error Code: BadDigest, AWS Error Message: The Content-MD5 you specified did not match what we received.
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:614)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:312)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:165)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:2951)
    at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1123)
    at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:979)
    at com.myproject.LogToS3Bucket.run(LogToS3Bucket.java:22)
like image 944
kelorek Avatar asked Feb 05 '13 05:02

kelorek


People also ask

How do I upload files to AWS S3?

In the Amazon S3 console, choose the bucket where you want to upload an object, choose Upload, and then choose Add Files. In the file selection dialog box, find the file that you want to upload, choose it, choose Open, and then choose Start Upload. You can watch the progress of the upload in the Transfer pane.

How do I access an S3 bucket from AWS SDK?

To access Amazon Simple Storage Service, create an AWS. S3 service object. Call the listBuckets method of the Amazon S3 service object to retrieve a list of your buckets. The data parameter of the callback function has a Buckets property containing an array of maps to represent the buckets.


1 Answers

I figured it out I think-- since my application is constantly editing that file and this snippet happens in a separate thread, the file is actually changed before it gets uploaded... If I make a temporary copy of the file and upload that to the bucket, it works.

like image 168
kelorek Avatar answered Sep 25 '22 13:09

kelorek