AWS - boto3 - setup
Boto3
Installation
1
2
3
4
5
pip install boto3
pip3 install boto3 botostubs
pip install boto3==1.0.0
Configuration
Before you can begin using Boto3, set up authentication credentials.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# can create the credential fileself.
# By default, its location is at ~/.aws/credentials:
$ touch ~/.aws/credentials
# [default]
# aws_access_key_id = YOUR_ACCESS_KEY_ID
# aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
# region = YOUR_PREFERRED_REGION
# If you have the AWS CLI installed, then you can use it to configure credentials file:
$ aws configure
$ aws configure --profile produser
# AWS Access Key ID [None]: aa
# AWS Secret Access Key [None]: bb/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
# Default region name [None]: us-east-1
# Default output format [None]: json
# Alternatively, you can pass a region_name when creating clients and resources.
# This sets up credentials for the default profile as well as a default region to use when creating connections.
Using Boto3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# import it and tell it what service you are going to use:
import boto3
# use Amazon S3
s3 = boto3.resource('s3')
# Now that you have an s3 resource, you can make requests and process responses from the service.
# The following uses the buckets collection to print out all bucket names:
# Print out bucket names
for bucket in s3.buckets.all():
print(bucket.name)
# It's also easy to upload and download binary data.
# For example, the following uploads a new file to S3.
# It assumes that the bucket my-bucket already exists:
# Upload a new file
data = open('test.jpg', 'rb')
s3.Bucket('my-bucket').put_object(Key='test.jpg', Body=data)
Client Versus Resource
- Client: low-level service access
- Resource: higher-level object-oriented service access
Boto3 reference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class boto3.NullHandler(level=0)
# Initializes the instance - basically setting the formatter to None and the filter list to empty.
emit(record)
boto3.client(*args, **kwargs)
# Create a low-level service client by name using the default session.
boto3.resource(*args, **kwargs)
# Create a resource service client by name using the default session.
boto3.set_stream_logger(name='boto3', level=10, format_string=None)
# Add a stream handler for the given name and level to the logging module.
# By default, this logs all boto3 messages to stdout.
>>> import boto3
>>> boto3.set_stream_logger('boto3.resources', logging.INFO)
# For debugging purposes a good choice is to set the stream logger to '' which is equivalent to saying "log everything".
# Warning: Be aware that when logging anything from 'botocore' the full wire trace will appear in your logs. If your payloads contain sensitive data this should not be used in production.
boto3.setup_default_session(**kwargs)
# Set up a default session, passing through any parameters to the session constructor. There is no need to call this unless you wish to pass custom parameters, because a default session will be created for you.
ref
.
This post is licensed under CC BY 4.0 by the author.
Comments powered by Disqus.