Skip to content

Instantly share code, notes, and snippets.

@toast38coza
Created April 6, 2011 19:41
Show Gist options
  • Save toast38coza/906363 to your computer and use it in GitHub Desktop.
Save toast38coza/906363 to your computer and use it in GitHub Desktop.
Basic wrapper round boto to create an s3 key from a string
import boto
from boto.s3.key import Key
from django.conf import settings
#settings:
"""
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
DEFAULT_BUCKET
usage:
s3 = S3()
s3.write_string_to_s3('/path/to/file.txt','string for file')
"""
class S3:
def s3_connect(self):
return boto.connect_s3(aws_access_key_id=settings.AWS_ACCESS_KEY_ID, aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY)
def write_string_to_s3(self,path,string,policy='public-read'):
conn = self.s3_connect()
bucket = conn.get_bucket(settings.DEFAULT_BUCKET)
key = Key(bucket)
key.key = path
key.set_contents_from_string(string,policy=policy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment