Qasim
Qasim

Reputation: 9648

Want to upload file to s3 bucket using presigned url

I am having trouble to upload image using presigned url . I am following amazon java code but it is not working.

My requirement is as follows I have created bucket on Amazon XYZBucket and my bucket is empty. I am acting as a server which gives presigned url to user and user will use this url to upload image.

Code to generate presigned url

AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
        URL url = null;

        try {
            java.util.Date expiration = new java.util.Date();
            long milliSeconds = expiration.getTime();
            milliSeconds += 1000 * 60 * 60 * 12; // Add 1 hour.
            expiration.setTime(milliSeconds);

            GeneratePresignedUrlRequest generatePresignedUrlRequest = 
                    new GeneratePresignedUrlRequest(bucketName, objectKey);
            generatePresignedUrlRequest.setMethod(HttpMethod.GET); 
            generatePresignedUrlRequest.setExpiration(expiration);

            url = s3client.generatePresignedUrl(generatePresignedUrlRequest); 

        } catch (AmazonServiceException exception) {

        } catch (AmazonClientException ace) {

        }
        return url.toString();

I have also use put method

AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); 

        java.util.Date expiration = new java.util.Date();
        long msec = expiration.getTime();
        msec += 1000 * 60 * 60; // Add 1 hour.
        expiration.setTime(msec);

        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey);
        generatePresignedUrlRequest.setMethod(HttpMethod.PUT); 
        generatePresignedUrlRequest.setExpiration(expiration);

        URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
        return url.toString()

My bucketName and objectkey is

XYZBucket and file1

When I hit the url in browser it gives me

SignatureDoesNotMatch 

error.

Can anyone help me to upload file using presigned url to s3 bucket?

Upvotes: 1

Views: 2768

Answers (2)

Harshil Kansagara
Harshil Kansagara

Reputation: 110

According to the AWS S3 documentation Signing and Authenticating REST request, S3 is now using SignatureVersion4 by default.

But the AWS-SDK is using SignatureVersion2 by default.

So we have to explicitly specify SignatureVersion4 in request header

Upvotes: 0

According to the AWS documentation, you should use the "PUT" method to create an "upload" URL. Then the user will make a "PUT" request on this URL to upload its files.

Hitting this URL within the browser will make a "GET" request, but the signature contains "PUT" so it throws a SignatureDoesNotMatch error.

Upvotes: 2

Related Questions