27 February 2017

You can save files to Amazon with Python using library called boto. Boto is a Python interface to Amazon Web Services and we need just a tiny part of it. If you have very large files you have to check into "multipart upload".

Source code viewer
  1. import os
  2.  
  3. from boto import s3
  4. from boto.s3.key import Key
  5. from boto.s3.connection import OrdinaryCallingFormat
  6.  
  7. # Directory for uploaded files.
  8. upload_dir = '/var/www/upload_dir'
  9.  
  10. # Amazon cloud connection.
  11. s3_connection = s3.connect_to_region(
  12. 'region',
  13. calling_format=OrdinaryCallingFormat(),
  14. aws_access_key_id='access_key',
  15. aws_secret_access_key='access_key_secret'
  16. )
  17. bucket = s3_connection.get_bucket('bucket_name')
  18.  
  19. # Save directory to Amazon cloud.
  20. for root, dirs, files in os.walk(upload_dir):
  21. for name in files:
  22. # Count the directory depth.
  23. dir_count = upload_dir.count('/') + 1
  24. # Get current directory.
  25. name_path = root.split(os.path.sep)[dir_count:]
  26. # Add name to current directory.
  27. name_path.append(name)
  28. # Join the directory list to string.
  29. name_path = os.path.join(*name_path)
  30. # Upload a file.
  31. k = Key(bucket=bucket, name=name_path)
  32. k.set_contents_from_filename(os.path.join(root, name))
Programming Language: Python