amazon-web-servicesamazon-s3aws-sdk

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?...


Solution

  • 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 ...