migrate.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # -*- coding: utf-8 -*-
  2. import os
  3. from boto.s3.connection import S3Connection
  4. from boto.s3.key import Key
  5. connection = S3Connection(
  6. host = 's3.amazonaws.com', # S3 Compatible Services
  7. is_secure = True,
  8. aws_access_key_id = 'access_key_id', # Add your access key
  9. aws_secret_access_key = 'secret_access_key' # Add your secret key
  10. )
  11. bucket = connection.get_bucket('bucket_name', validate = True)
  12. COMMON_PATH = '/common_folder/' # COMMON PATH OF YOUR S3 AND YOUR SERVER
  13. BASE = os.path.dirname(os.path.abspath(__file__))
  14. def upload(path, filename):
  15. path_file = '%s/%s'%(BASE, filename)
  16. if COMMON_PATH in path_file:
  17. path_upload = ROOT_PATH + path_file.rsplit(COMMON_PATH, 1)[1]
  18. print (' Upload to : %s' % path_upload)
  19. key = Key(bucket, path_upload)
  20. key.set_contents_from_filename(path_file)
  21. else:
  22. print (' Upload path not found.')
  23. if __name__ == '__main__':
  24. count = 1
  25. for path, subdirs, files in os.walk('.'):
  26. for name in files:
  27. if name not in os.path.basename(__file__):
  28. print ('> Execute File (%s/%s) : %s '% (count, len(files)-1, os.path.join(path, name)[1:]))
  29. upload(path, name)
  30. count += 1