2019-10-20 18:51:09 +02:00
|
|
|
.. Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2018-07-11 07:39:36 +02:00
|
|
|
|
|
|
|
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0
|
|
|
|
International License (the "License"). You may not use this file except in compliance with the
|
|
|
|
License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/.
|
|
|
|
|
|
|
|
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
|
|
either express or implied. See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
|
2019-10-20 18:51:09 +02:00
|
|
|
#################
|
2021-09-22 18:34:33 +02:00
|
|
|
Amazon S3 buckets
|
2019-10-20 18:51:09 +02:00
|
|
|
#################
|
2018-07-11 07:39:36 +02:00
|
|
|
|
2019-10-20 18:51:09 +02:00
|
|
|
An Amazon S3 bucket is a storage location to hold files. S3 files are referred
|
|
|
|
to as objects.
|
2018-07-11 07:39:36 +02:00
|
|
|
|
2019-10-20 18:51:09 +02:00
|
|
|
This section describes how to use the AWS SDK for Python to perform common
|
|
|
|
operations on S3 buckets.
|
2018-07-11 07:39:36 +02:00
|
|
|
|
|
|
|
|
2021-09-22 18:34:33 +02:00
|
|
|
Create an Amazon S3 bucket
|
2019-10-20 18:51:09 +02:00
|
|
|
==========================
|
2018-07-11 07:39:36 +02:00
|
|
|
|
2019-10-20 18:51:09 +02:00
|
|
|
The name of an Amazon S3 bucket must be unique across all regions of the AWS
|
|
|
|
platform. The bucket can be located in a specific region to minimize latency
|
|
|
|
or to address regulatory requirements.
|
2018-07-11 07:39:36 +02:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2019-10-20 18:51:09 +02:00
|
|
|
import logging
|
2018-07-11 07:39:36 +02:00
|
|
|
import boto3
|
2019-10-20 18:51:09 +02:00
|
|
|
from botocore.exceptions import ClientError
|
2018-07-11 07:39:36 +02:00
|
|
|
|
|
|
|
|
2019-10-20 18:51:09 +02:00
|
|
|
def create_bucket(bucket_name, region=None):
|
|
|
|
"""Create an S3 bucket in a specified region
|
2018-07-11 07:39:36 +02:00
|
|
|
|
2019-10-20 18:51:09 +02:00
|
|
|
If a region is not specified, the bucket is created in the S3 default
|
|
|
|
region (us-east-1).
|
2018-07-11 07:39:36 +02:00
|
|
|
|
2019-10-20 18:51:09 +02:00
|
|
|
:param bucket_name: Bucket to create
|
|
|
|
:param region: String region to create bucket in, e.g., 'us-west-2'
|
|
|
|
:return: True if bucket created, else False
|
|
|
|
"""
|
2018-07-11 07:39:36 +02:00
|
|
|
|
2019-10-20 18:51:09 +02:00
|
|
|
# Create bucket
|
|
|
|
try:
|
|
|
|
if region is None:
|
|
|
|
s3_client = boto3.client('s3')
|
|
|
|
s3_client.create_bucket(Bucket=bucket_name)
|
|
|
|
else:
|
|
|
|
s3_client = boto3.client('s3', region_name=region)
|
|
|
|
location = {'LocationConstraint': region}
|
|
|
|
s3_client.create_bucket(Bucket=bucket_name,
|
|
|
|
CreateBucketConfiguration=location)
|
|
|
|
except ClientError as e:
|
|
|
|
logging.error(e)
|
|
|
|
return False
|
|
|
|
return True
|
2018-07-11 07:39:36 +02:00
|
|
|
|
|
|
|
|
2021-09-22 18:34:33 +02:00
|
|
|
List existing buckets
|
2019-10-20 18:51:09 +02:00
|
|
|
=====================
|
2018-07-11 07:39:36 +02:00
|
|
|
|
2019-10-20 18:51:09 +02:00
|
|
|
List all the existing buckets for the AWS account.
|
2018-07-11 07:39:36 +02:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2019-10-20 18:51:09 +02:00
|
|
|
# Retrieve the list of existing buckets
|
2018-07-11 07:39:36 +02:00
|
|
|
s3 = boto3.client('s3')
|
2019-10-20 18:51:09 +02:00
|
|
|
response = s3.list_buckets()
|
2018-07-11 07:39:36 +02:00
|
|
|
|
2019-10-20 18:51:09 +02:00
|
|
|
# Output the bucket names
|
|
|
|
print('Existing buckets:')
|
|
|
|
for bucket in response['Buckets']:
|
|
|
|
print(f' {bucket["Name"]}')
|
2018-07-11 07:39:36 +02:00
|
|
|
|