123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- def main(args):
- bucket = args.bucket
- upload_dir = args.dir
- print("Uploading directory: %s" % (upload_dir))
- print("Uploading to bucket: %s" % (bucket))
- s3_client = boto3.client('s3', DEFAULT_REGION)
- transfer = S3Transfer(s3_client)
- for root, dirs, files in os.walk(upload_dir):
- for name in files:
- path = root.split(os.path.sep)[1:]
- path.append(name)
- key_id = os.path.join(*path)
- upload_file = os.path.join(root, name)
- if key_id == sys.argv[0]:
- if VERBOSE:
- print("Skipping upload of this uploader script")
- continue
- mime_type = mimetypes.guess_type(upload_file)[0]
- # print "mime_type: %s" % (mime_type)
- if mime_type:
- content_type = {'ContentType': mime_type}
- else:
- mime_type = "text/html"
- # upload normal files
- if VERBOSE:
- print("Uploading file: %s to %s:%s type: %s"
- % (upload_file, bucket, key_id, mime_type))
- transfer.upload_file(upload_file, bucket, key_id,
- extra_args=content_type)
- # for .html files, excluding index.html, also upload the file
- # without .html extension for pretty urls
- if (upload_file.endswith('.html') and not
- upload_file.endswith('index.html')):
- key_id_pretty_url = key_id.replace('.html', '')
- if VERBOSE:
- print("Uploading file WITHOUT HTML EXTENSION: "
- "%s to %s:%s type: %s"
- % (upload_file, bucket, key_id_pretty_url,
- mime_type))
- transfer.upload_file(upload_file, bucket, key_id_pretty_url,
- extra_args=content_type)
- print("Upload completed.")
|