cli_20.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. def put_object(bucket, key, path, content_type, silent, **boto_options):
  2. """
  3. Upload an object to an S3 bucket
  4. To upload a file to /my-key.txt in the my-bucket bucket:
  5. s3-credentials put-object my-bucket my-key.txt /path/to/file.txt
  6. Use - to upload content from standard input:
  7. echo "Hello" | s3-credentials put-object my-bucket hello.txt -
  8. """
  9. s3 = make_client("s3", **boto_options)
  10. size = None
  11. extra_args = {}
  12. if path == "-":
  13. # boto needs to be able to seek
  14. fp = io.BytesIO(sys.stdin.buffer.read())
  15. if not silent:
  16. size = fp.getbuffer().nbytes
  17. else:
  18. if not content_type:
  19. content_type = mimetypes.guess_type(path)[0]
  20. fp = click.open_file(path, "rb")
  21. if not silent:
  22. size = os.path.getsize(path)
  23. if content_type is not None:
  24. extra_args["ContentType"] = content_type
  25. if not silent:
  26. # Show progress bar
  27. with click.progressbar(length=size, label="Uploading") as bar:
  28. s3.upload_fileobj(
  29. fp, bucket, key, Callback=bar.update, ExtraArgs=extra_args
  30. )
  31. else:
  32. s3.upload_fileobj(fp, bucket, key, ExtraArgs=extra_args)