utils_7.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. def upload_file_to_s3(file_key, filename, key, do_gzip=False,
  2. do_expires=False, verbosity=0):
  3. """Details about params:
  4. * file_key is the relative path from media, e.g. media/folder/file.png
  5. * filename is the full path to the file, e.g.
  6. /var/www/site/media/folder/file.png
  7. """
  8. headers = {}
  9. content_type = guess_mimetype(filename)
  10. file_obj = open(filename, 'rb')
  11. if content_type:
  12. headers['Content-Type'] = content_type
  13. file_size = os.fstat(file_obj.fileno()).st_size
  14. filedata = file_obj.read()
  15. if do_gzip:
  16. # Gzipping only if file is large enough (>1K is recommended)
  17. # and only if file is a common text type (not a binary file)
  18. if file_size > 1024 and content_type in GZIP_CONTENT_TYPES:
  19. filedata = compress_string(filedata)
  20. headers['Content-Encoding'] = 'gzip'
  21. gzip_file_size = len(filedata)
  22. if verbosity > 1:
  23. print ("\tgzipped: %dk to %dk" % \
  24. (file_size / 1024, gzip_file_size / 1024))
  25. file_size = gzip_file_size
  26. headers['Content-Length'] = str(file_size)
  27. if do_expires:
  28. # HTTP/1.0
  29. headers['Expires'] = '%s GMT' % (email.Utils.formatdate(
  30. time.mktime((datetime.datetime.now() +
  31. datetime.timedelta(days=365 * 2)).timetuple())))
  32. # HTTP/1.1
  33. headers['Cache-Control'] = 'max-age %d' % (3600 * 24 * 365 * 2)
  34. try:
  35. key.name = file_key
  36. key.set_contents_from_string(filedata, headers, replace=True)
  37. key.set_acl('public-read')
  38. finally:
  39. file_obj.close()