amazon-s3-upload-static-site_1.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. def main(args):
  2. bucket = args.bucket
  3. upload_dir = args.dir
  4. print("Uploading directory: %s" % (upload_dir))
  5. print("Uploading to bucket: %s" % (bucket))
  6. s3_client = boto3.client('s3', DEFAULT_REGION)
  7. transfer = S3Transfer(s3_client)
  8. for root, dirs, files in os.walk(upload_dir):
  9. for name in files:
  10. path = root.split(os.path.sep)[1:]
  11. path.append(name)
  12. key_id = os.path.join(*path)
  13. upload_file = os.path.join(root, name)
  14. if key_id == sys.argv[0]:
  15. if VERBOSE:
  16. print("Skipping upload of this uploader script")
  17. continue
  18. mime_type = mimetypes.guess_type(upload_file)[0]
  19. # print "mime_type: %s" % (mime_type)
  20. if mime_type:
  21. content_type = {'ContentType': mime_type}
  22. else:
  23. mime_type = "text/html"
  24. # upload normal files
  25. if VERBOSE:
  26. print("Uploading file: %s to %s:%s type: %s"
  27. % (upload_file, bucket, key_id, mime_type))
  28. transfer.upload_file(upload_file, bucket, key_id,
  29. extra_args=content_type)
  30. # for .html files, excluding index.html, also upload the file
  31. # without .html extension for pretty urls
  32. if (upload_file.endswith('.html') and not
  33. upload_file.endswith('index.html')):
  34. key_id_pretty_url = key_id.replace('.html', '')
  35. if VERBOSE:
  36. print("Uploading file WITHOUT HTML EXTENSION: "
  37. "%s to %s:%s type: %s"
  38. % (upload_file, bucket, key_id_pretty_url,
  39. mime_type))
  40. transfer.upload_file(upload_file, bucket, key_id_pretty_url,
  41. extra_args=content_type)
  42. print("Upload completed.")