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. Create a service account .

  2. Assign a role to a service account .

  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:

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

    [default]
                region=us-east-1

Use the address to access Object Storage s3.coldstack.io.

Example

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

# Uploading 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 a list of objects in bucket
for key in s3.list_objects(Bucket='bucket-name')['Contents']:
    print(key['Key'])

# Download an object and print the contents to the console
get_object_response = s3.get_object(Bucket='bucket-name',Key='py_script.py')
print(get_object_response['Body'].read())

Last updated