# 2013-05-20 johnpfeiffer
from boto.sts import STSConnection # http://boto.readthedocs.org/en/latest/ref/sts.html
from boto.s3.connection import S3Connection
from boto.s3.key import Key
import time
start = time.time()
sts_conn = STSConnection( 'AKIAxxxx' , 'YouRSecRetKeY' )
temp_session = sts_conn.get_session_token( 900 ) # 15 minute is the minimum duration for temp credentials
print "temp session = " , temp_session
print 'temp session generated in {} seconds'.format( time.time() - start )
start = time.time()
temp_connection = S3Connection( temp_session.access_key , temp_session.secret_key , security_token = temp_session.session_token )
print "temp connection = " , temp_connection
print 'temp connection opened in {} seconds'.format( time.time() - start )
start = time.time()
buckets = temp_connection.get_all_buckets()
print len( buckets ), "buckets:"
for bucket in buckets:
print bucket.name
print 'bucket listing in {} seconds'.format( time.time() - start )
start = time.time()
bucket = temp_connection.get_bucket( 'test-bucket' )
print "\nbucket = " , bucket
k = bucket.get_key( 'helloworld' )
print "key =" , k
print "size =" , k.size
print "content-type =" , k.content_type
new_file_key = Key( bucket )
temp_key_name = 'random-s3-object-identifier'
new_file_key.key = temp_key_name
new_file_key.set_contents_from_string( 'random content' ) # boto auto detects mime type and uploads
# new_file_key.set_contents_from_stream()
# new_file_key.set_contents_from_file() , if using from_filename I guess it's a local relative path?
retrieved_key = bucket.get_key( temp_key_name )
print 'got bucket and uploaded to it in {} seconds'.format( time.time() - start )
import requests
from cStringIO import StringIO
name = 'testing-presigned-upload-url'
string_buffer = StringIO()
string_buffer.write( 'testing-presigned-upload-url-CONTENT-HERE' )
data = string_buffer.getvalue()
headers = {'Content-Length': str( len( data ) ) }
start = time.time()
upload_url = temp_connection.generate_url( expires_in=300, method='PUT', bucket='test-bucket', key=name, headers=headers ) # if set to 'GET' and without headers can be used for download
print upload_url
response = requests.put( upload_url, data=data )
print "BOTO generated presigned upload url http response code:", response.status_code
print 'generated pre-signed url and uploaded with http put completed in {} seconds'.format( time.time() - start )
"""
import boto.glacier.layer2 # friendlier boto interface on top of boto layer1 http://boto.readthedocs.org/en/latest/ref/glacier.html
# Various variables for AWS creds, vault name, local file name
awsAccess = "AKIAxxxx"
awsSecret = "YouRSecRetKeY"
vaultName = "YourVaultName"
fileName = "LocalFileName"
connection = boto.glacier.layer2.Layer2( aws_access_key_id=awsAccess, aws_secret_access_key=awsSecret )
print connection.list_vaults()
vault = connection.get_vault( vaultName ) # vault must already have been created
archiveID = vault.create_archive_from_file( fileName ) # get the archiveId for the file, usually you pass the filename and file-like-object together
vault.delete_archive( archiveID ) # probably not immediately useful
"""