Skip to content

Client

AsyncS3Client

Bases: S3Client

Async Client for interacting with s3 based on boto3 client.

Source code in saritasa_s3_tools/async_client/client.py
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class AsyncS3Client(client.S3Client):
    """Async Client for interacting with s3 based on boto3 client."""

    async def run_sync_as_async(
        self,
        func: collections.abc.Callable[ParamT, ReturnT],
        *args: ParamT.args,
        **kwargs: ParamT.kwargs,
    ) -> ReturnT:
        """Make sync function run in async env."""
        return await anyio.to_thread.run_sync(  # type: ignore
            functools.partial(func, *args, **kwargs),
        )

    async def async_generate_params(
        self,
        filename: str,
        config: configs.S3FileTypeConfig,
        content_type: str,
        bucket: str = "",
        upload_folder: str = "",
        extra_metadata: dict[str, str] | None = None,
    ) -> client.S3UploadParams:
        """Generate params for s3 upload in async env."""
        return await self.run_sync_as_async(
            self.generate_params,
            filename=filename,
            upload_folder=upload_folder,
            config=config,
            bucket=bucket,
            content_type=content_type,
            extra_metadata=extra_metadata,
        )

    async def async_upload_file(
        self,
        filename: str,
        config: configs.S3FileTypeConfig,
        file_obj: mypy_boto3_s3.type_defs.FileobjTypeDef,
        bucket: str = "",
    ) -> str:
        """Upload file to s3 in async env."""
        return await self.run_sync_as_async(
            self.upload_file,
            filename=filename,
            config=config,
            bucket=bucket,
            file_obj=file_obj,
        )

    async def async_download_file(
        self,
        key: str,
        file_obj: mypy_boto3_s3.type_defs.FileobjTypeDef,
        bucket: str = "",
    ) -> mypy_boto3_s3.type_defs.FileobjTypeDef:
        """Download file from s3 in async env."""
        return await self.run_sync_as_async(
            self.download_file,
            file_obj=file_obj,
            bucket=bucket,
            key=key,
        )

    async def async_get_file_metadata(
        self,
        key: str,
        bucket: str = "",
    ) -> mypy_boto3_s3.type_defs.HeadObjectOutputTypeDef:
        """Get file's metadata in async env."""
        return await self.run_sync_as_async(
            self.get_file_metadata,
            bucket=bucket,
            key=key,
        )

    async def async_is_file_in_bucket(
        self,
        key: str,
        bucket: str = "",
    ) -> bool:
        """Check if file is in bucket in async env."""
        return await self.run_sync_as_async(
            self.is_file_in_bucket,
            bucket=bucket,
            key=key,
        )

    async def async_copy_object(
        self,
        key: str,
        source_key: str,
        bucket: str = "",
        source_bucket: str = "",
    ) -> None:
        """Copy file object from copy source to key path in async env."""
        return await self.run_sync_as_async(
            self.copy_object,
            key=key,
            source_key=source_key,
            bucket=bucket,
            source_bucket=source_bucket,
        )

    async def async_delete_object(
        self,
        key: str,
        bucket: str = "",
    ) -> None:
        """Delete file object from s3 bucket is async env."""
        return await self.run_sync_as_async(
            self.delete_object,
            key=key,
            bucket=bucket,
        )

async_copy_object(key, source_key, bucket='', source_bucket='') async

Copy file object from copy source to key path in async env.

Source code in saritasa_s3_tools/async_client/client.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
async def async_copy_object(
    self,
    key: str,
    source_key: str,
    bucket: str = "",
    source_bucket: str = "",
) -> None:
    """Copy file object from copy source to key path in async env."""
    return await self.run_sync_as_async(
        self.copy_object,
        key=key,
        source_key=source_key,
        bucket=bucket,
        source_bucket=source_bucket,
    )

async_delete_object(key, bucket='') async

Delete file object from s3 bucket is async env.

Source code in saritasa_s3_tools/async_client/client.py
119
120
121
122
123
124
125
126
127
128
129
async def async_delete_object(
    self,
    key: str,
    bucket: str = "",
) -> None:
    """Delete file object from s3 bucket is async env."""
    return await self.run_sync_as_async(
        self.delete_object,
        key=key,
        bucket=bucket,
    )

async_download_file(key, file_obj, bucket='') async

Download file from s3 in async env.

Source code in saritasa_s3_tools/async_client/client.py
65
66
67
68
69
70
71
72
73
74
75
76
77
async def async_download_file(
    self,
    key: str,
    file_obj: mypy_boto3_s3.type_defs.FileobjTypeDef,
    bucket: str = "",
) -> mypy_boto3_s3.type_defs.FileobjTypeDef:
    """Download file from s3 in async env."""
    return await self.run_sync_as_async(
        self.download_file,
        file_obj=file_obj,
        bucket=bucket,
        key=key,
    )

async_generate_params(filename, config, content_type, bucket='', upload_folder='', extra_metadata=None) async

Generate params for s3 upload in async env.

Source code in saritasa_s3_tools/async_client/client.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
async def async_generate_params(
    self,
    filename: str,
    config: configs.S3FileTypeConfig,
    content_type: str,
    bucket: str = "",
    upload_folder: str = "",
    extra_metadata: dict[str, str] | None = None,
) -> client.S3UploadParams:
    """Generate params for s3 upload in async env."""
    return await self.run_sync_as_async(
        self.generate_params,
        filename=filename,
        upload_folder=upload_folder,
        config=config,
        bucket=bucket,
        content_type=content_type,
        extra_metadata=extra_metadata,
    )

async_get_file_metadata(key, bucket='') async

Get file's metadata in async env.

Source code in saritasa_s3_tools/async_client/client.py
79
80
81
82
83
84
85
86
87
88
89
async def async_get_file_metadata(
    self,
    key: str,
    bucket: str = "",
) -> mypy_boto3_s3.type_defs.HeadObjectOutputTypeDef:
    """Get file's metadata in async env."""
    return await self.run_sync_as_async(
        self.get_file_metadata,
        bucket=bucket,
        key=key,
    )

async_is_file_in_bucket(key, bucket='') async

Check if file is in bucket in async env.

Source code in saritasa_s3_tools/async_client/client.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
async def async_is_file_in_bucket(
    self,
    key: str,
    bucket: str = "",
) -> bool:
    """Check if file is in bucket in async env."""
    return await self.run_sync_as_async(
        self.is_file_in_bucket,
        bucket=bucket,
        key=key,
    )

async_upload_file(filename, config, file_obj, bucket='') async

Upload file to s3 in async env.

Source code in saritasa_s3_tools/async_client/client.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
async def async_upload_file(
    self,
    filename: str,
    config: configs.S3FileTypeConfig,
    file_obj: mypy_boto3_s3.type_defs.FileobjTypeDef,
    bucket: str = "",
) -> str:
    """Upload file to s3 in async env."""
    return await self.run_sync_as_async(
        self.upload_file,
        filename=filename,
        config=config,
        bucket=bucket,
        file_obj=file_obj,
    )

run_sync_as_async(func, *args, **kwargs) async

Make sync function run in async env.

Source code in saritasa_s3_tools/async_client/client.py
18
19
20
21
22
23
24
25
26
27
async def run_sync_as_async(
    self,
    func: collections.abc.Callable[ParamT, ReturnT],
    *args: ParamT.args,
    **kwargs: ParamT.kwargs,
) -> ReturnT:
    """Make sync function run in async env."""
    return await anyio.to_thread.run_sync(  # type: ignore
        functools.partial(func, *args, **kwargs),
    )