Use S3 Java SDK shard File Upload API report ' Signaturedoesnotmatch ' exception location and solution

Source: Internet
Author: User

importjava.io.File;
importcom.amazonaws.AmazonClientException;
importcom.amazonaws.auth.profile.ProfileCredentialsProvider;
importcom.amazonaws.services.s3.transfer.TransferManager;
importcom.amazonaws.services.s3.transfer.Upload;
publicclassUploadObjectMultipartUploadUsingHighLevelAPI {
    publicstaticvoidmain(String[] args) throwsException {
        String existingBucketName = "*** Provide existing bucket name ***";
        String keyName            = "*** Provide object key ***";
        String filePath           = "*** Path to and name of the file to upload ***"; 
        
        TransferManager tm = newTransferManager(newProfileCredentialsProvider());       
        System.out.println("Hello");
        // TransferManager processes all transfers asynchronously,
        // so this call will return immediately.
        Upload upload = tm.upload(
                existingBucketName, keyName, newFile(filePath));
        System.out.println("Hello2");
        try{
            // Or you can block and wait for the upload to finish
            upload.waitForCompletion();
            System.out.println("Upload complete.");
        } catch(AmazonClientException amazonClientException) {
            System.out.println("Unable to upload file, upload was aborted.");
            amazonClientException.printStackTrace();
        }
    }
}

problem Description: use S3 Java SDK to upload the above code shard file, quote ' Signaturedoesnotmatch ' exception as follows

Unable to abort multipart upload, your may need to manually remove uploaded parts:null (Service:amazon S3; Status code:403; Error Code:signaturedoesnotmatch; Request Id:tx000000000000000003ddf-005853b76e-1a56f9b-default)
Com.amazonaws.services.s3.model.AmazonS3Exception:null (Service:amazon S3; Status code:403; Error Code:signaturedoesnotmatch; Request Id:tx000000000000000003ddf-005853b76e-1a56f9b-default), S3 Extended Request Id:1a56f9b-default-default

Positioning process:

Download S3 Java SDK Source code, and configure the log4j, print AWS Debug logs;

# for Jboss:avoid to setup log4j outside $JBOSS _home/server/default/deploy/log4j.xml! servers:comment out the log4j Listener in Web. XML to activate log4j. #log4j. Rootlogger=INFO, stdout, Logfilelog4j.rootlogger== DEBUGlog4j.appender.stdout=  Org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=  Org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d%p [%c]-<%m>%n

RGW configuration file/etc/ceph/ceph.conf the log level is adjusted to 20, restart the RGW process

Debug RGW = 20

Execute the multipart upload code to intercept the request logs from the client and server respectively. The following 2 images show that the Uploadid client and service side are inconsistent, causing the service side to fail the signature check.

So the question is, is RGW server, or Java S3 SDK implementation bug? By looking at the Java code and the AWS documentation description, I think there is a bug in the RGW signature implementation. RGW should be in

After getting the Uploadid value from the parameters in the client URL (already urlencode), decode, and then encode (~ special characters not encode) according to the steps described in the AWS documentation, gets the parameter string that conforms to the rule.

Http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html

Evasion scenarios:

Because the code period for modifying the RGW is longer, modify the modified Java code and compile a new jar package for the time being. The following is a modification method that does not follow AWS rules and only performs urlencode on parameter values.

Finally, you will submit a patch to the RGW Git repository.

https://github.com/BodihTao/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/java/com/amazonaws/auth/ Abstractawssigner.java

For the convenience of everyone, I uploaded the modified class file, put the 2 files with WinRAR in the Aws-java-sdk-1.11.69.jar package Com/amazonaws/auth directory to replace the original file can be a new jar package.

Https://github.com/BodihTao/aws-sdk-java/raw/master/aws-java-sdk/AbstractAWSSigner%241.class

Https://github.com/BodihTao/aws-sdk-java/raw/master/aws-java-sdk/AbstractAWSSigner.class

Use S3 Java SDK shard File Upload API report ' Signaturedoesnotmatch ' exception location and solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.