s3.py 997 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import ConfigParser
  2. import boto
  3. import io
  4. from boto.s3.key import Key
  5. config = ConfigParser.ConfigParser()
  6. config.read("config.ini")
  7. AWS_ACCESS_KEY = config.get('S3','AccessKey')
  8. AWS_SECRET_ACCESS_KEY = config.get('S3','SecretKey')
  9. S3_BUCKET = config.get('S3','Bucket')
  10. def s3_upload(uploaded_file, id):
  11. s3conn = boto.connect_s3(AWS_ACCESS_KEY,AWS_SECRET_ACCESS_KEY)
  12. bucket = s3conn.get_bucket(S3_BUCKET)
  13. k = Key(bucket)
  14. k.key = 'id-' + str(id)
  15. k.content_type = uploaded_file.content_type
  16. if hasattr(uploaded_file,'temporary_file_path'):
  17. k.set_contents_from_filename(uploaded_file.temporary_file_path())
  18. else:
  19. k.set_contents_from_string(uploaded_file.read())
  20. k.set_canned_acl('public-read')
  21. return k.generate_url(expires_in=0, query_auth=False)
  22. def s3_delete(id):
  23. s3conn = boto.connect_s3(AWS_ACCESS_KEY,AWS_SECRET_ACCESS_KEY)
  24. bucket = s3conn.get_bucket(S3_BUCKET)
  25. k = Key(bucket)
  26. k.key = 'id-' + str(id)
  27. k.delete()