TinhDuong
TinhDuong

Reputation: 1

S3 pre-signed URL file upload not working when generatePresignedUrlRequest with CannedAccessControlList.Private

My example:

AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
    ClientConfiguration clientConfig = new ClientConfiguration();
    clientConfig.setProtocol(Protocol.HTTP);
    BasicAWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey); 
    AmazonS3 s3client = new AmazonS3Client(credentials, clientConfig);
    Region region = Region.getRegion(Regions.EU_WEST_1);
    s3client.setRegion(region);
    s3client.setEndpoint(serverCeph);
    Bucket bucket = s3client.createBucket(bucketName);
    File file = new File(fileNameTest);
    AccessControlList acl = new AccessControlList();
    acl.grantPermission(GroupGrantee.AllUsers, Permission.FullControl);
       s3client.putObject(new PutObjectRequest(bucketName, keyObject,   file).withAccessControlList(acl));
    s3client.setObjectAcl(bucket.getName(), keyObject,CannedAccessControlList.Private);


           // Generate URL
    System.out.println("Generating pre-signed URL.");
    java.util.Date expiration = new java.util.Date();
    long milliSeconds = expiration.getTime();
    milliSeconds += 1000 * 60 * 60; // Add 1 hour.
    expiration.setTime(milliSeconds);
    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, keyObject);
    generatePresignedUrlRequest.setMethod(HttpMethod.GET);
    generatePresignedUrlRequest.setExpiration(expiration);
    generatePresignedUrlRequest.setContentType("image/png");
    URL url = s3client.generatePresignedUrl(generatePresignedUrlRequest);
    System.out.println("Pre-Signed URL = " + url.toString());

If I using s3client.setObjectAcl(bucket.getName(), keyObject,CannedAccessControlList.PublicRead); then i can get URL and this work fine But If I used s3client.setObjectAcl(bucket.getName(), keyObject,CannedAccessControlList.Private); then I can get URL and The URL not working

I always recived error below when I passed the URl into browsers.

<Error>
<Code>SignatureDoesNotMatch</Code>
<RequestId>tx000000000000000068415-0058d62341-5229e3-default</RequestId>
<HostId>5229e3-default-default</HostId>
</Error>

And this is URL I generated:

http://server:port/bucketname/keyobject?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20170325T081827Z&X-Amz-SignedHeaders=content-type%3Bhost&X-Amz-Expires=3599&X-Amz-Credential=HGPX8TW9RGI5LEFLARTX%2F20170325%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=76cb890603b2d9a64fd9c5c670584c73f457c08fc0a31f67c7b2a6c92265a427

Somebody can have any idea or the way that I used to run URL correctly?

Upvotes: 0

Views: 2274

Answers (1)

Alex
Alex

Reputation: 2126

Just in case if somebody is looking for the solution.

If you see SignatureDoesNotMatch - first thing to check is the "Content-Type" header that you are sending when uploading a file. It must have the same value that you used for the URL generation.

Upvotes: 0

Related Questions