简体   繁体   English

无法使用Java在Amazon S3中上载文件

[英]Unable to Upload files in Amazon S3 in Java

I am a beginner using S3 and I want to use it to store certain files of my computer. 我是初学者使用S3,我想用它来存储我的电脑的某些文件。 I wrote a small program in Java that uploads all my files and folders but I have this exception : 我用Java编写了一个小程序来上传我的所有文件和文件夹,但我有这个例外:

    Infos: Received error response: Status Code: 403, AWS Request ID: F75DC5496B071F7D, AWS Error Code: SignatureDoesNotMatch, AWS Error Message: The request signature we calculated does not match the signature you provided. Check your key and signing method., S3 Extended Request ID: XtRqjEuUmygswUxFUophRudYKbwi4OY4MK9QnYS4DMrH6JrHZXihUEC4mLZbPqz4
Exception in thread "main" Status Code: 403, AWS Request ID: F75DC5496B071F7D, AWS Error Code: SignatureDoesNotMatch, AWS Error Message: The request signature we calculated does not match the signature you provided. Check your key and signing method., S3 Extended Request ID: XtRqjEuUmygswUxFUophRudYKbwi4OY4MK9QnYS4DMrH6JrHZXihUEC4mLZbPqz4
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:538)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:283)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:168)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:2555)
    at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1044)
    at com.socialite.s3export.S3Export.parseDirectory(S3Export.java:89)
    at com.socialite.s3export.S3Export.parseDirectory(S3Export.java:73)
    at com.socialite.s3export.S3Export.parse(S3Export.java:48)
    at com.socialite.s3export.S3Export.main(S3Export.java:122)

I am sure I provided the correct credentials to the SDK. 我确信我为SDK提供了正确的凭据。 I used the same credentials from a previous program for testing purposes and it worked. 我使用了之前程序中的相同凭据进行测试,但它确实有效。 It's possible for me to create buckets but I am unable to store objects in it. 我可以创建存储桶,但我无法在其中存储对象。 In my code, I use the PutObjectRequest to store objects. 在我的代码中,我使用PutObjectRequest来存储对象。

Here's how I init my amazon S3 object : 这是我如何初始化我的亚马逊S3对象:

AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(
            S3Export.class                      .getResourceAsStream("AwsCredentials.properties")));

The uploading code : 上传代码:

private void parseDirectory(File fin, File fdefault, String out)
            throws FileNotFoundException {

        System.out.println("processing .." + fin.getName());

        File[] files = fin.listFiles();

        PrintWriter pw = new PrintWriter(fdefault);

        for (File f : files) {

            String key = UUID.randomUUID().toString();

            String name = "";

            PutObjectRequest por = null;

            if (f.isDirectory()) {

                name = "d_" + f.getName() + ".properties";

                File metadata = new File(out + name);

                parseDirectory(f, metadata, out);

                pw.println(key + "=" + name);

                //upload to s3
                por = new PutObjectRequest(this.bucketName, key, metadata);

            } else {

                name = f.getName();

                pw.println(key + "=" + name);

                por = new PutObjectRequest(this.bucketName, key, f);

            }


            this.amazonS3.putObject(por);
        }
        pw.close();
    }

My code fails in this line : this.amazonS3.putObject(por); 我的代码在这行中失败了: this.amazonS3.putObject(por); .

Can somebody explain what is wrong with my code. 有人可以解释我的代码有什么问题。

I finally find the anwer to my question. 我终于找到了问题的答案。 The truth is in the initialization, I passed a wrong bucket name and I am using a different name for making the requests. 事实是在初始化中,我传递了一个错误的存储桶名称,我使用不同的名称来发出请求。 That's all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM