upload.py 943 B

123456789101112131415161718192021222324252627282930313233343536
  1. import boto
  2. from boto.s3.key import Key
  3. import urllib2, StringIO
  4. keyId = ""
  5. sKeyId= "/ZY0"
  6. fileName="abcd.txt"
  7. bucketName=""
  8. file = open(fileName)
  9. conn = boto.connect_s3(keyId,sKeyId)
  10. bucket = conn.get_bucket(bucketName)
  11. #Get the Key object of the bucket
  12. k = Key(bucket)
  13. #Crete a new key with id as the name of the file
  14. url = ""
  15. k.key=url.split('/')[::-1][0]
  16. import pdb;pdb.set_trace()
  17. file_object = urllib2.urlopen(url) # 'Like' a file object
  18. fp = StringIO.StringIO(file_object.read()) # Wrap object
  19. k.set_contents_from_file(fp)
  20. #Upload the file
  21. # result = k.set_contents_from_file(file)
  22. k.set_acl('public-read')
  23. url = k.generate_url(expires_in=0, query_auth=False)
  24. print (url)
  25. # import pdb;pdb.set_trace()
  26. #result contains the size of the file uploaded
  27. from zipfile import ZipFile
  28. zipfile = ZipFile(file_object)
  29. zipfile = ZipFile(fp)
  30. zipfile.namelist()
  31. k.set_contents_from_file(StringIO.StringIO(zipfile.read('2.jpg')))