Petabox
  • Introduction
  • Petabox FAQ
  • Concepts
    • Bucket versioning
    • Logging actions with a bucket
    • Object Lock
  • Tools
    • Supported tools
      • CyberDuck S3 Client
      • Mountain Duck
      • S3 Browser
      • RClone
      • AWS CLI Console client
      • SDKs for different languages
        • JavaScript SDK
        • AWS SDK for Java
        • Python SDK (boto)
  • S3 Compatible API
    • How to use the API
    • Signing Requests
    • API Reference
      • Bucket
        • HeadBucket
        • ListObjects/ListObjectsV2
        • PutBucketVersioning
        • PutBucketLogging
        • ListBuckets
        • RenameBucket
        • GetBucketLocation
      • Object
        • PutObject
        • GetObject
        • HeadObject
        • RenameObject
        • PutObjectAcl
      • Multipart upload
        • General multipart upload order
        • CreateMultipartUpload
        • UploadPart
        • CompleteMultipartUpload
        • ListMultipartUploads
      • Analytics
        • GetStatistics
        • GetBandwidthAnalytics
        • GetStorageAnalytics
      • Common request headers
      • Common response headers
      • Responses
      • GetObjectTagging
      • GetObjectAcl
      • GetBucketAcl
      • ListObjectVersions
      • GetBucketRequestPayment
      • GetBucketReplication
      • GetBucketTagging
      • GetBucketLocation
      • GetBucketVersioning
      • GetBucketLifecycle
      • GetObjectLockConfiguration
      • PutObjectLockConfiguration
      • GetObjectRetention
      • GetObjectLegalHold
      • PutObjectRetention
      • PutObjectLegalHold
      • PutObjectAcl
      • PutBucketAcl
      • PutBucketVersioning
      • CopyObject
      • DeleteObjects
      • AbortMultipartUpload
      • DeleteObject
      • DeleteBucket
      • PutBucketTagging
      • PutObjectTagging
      • DeleteBucketTagging
      • DeleteObjectTagging
      • PutBucketLogging
      • GetBucketLogging
      • ListParts
      • UploadPartCopy
      • PutBucketPolicy
      • GetBucketPolicy
      • DeleteBucketPolicy
Powered by GitBook
On this page
  • boto3 and boto
  • Preparation for work
  • Installation
  • Customization
  • Example
  1. Tools
  2. Supported tools
  3. SDKs for different languages

Python SDK (boto)

PreviousAWS SDK for JavaNextHow to use the API

Last updated 3 years ago

boto3 and boto

and 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: , .

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.petabox.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.petabox.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())
#!/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='s3.petabox.io'
)
conn.auth_region_name = 'us-east-1'

# Создать новый бакет
conn.create_bucket('bucket-name')
bucket = conn.get_bucket('bucket-name')

# Загрузить объекты в бакет

## Из строки
bucket.new_key('test-string').set_contents_from_string('TEST')

## Из файла
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')

# Получить список объектов в бакете
keys_list=bucket.list()
for key in keys_list:
    print key.key

# Удалить несколько объектов
response = bucket.delete_keys(['test-string', 'py_script.py'])

# Получить объект
key = bucket.get_key('script/py_script.py')
print key.get_contents_as_string()

boto3
boto
boto3
boto