Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access S3 Pre-Signed URL due to authorization [duplicate]

Using Java8 and aws-java-sdk 1.10.43 I'm trying to get a Pre-Signed URL to an S3 file. I do get back a link, but browsing to it lead to this error:

authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256

To emphasize, I wish to generate a URL that can be sent via email and opened in a browser, not to use Java code to read from that URL.

I'm using the bellow code, and I believe to find out that I need to somehow set setSSEAlgorithm to use "v4", however I've failed to make it work. What am I missing? What should I configure (note: I'm avoiding configuration file on purpose, I wish the code to set all attributes from environment variables)

Date expiration = <some date>;
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, targetPath);
generatePresignedUrlRequest.setMethod(HttpMethod.GET);
generatePresignedUrlRequest.setExpiration(expiration);

AmazonS3 s3client = new AmazonS3Client(s3Credentials);
URL s = s3client.generatePresignedUrl(generatePresignedUrlRequest);

The bucket is at eu-central-1

Thank you

like image 773
user2339344 Avatar asked Dec 27 '15 19:12

user2339344


People also ask

How do I get pre-signed URL S3?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that contains the object that you want a presigned URL for. In the Objects list, select the object that you want to create a presigned URL for.

What are S3 pre-signed URLs?

A presigned URL is a URL that you can provide to your users to grant temporary access to a specific S3 object. Using the URL, a user can either READ the object or WRITE an Object (or update an existing object). The URL contains specific parameters which are set by your application.

How do S3 pre-signed URLs work?

Pre-signed URLs are used to provide short-term access to a private object in your S3 bucket. They work by appending an AWS Access Key, expiration time, and Sigv4 signature as query parameters to the S3 object. There are two common use cases when you may want to use them: Simple, occasional sharing of private files.


Video Answer


1 Answers

Using some help I've found the answer, which was a combination of 2 missing pieces (one of which was referred to in the comments):

  1. Need to set this:

    System.setProperty(SDKGlobalConfiguration.ENABLE_S3_SIGV4_SYSTEM_PROPERTY, "true");
    
  2. Must set the "endPoint" (which was not required for upload or download):

    s3Client.setEndpoint(endpoint);
    

Optionally it might be useful to also add this:

s3Client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true));
like image 193
user2339344 Avatar answered Oct 07 '22 08:10

user2339344