Python Boto S3 for working with custom domains in Amazon S3

How to use Python Boto library with S3, where the generated URL will be my CNAME subdomain on Amazon S3 server.

The default format is BUCKETNAME.s3.amazonaws.com , but S3 supports its own domain alias using CNAME (so you can have custom.domain.com → CNAME → custom.domain.com.s3.amazonaws.com where " custom.domain.com "is a bucket. AWS Documentation

I see that the boto library has boto.s3.connection.SubdomainCallingFormat and the boto.s3.connection.VHostCallingFormat class ...

Does anyone know how I can configure boto.s3, where is the generated URL for my own custom domain instead of the standard one?

+4
source share
1 answer
  • Your CNAME records should already point to your S3 bucket.
  • Your S3 bucket should also be named custom.domain.com
  • Make sure you can access your files from custom.domain.com in your browser.

After that, the following snippet that I wrote will print the URL for all the files in the key:

import sys import boto.s3 from boto.s3.connection import VHostCallingFormat from boto.s3.connection import S3Connection def main(): access_key = "<AWS_ACCESS_KEY>" secret_key = "<AWS_SECRET_KEY>" bucket = "custom.domain.com" # assuming you have your files organized with keys key_prefix = "css" key_prefix = key_prefix + "/" conn = S3Connection(access_key, secret_key, calling_format=VHostCallingFormat()) bucket = conn.get_bucket(bucket) # get all the keys with the prefix 'css/' inside said bucket keys = bucket.get_all_keys(prefix=key_prefix) for k in keys: print k.generate_url(3600, query_auth=False, force_http=True) # output: # http://custom.domain.com/css/ie.css # http://custom.domain.com/css/print.css # http://custom.domain.com/css/screen.css # http://custom.domain.com/css/style.min.css if __name__ == '__main__': main() 
+2
source

Source: https://habr.com/ru/post/1302900/


All Articles