Skip to content

Instantly share code, notes, and snippets.

@chmouel
Last active May 18, 2018 12:05
Show Gist options
  • Save chmouel/5310982 to your computer and use it in GitHub Desktop.
Save chmouel/5310982 to your computer and use it in GitHub Desktop.
Connect to python swift boto.
#!/usr/bin/python
# Chmouel Boudjnah <[email protected]>
# source devstack/eucarc first
import boto
import sys
import boto.s3.connection
import boto.s3.key
import boto.exception
import StringIO
import os
import urlparse
s3_url = os.environ.get('S3_URL')
if not s3_url:
print "Source your devstack eucarc or provide a " \
"S3_URL EC2_SECRET_KEY EC2_SECRET_KEY env variable."
sys.exit(1)
parsed = urlparse.urlparse(s3_url)
print parsed
connection = boto.connect_s3(
aws_access_key_id=os.environ.get('EC2_ACCESS_KEY'),
aws_secret_access_key=os.environ.get('EC2_SECRET_KEY'),
port=parsed.port,
host=parsed.hostname,
is_secure=False,
calling_format=boto.s3.connection.OrdinaryCallingFormat())
try:
bucket = connection.get_bucket("cont_s3")
for x in bucket.get_all_keys():
print "Delete key: %s" % (x.name)
x.delete()
print "Deleting bucket: cont_s3"
connection.delete_bucket("cont_s3")
except(boto.exception.S3ResponseError):
pass
print "Creating bucket: cont_s3"
bucket = connection.create_bucket("cont_s3")
key = boto.s3.key.Key(bucket, "uploaded_from_s3.txt")
fp = StringIO.StringIO()
fp.write('This was uploaded to swift from Boto.\n')
print "uploading: uploaded_from_s3.txt key"
key.set_contents_from_file(fp)
fp.close()
#Modify
bucket = connection.get_bucket("cont_s3")
for x in bucket.get_all_keys():
if x.name == "uploaded_from_s3.txt":
fp = StringIO.StringIO()
fp.write('This was modified/uploaded to swift from Boto.\n')
print "modifying: uploaded_from_s3.txt key"
key.set_contents_from_file(fp)
fp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment