Python SDK (boto)

boto3 and boto

boto3 and boto are development kits (SDKs) for the Python 2.x and 3.x programming languages. SDKs are designed to work with AWS services.

Preparation for work

  1. 1.
    Create a service account .
  2. 2.
    Assign a role to a service account .
  3. 3.
    Create a static access key .

Installation

To install boto, follow the instructions in the developer's repository: boto3 , boto .

Customization

To configure, create configuration files in your home directory and specify in them:
  • Static key in file .aws/credentials:
    1
    [default]
    2
    aws_access_key_id = <id>
    3
    aws_secret_access_key = <secretKey>
    Copied!
  • Default region in file .aws/config:
    1
    [default]
    2
    region=us-east-1
    Copied!
Use the address to access Object Storage s3.coldstack.io.

Example

boto3
boto
1
#!/usr/bin/env python
2
#-*- coding: utf-8 -*-
3
import boto3
4
session = boto3.session.Session()
5
s3 = session.client(
6
service_name='s3',
7
endpoint_url='https://s3.coldstack.io'
8
)
9
10
# Uploading objects to the bucket
11
12
## From a string
13
s3.put_object(Bucket='bucket-name', Key='object_name', Body='TEST', StorageClass='COLD')
14
15
## From a file
16
s3.upload_file('this_script.py', 'bucket-name', 'py_script.py')
17
s3.upload_file('this_script.py', 'bucket-name', 'script/py_script.py')
18
19
# Get a list of objects in bucket
20
for key in s3.list_objects(Bucket='bucket-name')['Contents']:
21
print(key['Key'])
22
23
# Download an object and print the contents to the console
24
get_object_response = s3.get_object(Bucket='bucket-name',Key='py_script.py')
25
print(get_object_response['Body'].read())
Copied!
1
#!/usr/bin/env python
2
#-*- coding: utf-8 -*-
3
import os
4
from boto.s3.key import Key
5
from boto.s3.connection import S3Connection
6
os.environ['S3_USE_SIGV4'] = 'True'
7
conn = S3Connection(
8
host='s3.coldstack.io'
9
)
10
conn.auth_region_name = 'us-east-1'
11
12
# Создать новый бакет
13
conn.create_bucket('bucket-name')
14
bucket = conn.get_bucket('bucket-name')
15
16
# Загрузить объекты в бакет
17
18
## Из строки
19
bucket.new_key('test-string').set_contents_from_string('TEST')
20
21
## Из файла
22
file_key_1 = Key(bucket)
23
file_key_1.key = 'py_script.py'
24
file_key_1.set_contents_from_filename('this_script.py')
25
file_key_2 = Key(bucket)
26
file_key_2.key = 'script/py_script.py'
27
file_key_2.set_contents_from_filename('this_script.py')
28
29
# Получить список объектов в бакете
30
keys_list=bucket.list()
31
for key in keys_list:
32
print key.key
33
34
# Удалить несколько объектов
35
response = bucket.delete_keys(['test-string', 'py_script.py'])
36
37
# Получить объект
38
key = bucket.get_key('script/py_script.py')
39
print key.get_contents_as_string()
Copied!