Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Presigned URL with format: s3.amazonaws.com/bucket.name

Tags:

amazon-s3

I'm generating presigned urls but its problematic for bucket names with periods and SSL because of the *.s3.amazonaws certificate, as described here: http://shlomoswidler.com/2009/08/amazon-s3-gotcha-using-virtual-host.html

Is there a way to generate urls with the following format?: http://s3.amazonaws.com/bucket.name/key

I didn't see an option in the API. I'm guessing I can do it "manually" by rearranging the url, but I'd rather avoid a hack if its not necessary.

Based on Michael's answer here is the code I'm now using:

public static URL bucketNameAfterS3Url(URL url) {

    int index = url.getHost().indexOf("s3");
    String host = url.getHost().substring(index);
    String bucket = url.getHost().substring(0, index - 1);
    URL toUse;
    try {
        toUse = new URL(url.getProtocol(), host, url.getPort(), '/' + bucket + url.getFile());
    } catch (MalformedURLException e) {
    }
    return toUse;
}
like image 964
Joel Avatar asked Apr 15 '15 13:04

Joel


People also ask

How do I get a Presigned URL on AWS?

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.

Can S3 bucket name be formatted as an IP address?

Bucket names must not be formatted as an IP address (for example, 192.168. 5.4). Bucket names must not start with the prefix xn-- . Bucket names must not end with the suffix -s3alias .

How do I create a URL for Amazon S3?

Nothing could be easier, just select all files you want to generate URLs for and click the “Web URL” button on the toolbar. You will see the list of URLs for each of the selected files. Click copy to clipboard button and you are ready to paste URLs to other programs such as an HTML files editor.

What URL format do Amazon S3 buckets end in?

An S3 bucket can be accessed through its URL. The URL format of a bucket is either of two options: http://s3.amazonaws.com/[bucket_name]/ http://[bucket_name].s3.amazonaws.com/


1 Answers

The S3 client allows you to set an option to configure this:

    // Configure the S3 client to generate urls with valid SSL certificates. With this setting we will get urls
    // like https://s3.amazonaws.com/bucket_name/... but if this is not set we will get urls like
    // https://bucket_name.s3.amazonaws.com/ which will cause the browser to complain about invalid SSL
    // certificates.
    s3Client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true));
like image 176
Yasser Avatar answered Sep 28 '22 10:09

Yasser