Back to Aws Doc Sdk Examples

Amazon S3 code examples for the SDK for Python

python/example_code/s3/README.md

latest9.9 KB
Original Source

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).

<!--custom.overview.start--> <!--custom.overview.end-->

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 and Free Tier.
  • 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.
  • This code is not tested in every AWS Region. For more information, see AWS Regional Services.
<!--custom.important.start--> <!--custom.important.end-->

Code examples

Prerequisites

For prerequisites, see the README 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
<!--custom.prerequisites.start--> <!--custom.prerequisites.end-->

Get started

Basics

Code examples that show you how to perform the essential operations within a service.

Single actions

Code excerpts that show you how to call individual service functions.

Scenarios

Code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

<!--custom.examples.start--> <!--custom.examples.end-->

Run the examples

Instructions

<!--custom.instructions.start--> <!--custom.instructions.end-->

Hello Amazon S3

This example shows you how to get started using Amazon S3.

python s3_basics/hello.py

Learn the basics

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.
<!--custom.basic_prereqs.s3_Scenario_GettingStarted.start--> <!--custom.basic_prereqs.s3_Scenario_GettingStarted.end-->

Start the example by running the following at a command prompt:

python s3_basics/scenario_getting_started.py
<!--custom.basics.s3_Scenario_GettingStarted.start--> <!--custom.basics.s3_Scenario_GettingStarted.end-->

Create a presigned URL

This example shows you how to create a presigned URL for Amazon S3 and upload an object.

<!--custom.scenario_prereqs.s3_Scenario_PresignedUrl.start--> <!--custom.scenario_prereqs.s3_Scenario_PresignedUrl.end-->

Start the example by running the following at a command prompt:

python s3_basics/presigned_url.py
<!--custom.scenarios.s3_Scenario_PresignedUrl.start--> <!--custom.scenarios.s3_Scenario_PresignedUrl.end-->

Create an Amazon Textract explorer application

This example shows you how to explore Amazon Textract output through an interactive application.

<!--custom.scenario_prereqs.cross_TextractExplorer.start--> <!--custom.scenario_prereqs.cross_TextractExplorer.end--> <!--custom.scenarios.cross_TextractExplorer.start--> <!--custom.scenarios.cross_TextractExplorer.end-->

Detect entities in text extracted from an image

This example shows you how to use Amazon Comprehend to detect entities in text extracted by Amazon Textract from an image that is stored in Amazon S3.

<!--custom.scenario_prereqs.cross_TextractComprehendDetectEntities.start--> <!--custom.scenario_prereqs.cross_TextractComprehendDetectEntities.end--> <!--custom.scenarios.cross_TextractComprehendDetectEntities.start--> <!--custom.scenarios.cross_TextractComprehendDetectEntities.end-->

Detect objects in images

This example shows you how to build an app that uses Amazon Rekognition to detect objects by category in images.

<!--custom.scenario_prereqs.cross_RekognitionPhotoAnalyzer.start--> <!--custom.scenario_prereqs.cross_RekognitionPhotoAnalyzer.end--> <!--custom.scenarios.cross_RekognitionPhotoAnalyzer.start--> <!--custom.scenarios.cross_RekognitionPhotoAnalyzer.end-->

Detect people and objects in a video

This example shows you how to detect people and objects in a video with Amazon Rekognition.

<!--custom.scenario_prereqs.cross_RekognitionVideoDetection.start--> <!--custom.scenario_prereqs.cross_RekognitionVideoDetection.end--> <!--custom.scenarios.cross_RekognitionVideoDetection.start--> <!--custom.scenarios.cross_RekognitionVideoDetection.end-->

Make conditional requests

This example shows you how to add preconditions to Amazon S3 requests.

<!--custom.scenario_prereqs.s3_Scenario_ConditionalRequests.start--> <!--custom.scenario_prereqs.s3_Scenario_ConditionalRequests.end-->

Start the example by running the following at a command prompt:

python scenarios/conditional_requests/scenario.py
<!--custom.scenarios.s3_Scenario_ConditionalRequests.start--> <!--custom.scenarios.s3_Scenario_ConditionalRequests.end-->

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.

<!--custom.scenario_prereqs.s3_Scenario_BatchObjectVersioning.start-->

Start the example by running the following at a command prompt:

<!--custom.scenario_prereqs.s3_Scenario_BatchObjectVersioning.end--> <!--custom.scenarios.s3_Scenario_BatchObjectVersioning.start--> <!--custom.scenarios.s3_Scenario_BatchObjectVersioning.end-->

Upload or download large files

This example shows you how to upload or download large files to and from Amazon S3.

<!--custom.scenario_prereqs.s3_Scenario_UsingLargeFiles.start--> <!--custom.scenario_prereqs.s3_Scenario_UsingLargeFiles.end-->

Start the example by running the following at a command prompt:

python file_transfer/file_transfer.py
<!--custom.scenarios.s3_Scenario_UsingLargeFiles.start--> <!--custom.scenarios.s3_Scenario_UsingLargeFiles.end-->

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.
<!--custom.scenario_prereqs.s3_Scenario_ObjectVersioningUsage.start--> <!--custom.scenario_prereqs.s3_Scenario_ObjectVersioningUsage.end-->

Start the example by running the following at a command prompt:

python s3_versioning/versioning.py
<!--custom.scenarios.s3_Scenario_ObjectVersioningUsage.start--> <!--custom.scenarios.s3_Scenario_ObjectVersioningUsage.end-->

Tests

⚠ Running tests might result in charges to your AWS account.

To find instructions for running these tests, see the README in the python folder.

<!--custom.tests.start--> <!--custom.tests.end-->

Additional resources

<!--custom.resources.start--> <!--custom.resources.end-->

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0