Failed to upload files to Amazon S3 in Java

I am starting to use S3, and I want to use it to store certain files on my computer. I wrote a small Java program that downloads all my files and folders, but I have this exception:

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 SDK credentials. I used the same credentials from the previous program for testing, and it worked. It is possible to create buckets, but I cannot store objects in it. In my code, I use PutObjectRequest to store objects.

Here's how I launch my Amazon S3 object:

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

Download 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 does not work on this line: this.amazonS3.putObject(por); .

Can someone explain what is wrong with my code.

+4
source share
1 answer

I finally found the answer to my question. The truth is in the initialization, I passed the wrong bucket name, and I use a different name to execute the queries. All this.

+5
source

Source: https://habr.com/ru/post/1394055/


All Articles