# Amazon S3 code examples for the SDK for Python ## Overview Shows how to use the AWS SDK for Python (Boto3) to work with Amazon Simple Storage Service (Amazon S3). *Amazon S3 is storage for the internet. You can use Amazon S3 to store and retrieve any amount of data at any time, from anywhere on the web.* ## ⚠ Important * Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/?aws-products-pricing.sort-by=item.additionalFields.productNameLowercase&aws-products-pricing.sort-order=asc&awsf.Free%20Tier%20Type=*all&awsf.tech-category=*all) and [Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all). * Running the tests might result in charges to your AWS account. * We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). * This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). ## Code examples ### Prerequisites For prerequisites, see the [README](../../README.md#Prerequisites) in the `python` folder. Install the packages required by these examples by running the following in a virtual environment: ``` python -m pip install -r requirements.txt ``` ### Get started * [Hello Amazon S3](s3_basics/hello.py#L4) (`ListBuckets`) ### Single actions Code excerpts that show you how to call individual service functions. * [Add CORS rules to a bucket](s3_basics/bucket_wrapper.py#L164) (`PutBucketCors`) * [Add a lifecycle configuration to a bucket](s3_basics/bucket_wrapper.py#L260) (`PutBucketLifecycleConfiguration`) * [Add a policy to a bucket](s3_basics/bucket_wrapper.py#L214) (`PutBucketPolicy`) * [Copy an object from one bucket to another](s3_basics/object_wrapper.py#L111) (`CopyObject`) * [Create a bucket](s3_basics/bucket_wrapper.py#L33) (`CreateBucket`) * [Delete CORS rules from a bucket](s3_basics/bucket_wrapper.py#L199) (`DeleteBucketCors`) * [Delete a policy from a bucket](s3_basics/bucket_wrapper.py#L247) (`DeleteBucketPolicy`) * [Delete an empty bucket](s3_basics/bucket_wrapper.py#L101) (`DeleteBucket`) * [Delete an object](s3_basics/object_wrapper.py#L137) (`DeleteObject`) * [Delete multiple objects](s3_basics/object_wrapper.py#L155) (`DeleteObjects`) * [Delete the lifecycle configuration of a bucket](s3_basics/bucket_wrapper.py#L300) (`DeleteBucketLifecycle`) * [Determine the existence of a bucket](s3_basics/bucket_wrapper.py#L61) (`HeadBucket`) * [Get CORS rules for a bucket](s3_basics/bucket_wrapper.py#L181) (`GetBucketCors`) * [Get an object from a bucket](s3_basics/object_wrapper.py#L66) (`GetObject`) * [Get the ACL of a bucket](s3_basics/bucket_wrapper.py#L146) (`GetBucketAcl`) * [Get the ACL of an object](s3_basics/object_wrapper.py#L239) (`GetObjectAcl`) * [Get the lifecycle configuration of a bucket](s3_basics/bucket_wrapper.py#L281) (`GetBucketLifecycleConfiguration`) * [Get the policy for a bucket](s3_basics/bucket_wrapper.py#L230) (`GetBucketPolicy`) * [List buckets](s3_basics/bucket_wrapper.py#L80) (`ListBuckets`) * [List objects in a bucket](s3_basics/object_wrapper.py#L21) (`ListObjectsV2`) * [Set a new ACL for a bucket](s3_basics/bucket_wrapper.py#L115) (`PutBucketAcl`) * [Set the ACL of an object](s3_basics/object_wrapper.py#L207) (`PutObjectAcl`) * [Upload an object to a bucket](s3_basics/object_wrapper.py#L33) (`PutObject`) ### Scenarios Code examples that show you how to accomplish a specific task by calling multiple functions within the same service. * [Create a presigned URL](s3_basics/presigned_url.py) * [Get started with buckets and objects](s3_basics/scenario_getting_started.py) * [Manage versioned objects in batches with a Lambda function](s3_versioning/batch_versioning.py) * [Upload or download large files](file_transfer/file_transfer.py) * [Work with versioned objects](s3_versioning/versioning.py) ### Cross-service examples Sample applications that work across multiple AWS services. * [Create an Amazon Textract explorer application](../../cross_service/textract_explorer) * [Detect entities in text extracted from an image](../../cross_service/textract_comprehend_notebook) * [Detect objects in images](../../cross_service/photo_analyzer) ## Run the examples ### Instructions #### Hello Amazon S3 This example shows you how to get started using Amazon S3. ``` python hello.py ``` #### Create a presigned URL This example shows you how to create a presigned URL for Amazon S3 and upload an object. Start the example by running the following at a command prompt: ``` python s3_basics/presigned_url.py ``` #### Get started with buckets and objects This example shows you how to do the following: * Create a bucket and upload a file to it. * Download an object from a bucket. * Copy an object to a subfolder in a bucket. * List the objects in a bucket. * Delete the bucket objects and the bucket. Start the example by running the following at a command prompt: ``` python s3_basics/scenario_getting_started.py ``` #### Manage versioned objects in batches with a Lambda function This example shows you how to manage versioned S3 objects in batches with a Lambda function. Start the example by running the following at a command prompt: ``` python s3_versioning/batch_versioning.py ``` #### Upload or download large files This example shows you how to upload or download large files to and from Amazon S3. Start the example by running the following at a command prompt: ``` python file_transfer/file_transfer.py ``` #### Work with versioned objects This example shows you how to do the following: * Create a versioned S3 bucket. * Get all versions of an object. * Roll an object back to a previous version. * Delete and restore a versioned object. * Permanently delete all versions of an object. Start the example by running the following at a command prompt: ``` python s3_versioning/versioning.py ``` ### Tests ⚠ Running tests might result in charges to your AWS account. To find instructions for running these tests, see the [README](../../README.md#Tests) in the `python` folder. ## Additional resources * [Amazon S3 User Guide](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) * [Amazon S3 API Reference](https://docs.aws.amazon.com/AmazonS3/latest/API/Welcome.html) * [SDK for Python Amazon S3 reference](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html) --- Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0