Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeClose MenuSkip to content
Boto3 1.39.8 documentation
Light LogoDark Logo
Boto3 1.39.8 documentation

Feedback

Do you have a suggestion to improve this website or boto3?Give us feedback.

Back to top

Quickstart

This guide details the steps needed to install or update the AWS SDK for Python.

The SDK is composed of two key Python packages: Botocore (the library providing the low-levelfunctionality shared between the Python SDK and the AWS CLI) and Boto3 (the package implementing thePython SDK itself).

Note

Documentation and developers tend to refer to the AWS SDK for Python as “Boto3,” and thisdocumentation often does so as well.

Installation

To use Boto3, you first need to install it and its dependencies.

Install or update Python

Before installing Boto3, install Python 3.9 or later; support for Python 3.8 andearlier is deprecated. After the deprecation date listed for each Pythonversion, new releases of Boto3 will not include support for that version ofPython. For details, including the deprecation schedule and how to update yourproject to use Python 3.9, seeMigrating to Python 3.

For information about how to get the latest version of Python, see the officialPythondocumentation.

Setup a virtual environment

Once you have a supported version of Python installed, you should set upyour workspace by creating a virtual environment and activate it:

$ python -m venv .venv...$ source .venv/bin/activate

This provides an isolated space for your installation that will avoid unexpectedinteractions with packages installed at the system level. Skipping this step mayresult in unexpected dependency conflicts or failures with other tools installedon your system.

Install Boto3

Install the latest Boto3 release viapip:

pipinstallboto3

If your project requires a specific version of Boto3, or has compatibility concerns withcertain versions, you may provide constraints when installing:

# Install Boto3 version 1.0 specificallypipinstallboto3==1.0.0# Make sure Boto3 is no older than version 1.15.0pipinstallboto3>=1.15.0# Avoid versions of Boto3 newer than version 1.15.3pipinstallboto3<=1.15.3

Note

The latest development version of Boto3 is onGitHub.

Using the AWS Common Runtime (CRT)

In addition to the default install of Boto3, you can choose to include the newAWS Common Runtime(CRT). The AWS CRT is a collection of modular packages that serve as a new foundation for AWS SDKs.Each library provides better performance and minimal footprint for the functional area itimplements. Using the CRT, SDKs can share the same base code when possible, improving consistencyand throughput optimizations across AWS SDKs.

When the AWS CRT is included, Boto3 uses it to incorporate features not otherwiseavailable in the AWS SDK for Python.

You’ll find it used in features like:

However, Boto3 doesn’t use the AWS CRT by default but you can opt into using it by specifying thecrtextra feature when installing Boto3:

pipinstallboto3[crt]

To revert to the non-CRT version of Boto3, use this command:

pipuninstallawscrt

If you need to re-enable CRT, reinstallboto3[crt] to ensure you get a compatible version ofawscrt:

pipinstallboto3[crt]

Configuration

Before using Boto3, you need to set up authentication credentials for your AWS account using eithertheIAM Console or the AWS CLI. You can either choosean existing user or create a new one.

For instructions about how to create a user using the IAM Console, seeCreating IAM users.Once the user has been created, seeManaging access keysto learn how to create and retrieve the keys used to authenticate the user.

If you have theAWS CLI installed, then you can use theaws configure command to configure your credentials file:

awsconfigure

Alternatively, you can create the credentials file yourself. By default, its location is~/.aws/credentials. At a minimum, the credentials file should specify the access key and secretaccess key. In this example, the key and secret key for the account are specified in thedefault profile:

[default]aws_access_key_id=YOUR_ACCESS_KEYaws_secret_access_key=YOUR_SECRET_KEY

You may also want to add a default region to the AWS configuration file, which is located by defaultat~/.aws/config:

[default]region=us-east-1

Alternatively, you can pass aregion_name when creating clients and resources.

You have now configured credentials for the default profile as well as a default region to use whencreating connections. SeeConfiguration for in-depth configuration sources and options.

Using Boto3

To use Boto3, you must first import it and indicate which service or services you’re going to use:

importboto3# Let's use Amazon S3s3=boto3.resource('s3')

Now that you have ans3 resource, you can make send requests to the service. The following code uses thebuckets collection to print out all bucket names:

# Print out bucket namesforbucketins3.buckets.all():print(bucket.name)

You can also upload and download binary data. For example, the followinguploads a new file to S3, assuming that the bucketamzn-s3-demo-bucketalready exists:

# Upload a new filewithopen('test.jpg','rb')asdata:s3.Bucket('amzn-s3-demo-bucket').put_object(Key='test.jpg',Body=data)

Resources andCollections are covered in more detail in the followingsections.

On this page

[8]ページ先頭

©2009-2025 Movatter.jp