core_23.py 1.5 KB

123456789101112131415161718192021222324252627282930
  1. def upload_file(filepath, bucket_name=None, namekey=None, wait=False):
  2. """Uploads the given file to S3 storage.
  3. Arguments
  4. ---------
  5. filepath : str
  6. The full path, from root, to the desired file.
  7. bucket_name (optional) : str
  8. The name of the bucket to upload the file to. If not given, it will be
  9. inferred from any defined base directory that is present on the path
  10. (there is no guarentee which base directory will be used if several are
  11. present in the given path). If base directory inferrence fails the
  12. default bukcet will be used, if defined, else the operation will fail.
  13. namekey (optional) : bool
  14. Indicate whether to use the name of the file as the key when uploading
  15. to the bucket. If set, or if no base directory is found in the
  16. filepath, the file name will be used as key. Otherwise, the path
  17. rooted at the detected base directory will be used, resulting in a
  18. directory-like structure in the S3 bucket.
  19. wait (optional) : bool
  20. Defaults to False. If set to True, the function will wait on the upload
  21. operation. Otherwise, the upload will be performed asynchronously in a
  22. separate thread.
  23. """
  24. filepath = _parse_file_path(filepath)
  25. bucket, key = _get_bucket_and_key(filepath, bucket_name, namekey)
  26. if wait:
  27. bucket.upload_file(filepath, key)
  28. else:
  29. _get_executor().submit(_file_upload_thread, bucket, filepath, key)