boto3 and boto

The boto3 and boto development tools offer SDKs for the Python 2.x and 3.x programming languages. The SDKs are designed for working with AWS services.

Before you start

  1. Create a service account.
  2. Get the necessary roles.
  3. Get a static key.

Installation

Using pip:

pip install boto3

From sources:

git clone https://github.com/boto/boto3.git
cd boto3
python setup.py install

Using pip:

pip install boto

From sources:

git clone git://github.com/boto/boto.git
cd boto
python setup.py install

Setup

To configure the SDK, create configuration files in your home folder and set the following in them:

  • The static key in the file .aws/credentials:

    [default]
                aws_access_key_id = <id>
                aws_secret_access_key = <secretKey>
    
  • The default region in the file .aws/config:

    [default]
                region=us-east-1
    

    Note

    To work with Yandex Object Storage, always specify the us-east-1 region. A different value of the region may lead to an authorization error.

To access Object Storage, use the address storage.yandexcloud.net.

Example

#!/usr/bin/env python
#-*- coding: utf-8 -*-
import boto3
session = boto3.session.Session()
s3 = session.client(
    service_name='s3',
    endpoint_url='https://storage.yandexcloud.net'
)

# Create a new bucket
s3.create_bucket(Bucket='bucket-name')

# Upload objects to the bucket

## From a string
s3.put_object(Bucket='bucket-name', Key='object_name', Body='TEST', StorageClass='COLD')

## From a file
s3.upload_file('this_script.py', 'bucket-name', 'py_script.py')
s3.upload_file('this_script.py', 'bucket-name', 'script/py_script.py')

# Get the list of objects in the bucket
for key in s3.list_objects(Bucket='bucket-name')['Contents']:
    print(key['Key'])

# Delete multiple objects
forDeletion = [{'Key':'object_name'}, {'Key':'script/py_script.py'}]
response = s3.delete_objects(Bucket='bucket-name', Delete={'Objects': forDeletion})

# Retrieve an object
get_object_response = s3.get_object(Bucket='bucket-name',Key='py_script.py')
print get_object_response['Body'].read()
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import os
from boto.s3.key import Key
from boto.s3.connection import S3Connection
os.environ['S3_USE_SIGV4'] = 'True'
conn = S3Connection(
    host='storage.api.cloud.yandex.net'
)
conn.auth_region_name = 'us-east-1'

# Create a new bucket
conn.create_bucket('bucket-name')
bucket = conn.get_bucket('bucket-name')

# Upload objects to the bucket

## From a string
bucket.new_key('test-string').set_contents_from_string('TEST')

## From a file
file_key_1 = Key(bucket)
file_key_1.key = 'py_script.py'
file_key_1.set_contents_from_filename('this_script.py')
file_key_2 = Key(bucket)
file_key_2.key = 'script/py_script.py'
file_key_2.set_contents_from_filename('this_script.py')

# Get a list of objects in the bucket
keys_list=bucket.list()
for key in keys_list:
    print key.key

# Delete multiple objects
response = bucket.delete_keys(['test-string', 'py_script.py'])

# Retrieve an object
key = bucket.get_key('script/py_script.py')
print key.get_contents_as_string()