0

I am trying to upload files on Amazon s3 bucket in a very simple way, but it gives error if the file name consists of "(" or ")" , i dont want remove or replace these braces as they are required for my case

Code :

AWSCredentials awsCredentials = new BasicAWSCredentials("accessKey", "secretKey");
        AmazonS3 s3 = new AmazonS3Client(awsCredentials);
        String fileName = "TEST_FILE_(NAME).pdf";
        String directorykey = "path/to/amazon/directory";
        File file =  new File(/* path_to_file + */ fileName);
        
        try {
            s3.putObject(bucketName, directorykey+"/"+fileName , file);
        } catch (AmazonServiceException e) {
            LOG.fatal("Failed to store file to bucket: " , e);
        } catch (AmazonClientException e) {
            LOG.fatal("Failed to store file to bucket: " , e);
        }

ERROR :

Status Code: 403; Error Code: SignatureDoesNotMatch; Request ID: tx000000000000002e9edee-005a4ed3d2-2213a2-uky-campus-1; S3 Extended Request ID: 123456789)

Sidharth
  • 1,402
  • 2
  • 16
  • 37

1 Answers1

0

Name of the file TEST_FILE_(NAME).pdf won't be used here - this is path to a local file,

S3 isn't actually a filesystem, it's more like a big (hash table) associative array. The "Bucket" is the name of the hash table, and the "Key" is the key (from here)

String fileName = "TEST_FILE_(NAME).pdf";
String directorykey = "path/to/amazon/directory";

// make sure, that the file exists in a location - provide a path to a file
File file =  new File(/* path_to_file + */ fileName);
try {
  s3.putObject(bucketName, directorykey + "/" + fileName , file);
} catch (AmazonServiceException e) {
  LOG.fatal("Failed to store file to bucket: " , e);
} catch (AmazonClientException e) {
  LOG.fatal("Failed to store file to bucket: " , e);
}
mehowthe
  • 761
  • 6
  • 14
  • Yes, you are right, I am doing the same way you showed, I have edited my code here. now this code save the file if braces are removed from filename, but with braces it gives me given error. – Sidharth Feb 11 '21 at 07:04
  • It may be because of maven dependencies. Take a look on this topic; https://stackoverflow.com/questions/34625812/signaturedoesnotmatch-in-aws-java-sdk-for-s3 ```...I found that httpclient library did breaking changes with normalized URI, that affected Java AWS SDK S3. Please take a look for opened GitHub ticket org.apache.httpcomponents:httpclient:4.5.7 breaks fetching S3 objects for more details.``` I did some tests and key with braces works just fine. It's allowed to use them https://stackoverflow.com/questions/7116450/what-are-valid-s3-key-names-that-can-be-accessed-via-the-s3-rest-api – mehowthe Feb 11 '21 at 12:59