cilphex
cilphex

Reputation: 6096

How do I use a custom bucket URL with aws-sdk

Say I have an S3 bucket:

media.coolstuff.com

I have enabled static website hosting for the domain, and this is what AWS gives me as the URL:

media.coolstuff.com.s3-website-us-east-1.amazonaws.com

I've made a CNAME from my subdomain to the AWS domain. In my bucket I've put an index.html and made it public, and visiting media.coolstuff.com successfully renders it, so I know the redirect works as it should.

I am trying to figure out how to use the aws-sdk gem to return URLs using the subdomain for objects in the bucket.

Here's the code:

creds = Aws::Credentials.new(access_key, secret_access_key)
s3 = Aws::S3::Resource.new(
  credentials: creds,
  endpoint: 'http://media.coolstuff.com',
  region: 'us-east-1'
)
bucket = s3.bucket('media.coolstuff.com')
obj = bucket.object('index.html')

obj.public_url
=> "http://media.coolstuff.com.media.coolstuff.com/index.html"

As you can see, the public url has the bucket duplicated. I get why this is: the bucket name is prefixed to the endpoint. But here, it shouldn't be. I don't know how to get around this.

Similarly, trying to get the etag fails because it can't figure out the right URL:

obj.etag
Seahorse::Client::NetworkingError: unable to connect to `media.coolstuff.com.media.coolstuff.com`; SocketError: getaddrinfo: nodename nor servname provided, or not known

Upvotes: 2

Views: 1325

Answers (1)

Frederick Cheung
Frederick Cheung

Reputation: 84132

According to the documentationyou can do this with

object.public_url(virtual_host: true)

Don't change the endpoint from the default - this is used for the actual api calls to S3

Upvotes: 2

Related Questions