Skip to content

Commit cfa9485

Browse files
authored
Add Python S3 create_bucket.py example
Merge pull request #732 from awsdocs/py_create_bucket
2 parents 7ea7151 + 4b559e0 commit cfa9485

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
2+
# snippet-sourcedescription:[create_bucket.py demonstrates how to create an Amazon S3 bucket in any region.]
3+
# snippet-service:[s3]
4+
# snippet-keyword:[Amazon S3]
5+
# snippet-keyword:[Python]
6+
# snippet-keyword:[Code Sample]
7+
# snippet-sourcetype:[snippet]
8+
# snippet-sourcedate:[2019-06-24]
9+
# snippet-sourceauthor:[AWS]
10+
11+
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
12+
#
13+
# Licensed under the Apache License, Version 2.0 (the "License"). You
14+
# may not use this file except in compliance with the License. A copy of
15+
# the License is located at
16+
#
17+
# http://aws.amazon.com/apache2.0/
18+
#
19+
# or in the "license" file accompanying this file. This file is
20+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
21+
# ANY KIND, either express or implied. See the License for the specific
22+
# language governing permissions and limitations under the License.
23+
24+
25+
import logging
26+
import boto3
27+
from botocore.exceptions import ClientError
28+
29+
30+
def create_bucket(bucket_name, region=None):
31+
"""Create an S3 bucket in a specified region
32+
33+
If a region is not specified, the bucket is created in the S3 default
34+
region (us-east-1).
35+
36+
:param bucket_name: Bucket to create
37+
:param region: String region to create bucket in, e.g., 'us-west-2'
38+
:return: True if bucket created, else False
39+
"""
40+
41+
# Create bucket
42+
s3_client = boto3.client('s3')
43+
try:
44+
if region is None:
45+
s3_client.create_bucket(Bucket=bucket_name)
46+
else:
47+
location = {'LocationConstraint': region}
48+
s3_client.create_bucket(Bucket=bucket_name,
49+
CreateBucketConfiguration=location)
50+
except ClientError as e:
51+
logging.error(e)
52+
return False
53+
return True
54+
55+
56+
def main():
57+
"""Exercise create_bucket() method"""
58+
59+
# Assign these values before running the program
60+
bucket_name_in_default_region = 'BUCKET_NAME'
61+
bucket_name_in_specified_region = 'BUCKET_NAME'
62+
region = 'us-west-2'
63+
64+
# Set up logging
65+
logging.basicConfig(level=logging.DEBUG,
66+
format='%(levelname)s: %(asctime)s: %(message)s')
67+
68+
# Create a bucket in the S3 default region (us-east-1)
69+
if create_bucket(bucket_name_in_default_region):
70+
logging.info(f'Created bucket {bucket_name_in_default_region} '
71+
f'in the S3 default region (us-east-1)')
72+
73+
# Create a bucket in a specified region
74+
if create_bucket(bucket_name_in_specified_region, region):
75+
logging.info(f'Created bucket {bucket_name_in_specified_region} '
76+
f'in region {region}')
77+
78+
79+
if __name__ == '__main__':
80+
main()

0 commit comments

Comments
 (0)