5

我正在尝试使用 AWS 开发工具包将文件上传到 Amazon S3 存储桶。

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

}

但它不起作用,它会在调用 .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)
4

1 回答 1

5

我想通了——因为我的应用程序一直在编辑那个文件并且这个片段发生在一个单独的线程中,所以文件在上传之前实际上已经改变了......如果我制作文件的临时副本并将其上传到桶,它的工作原理。

于 2013-02-05T05:37:36.567 回答