You can control the location of a new bucket by specifying a value for the location
parameter in the create_bucket
method. For example, to create a bucket in the ap-northeast-1
region you would do this:
import boto.s3
from boto.s3.connection import Location
c = boto.s3.connect_to_region('ap-northeast-1')
bucket = c.create_bucket('mynewbucket', location=Location.APNortheast)
In this example, I am connecting to the S3 endpoint in the ap-northeast-1
region but that is not required. Even if you are connected to the universal S3 endpoint you can still create a bucket in another location using this technique.
To access the bucket after it has been created, you have a couple of options:
- You could connect to the S3 endpoint in the region you created the bucket and then use the
get_bucket
method to lookup your bucket and get aBucket
object for it. - You could connect to the universal S3 endpoint and use the
get_bucket
method to lookup your bucket. In order for this to work, you need to follow the more restricted bucket naming conventions described here. This allows your bucket to be accessed viavirtual hosting
style addressing, e.g. https://mybucket.s3.amazonaws.com/. This, in turn, allows DNS to resolve your request to the correct S3 endpoint. Note that the DNS records take time to propagate so if you try to address your bucket in this manner immediately after it has been create it might not work. Try again in a few minutes.