123456789101112131415161718192021222324252627282930313233343536 |
- def put_object(bucket, key, path, content_type, silent, **boto_options):
- """
- Upload an object to an S3 bucket
- To upload a file to /my-key.txt in the my-bucket bucket:
- s3-credentials put-object my-bucket my-key.txt /path/to/file.txt
- Use - to upload content from standard input:
- echo "Hello" | s3-credentials put-object my-bucket hello.txt -
- """
- s3 = make_client("s3", **boto_options)
- size = None
- extra_args = {}
- if path == "-":
- # boto needs to be able to seek
- fp = io.BytesIO(sys.stdin.buffer.read())
- if not silent:
- size = fp.getbuffer().nbytes
- else:
- if not content_type:
- content_type = mimetypes.guess_type(path)[0]
- fp = click.open_file(path, "rb")
- if not silent:
- size = os.path.getsize(path)
- if content_type is not None:
- extra_args["ContentType"] = content_type
- if not silent:
- # Show progress bar
- with click.progressbar(length=size, label="Uploading") as bar:
- s3.upload_fileobj(
- fp, bucket, key, Callback=bar.update, ExtraArgs=extra_args
- )
- else:
- s3.upload_fileobj(fp, bucket, key, ExtraArgs=extra_args)
|