I tried to find out how to use Boto3, which is the AWS SDK for Python, by looking at the Documentation again.
According to the PyPI page, it works on 2.6 or higher for 2 series and 3.3 or higher for 3 series.
The following is
We have confirmed the operation in the environment of.
The "Major Features" section of the What's New page outlines the following five features: ..
-** Resources : High-level object-oriented interface - Collections : Iterator working with multiple resources - Clients : Low level service connection - Paginators : Automatic paging - Waiters **: Wait until a certain state is reached
I didn't understand this configuration and until now I've confused ** Resources ** with ** Clients **.
Note that the high-level API is not available for all AWS services, and so far it seems that only some services such as EC2 and S3 support it.
Easy to install with pip.
$ pip install boto3
Since IAM access key is required for AWS operation, create it from the management console in advance and set appropriate permissions for the user.
On the terminal side, set the access key information to ~ / .aws / credentials
.
If you're using awscli, this file was already generated when you ʻaws configure`, but if not, set up awscli or set the access key directly in your credentials file.
~/.aws/credentials
[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY
You can access the bucket through the S3.Bucket object.
import boto3
#Bucket name
AWS_S3_BUCKET_NAME = 'hogehoge'
s3 = boto3.resource('s3')
bucket = s3.Bucket(AWS_S3_BUCKET_NAME)
print(bucket.name)
# => hogehoge
You can access information about S3 objects stored in your bucket through the attribute ʻobjects`.
This attribute is an instance of the Bucket.objectsCollectionManager
class, and the methods of ʻall (),
delete (),
filter (),
limit (), and
page_size ()are available. These methods return an instance of the
s3.Bucket.objectsCollection class, and you can iterate this object to get an instance of the ʻObjectSummary
class.
print(bucket.objects.all())
# => s3.Bucket.objectsCollection(s3.Bucket(name='hogehoge'), s3.ObjectSummary)
print([obj_summary.key for obj_summary in bucket.objects.all()])
# => ['hayabusa.txt']
The operation using ʻobjects` is effective when the target object is not specified, such as when searching for the object stored in the bucket.
If you want to get an S3 object whose key is known, use the S3.Object class.
GET_OBJECT_KEY_NAME = 'hayabusa.txt'
obj = bucket.Object(GET_OBJECT_KEY_NAME)
print(obj.key)
# => hayabusa.txt
ʻObject` objects can also be created by specifying the bucket name and key name without going through the Bucket object.
obj = s3.Object(AWS_S3_BUCKET_NAME, GET_OBJECT_KEY_NAME)
print(obj.key)
# => hayabusa.txt
To get the contents of an S3 object, use the object's get ()
method.
The return value of the get ()
method is a dictionary, and you can refer to the contents of the object through Body
in that dictionary.
This Body
is an instance of the botocore.response.StreamingBody
class and is a stream that handles byte data. Therefore, in order to handle it as a character string, it is necessary to read it from the stream and convert it to a character string type.
response = obj.get()
body = response['Body'].read()
print(type(body))
# => <class 'bytes'>
print(body.decode('utf-8'))
# =>10 round trips from Tokyo to Shin-Hakodate Hokuto
#Sendai-Shin-Hakodate Hokuto 1 round trip
Note that once the stream is read ()
, it will be sought at the end of the stream, so the results cannot be obtained with the second and subsequent calls.
As with retrieval, you can use the ʻObject` object to add new S3 objects to your bucket and update their contents.
To set the contents of the S3 object, pass the contents you want to save as a byte string in the argument Body
of theput ()
method. You can also specify detailed options such as ʻACLand
ContentType` with arguments.
PUT_OBJECT_KEY_NAME = 'hayate.txt'
obj = bucket.Object(PUT_OBJECT_KEY_NAME)
body = """1 round trip from Morioka to Shin-Hakodate Hokuto
Shin-Aomori-Shin-Hakodate Hokuto 1 round trip
"""
response = obj.put(
Body=body.encode('utf-8'),
ContentEncoding='utf-8',
ContentType='text/plane'
)
By using the S3.Client object, it is possible to operate using a low-level API.
For example, getting an S3 object can also be written using the low-level API as follows:
s3 = boto3.resource('s3')
client = s3.meta.client
response = client.get_object(Bucket=AWS_S3_BUCKET_NAME, Key=GET_OBJECT_KEY_NAME)
body = response['Body'].read()
print(body.decode('utf-8'))
# =>10 round trips from Tokyo to Shin-Hakodate Hokuto
#Sendai-Shin-Hakodate Hokuto 1 round trip
Until now, low-level APIs (Clients) and high-level APIs (Resources) were confused.
There are some features that are only provided by the low-level API, but you can write object-oriented programs, so if you have a high-level API, you should use that.
Recommended Posts