cdm_data_loaders.utils.file_transfer.s3.object_utils ¶
Utilities for s3 interaction.
split_s3_path ¶
Convert a full s3 path (including bucket) into a bucket and key pair.
Returns a tuple of bucket, key
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s3_path
|
str
|
an s3 path, including the bucket name ( |
required |
allow_bucket_only
|
bool
|
Allow parsing of a path that only includes a bucket (no key) |
False
|
Returns:
| Type | Description |
|---|---|
tuple[str, str | None]
|
tuple of (bucket, key) |
Source code in src/cdm_data_loaders/utils/file_transfer/s3/object_utils.py
list_objects ¶
list_objects(
s3_path: str,
*,
max_keys: int = 1000,
allow_bucket_only: bool = False
) -> list[dict[str, Any]]
List the remote paths that start with s3_path.
Note: since s3 paths are basically cosmetic, this function returns all paths that start with
s3_path minus the bucket name.
Retrieves all objects under the given prefix; collects all pages of results if there are more than
1000 files (the max retrievable per list_objects_v2 query) present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s3_path
|
str
|
directory to be listed, including the bucket name |
required |
max_keys
|
int
|
maximum number of keys to return. boto3 defaults to 1000 records max. |
1000
|
allow_bucket_only
|
bool
|
allow listing an entire bucket when |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
list of object metadata dicts in the directory |
Source code in src/cdm_data_loaders/utils/file_transfer/s3/object_utils.py
head_object ¶
Check whether an object exists on s3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s3_path
|
str
|
path to the object on s3, including the bucket name |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
response from the head_object request |
Source code in src/cdm_data_loaders/utils/file_transfer/s3/object_utils.py
get_existing_object_info ¶
Fetch metadata for an object already in S3, if it exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s3_path
|
str
|
path to the object on s3, including the bucket name |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
metadata for the existing object, or None if no object exists at |
Raises:
| Type | Description |
|---|---|
ClientError
|
for any S3 error other than "object not found" |
Source code in src/cdm_data_loaders/utils/file_transfer/s3/object_utils.py
object_exists ¶
Check whether an object exists on s3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s3_path
|
str
|
path to the object on s3, including the bucket name |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the object exists, False otherwise |
Source code in src/cdm_data_loaders/utils/file_transfer/s3/object_utils.py
upload_fileobj ¶
upload_fileobj(
fileobj: IO[bytes],
s3_path: str,
*,
file_path: Path | None = None,
file_size: int | None = 0,
user_metadata: dict[str, str] | None = None,
transfer_config_kwargs: dict[str, Any] | None = None,
skip_if_exists: bool = False,
show_progress: bool = True
) -> bool
Upload data from a binary file-like object to S3.
Can be used for file uploads via the :func:upload_file interface
or directly for binary data, e.g. data streamed via FTP/HTTP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fileobj
|
IO[bytes]
|
a binary-mode file-like object supporting |
required |
s3_path
|
str
|
s3 destination path, including the bucket name |
required |
file_path
|
Path | None
|
if the input is a file, a Path object for the file (used for calculating checksums, size, etc.) |
None
|
file_size
|
int | None
|
size of the file in bytes |
0
|
user_metadata
|
dict[str, str] | None
|
user metadata key/value pairs to attach to the object |
None
|
transfer_config_kwargs
|
dict[str, Any] | None
|
keyword arguments for configuring the S3 transfer |
None
|
skip_if_exists
|
bool
|
whether to skip the upload if the object already exists |
False
|
show_progress
|
bool
|
whether to display a tqdm progress bar during upload |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the upload succeeded, else False |
Source code in src/cdm_data_loaders/utils/file_transfer/s3/object_utils.py
upload_file ¶
upload_file(
local_file_path: Path | str,
destination_dir: str,
object_name: str | None = None,
*,
user_metadata: dict[str, str] | None = None,
transfer_config_kwargs: dict[str, Any] | None = None,
skip_if_exists: bool = False,
show_progress: bool = True
) -> bool
Upload a local file to an S3 bucket.
Internally opens the file and delegates to :func:upload_fileobj.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_file_path
|
Path | str
|
File to upload |
required |
destination_dir
|
str
|
path to the destination directory on s3, including the bucket name and EXCLUDING the file name |
required |
object_name
|
str | None
|
S3 object name. If not specified, the name of the file from local_file_path is used. |
None
|
user_metadata
|
dict[str, str] | None
|
user metadata key/value pairs to attach to the object; when provided the upload always runs |
None
|
transfer_config_kwargs
|
dict[str, Any] | None
|
keyword arguments for configuring the S3 transfer |
None
|
skip_if_exists
|
bool
|
whether to skip the upload if the object already exists; not yet implemented |
False
|
show_progress
|
bool
|
whether to display a tqdm progress bar during upload, defaults to True |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if file was uploaded, else False |
Source code in src/cdm_data_loaders/utils/file_transfer/s3/object_utils.py
upload_dir_files ¶
upload_dir_files(
local_dir: Path | str,
destination_dir: str,
file_glob: str | None = None,
*,
show_progress: bool = True,
delete_source: bool = False,
upload_fn: Callable[..., bool] | None = None
) -> tuple[int, bool]
Upload files under local_dir to destination_dir and return (uploaded_count, all_successful).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_dir
|
Path | str
|
local directory to upload |
required |
destination_dir
|
str
|
remote directory to upload to, including the bucket name |
required |
file_glob
|
str | None
|
glob for selecting files to upload (defaults to |
None
|
show_progress
|
bool
|
whether to display a tqdm progress bar during each upload |
True
|
delete_source
|
bool
|
whether to delete each file after uploading and remove |
False
|
upload_fn
|
Callable[..., bool] | None
|
callable used to upload each file (defaults to :func: |
None
|
Returns:
| Type | Description |
|---|---|
tuple[int, bool]
|
tuple of |
Source code in src/cdm_data_loaders/utils/file_transfer/s3/object_utils.py
upload_dir ¶
upload_dir(
local_dir: Path | str,
destination_dir: str,
file_glob: str | None = None,
*,
show_progress: bool = True,
delete_source: bool = False
) -> bool
Upload a directory to an s3 bucket.
If file_glob is not set, it will default to "**/*", i.e. any path with at least
one segment (recursive listing of all paths from the current directory).
Wildcards:
** (entire segment)
Matches any number of file or directory segments, including zero.
"assets/**" matches any path starting with "assets/", including "assets/"
* (entire segment)
Matches one file or directory segment.
"assets/*" matches any file or directory under "assets/" but none of the children
**/* (two segments)
Matches any path with at least one segment
"assets/**/*" matches any file or directory under "assets/" but not "assets/" itself
* (part of a segment)
Matches any number of non-separator characters, including zero.
"file*.txt" would match "file.txt", "file_type.txt", "file12345.txt", but not "file/b.txt"
? (part of a segment)
Matches one non-separator character.
"file?.txt" would match "filea.txt" or "file1.txt"
[seq] Matches one character in seq, where seq is a sequence of characters. Range expressions are supported; for example, [a-z] matches any lowercase ASCII letter. Multiple ranges can be combined: [a-zA-Z0-9_] matches any ASCII letter, digit, or underscore.
[!seq] Matches one character not in seq, where seq follows the same rules as above.
For a literal match, wrap the meta-characters in brackets. For example, "[?]" matches the character "?".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_dir
|
Path | str
|
local directory to upload |
required |
destination_dir
|
str
|
remote directory to upload to, including the bucket name |
required |
file_glob
|
str | None
|
glob for selecting files to upload |
None
|
show_progress
|
bool
|
whether to display a tqdm progress bar during each upload |
True
|
delete_source
|
bool
|
whether to delete each file after uploading and remove |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
True or False, depending on the result of the uploads |
Source code in src/cdm_data_loaders/utils/file_transfer/s3/object_utils.py
download_file ¶
download_file(
s3_path: str,
local_file_path: str | Path,
transfer_config_kwargs: dict[str, Any] | None = None,
show_progress: bool = True,
) -> None
Download an object from s3.
WARNING: will overwrite existing files but will not overwrite a file whilst trying to make a directory
Will attempt to create the local directory if it does not exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s3_path
|
str
|
path to the file on s3, including the bucket name |
required |
local_file_path
|
str | Path
|
local path (including file name) to save the downloaded file to |
required |
transfer_config_kwargs
|
dict[str, Any] | None
|
keyword arguments for configuring the S3 transfer |
None
|
show_progress
|
bool
|
whether to display a tqdm progress bar during upload, defaults to True |
True
|
Source code in src/cdm_data_loaders/utils/file_transfer/s3/object_utils.py
copy_object ¶
Copy an object from one place to another, inheriting the source user metadata.
Source user metadata (e.g. md5) is preserved on the destination because
MetadataDirective is omitted, which defaults to COPY.
A successful copy operation will return a response where
resp["ResponseMetadata"]["HTTPStatusCode"] == 200.
Errors (e.g, buckets or keys not existing, wrong credentials, etc.) are passed directly to the user without being caught.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_s3_path
|
str
|
path to the file on s3, including the bucket name |
required |
new_s3_path
|
str
|
the desired new file path on s3, including the bucket name |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dictionary containing response from the copy operation |
Source code in src/cdm_data_loaders/utils/file_transfer/s3/object_utils.py
copy_directory ¶
Copy all objects under a given S3 prefix to a new prefix.
Preserves the relative key structure under the source prefix. For example, copying s3://my-bucket/foo/ to s3://my-bucket/bar/ will copy s3://my-bucket/foo/a/b.txt -> s3://my-bucket/bar/a/b.txt
If the source bucket does not exist, a NoSuchBucket error will be thrown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_s3_path
|
str
|
path to the directory on s3, including the bucket name |
required |
new_s3_path
|
str
|
the desired new directory path on s3, including the bucket name |
required |
Returns:
| Type | Description |
|---|---|
tuple[dict[str, str], dict[str, Any]]
|
a tuple of (successes, errors) where: - successes maps "bucket/source_key" -> "bucket/dest_key" for each successfully copied object - errors maps "bucket/source_key" -> the exception or response object for each failed copy |
Source code in src/cdm_data_loaders/utils/file_transfer/s3/object_utils.py
delete_object ¶
Delete an object from s3.
A successful deletion will return a response where
resp["ResponseMetadata"]["HTTPStatusCode"] == 204.
Errors (e.g, buckets or keys not existing, wrong credentials, etc.) are passed directly to the user without being caught.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s3_path
|
str
|
path to the file on s3, including the bucket name |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dictionary containing response |
Source code in src/cdm_data_loaders/utils/file_transfer/s3/object_utils.py
delete_objects ¶
Delete multiple objects from an S3 bucket in a single API call.
Splits into batches of 1000 (the S3 API maximum per request).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket
|
str
|
S3 bucket name (no protocol prefix) |
required |
keys
|
list[str]
|
list of S3 keys to delete |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
list of per-key error dicts returned by S3 (empty if all succeeded) |