botouploader.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # encoding: utf-8
  2. import logging
  3. import os.path
  4. import sys
  5. from boto.exception import S3CreateError
  6. from boto.s3.connection import OrdinaryCallingFormat
  7. from boto.s3.key import Key
  8. import boto
  9. from terroroftinytown.release.baseuploader import BaseUploaderBootstrap
  10. logger = logging.getLogger(__name__)
  11. class BotoUploaderBootstrap(BaseUploaderBootstrap):
  12. def upload(self):
  13. self.setup_boto()
  14. logger.info('Begin upload %s %s.', self.identifier, self.filenames)
  15. for filename in self.filenames:
  16. key = Key(self.bucket)
  17. key.key = os.path.basename(filename)
  18. def progress(done, todo):
  19. sys.stdout.write('%s %d/%d\r' % (filename, done, todo))
  20. sys.stdout.flush()
  21. key.set_contents_from_filename(filename, cb=progress)
  22. logger.info('Done upload.')
  23. def setup_boto(self):
  24. self.boto = boto.connect_s3(
  25. self.access_key,
  26. self.secret_key,
  27. host=self.config.get('iaexporter', 'endpoint'),
  28. is_secure=False,
  29. calling_format=OrdinaryCallingFormat()
  30. )
  31. bucket_name = self.identifier
  32. try:
  33. self.bucket = self.boto.create_bucket(bucket_name, {
  34. 'x-archive-meta-title': self.title.encode('utf-8'),
  35. 'x-archive-meta-collection': self.collection,
  36. 'x-archive-meta-mediatype': 'software',
  37. 'x-archive-meta-subject':
  38. self.subject.encode('utf-8'),
  39. 'x-archive-meta-description': self.description.encode('utf-8'),
  40. 'x-archive-ignore-preexisting-bucket': '1'
  41. })
  42. except S3CreateError:
  43. self.bucket = self.boto.get_bucket(bucket_name)
  44. if __name__ == '__main__':
  45. BotoUploaderBootstrap().start()