Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate S3 URL in "path-style" format

https://wincent.com/wiki/HTTPS_access_to_Amazon_S3_buckets http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html

Is there any way to use the Java SDK to generate a presigned S3 temporary URL in the path-style instead of the virtual-host-style?

Edit: To clarify:

path-style means https://s3.amazonaws.com/my.bucket/my_item?...

virtual-host style means https://my.bucket.s3.amazon.aws.com/my_item?...

like image 620
dxuhuang Avatar asked Oct 19 '17 22:10

dxuhuang


1 Answers

I found the solution.

You create your AmazonS3Client by calling withPathStyleAccessEnabled(true) on the AmazonS3ClientBuilder. For example in the following Scala code:

val amazonS3Client = AmazonS3ClientBuilder.standard()
  .withRegion(Regions.US_EAST_1)
  .withCredentials(awsCredentialsProvider)
  .withPathStyleAccessEnabled(true)
  .build()

And then you generate the URL the usual way.

val generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectName)
...
amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest).toString

The resulting URL will be path-style.

I wouldn't have bothered asking this question if I had seen Configure path-style in Java SDK Amazon S3 ...

like image 174
dxuhuang Avatar answered Oct 04 '22 22:10

dxuhuang