Upload a local file to S3. Uploads a file from the local filesystem to an S3 location. Supports automatic content type detection based on file extension and provides progress callback functionality. Args: lpath: Local file path to upload. rpa
(self, lpath: str, rpath: str, callback=_DEFAULT_CALLBACK, **kwargs)
| 1273 | )[1] |
| 1274 | |
| 1275 | def put_file(self, lpath: str, rpath: str, callback=_DEFAULT_CALLBACK, **kwargs): |
| 1276 | """Upload a local file to S3. |
| 1277 | |
| 1278 | Uploads a file from the local filesystem to an S3 location. Supports |
| 1279 | automatic content type detection based on file extension and provides |
| 1280 | progress callback functionality. |
| 1281 | |
| 1282 | Args: |
| 1283 | lpath: Local file path to upload. |
| 1284 | rpath: S3 destination path (s3://bucket/key). |
| 1285 | callback: Progress callback for tracking upload progress. |
| 1286 | **kwargs: Additional S3 parameters (e.g., ContentType, StorageClass). |
| 1287 | |
| 1288 | Note: |
| 1289 | Directories are not supported for upload. If lpath is a directory, |
| 1290 | the method returns without performing any operation. Bucket-only |
| 1291 | destinations (without key) are also not supported. |
| 1292 | """ |
| 1293 | if os.path.isdir(lpath): |
| 1294 | # No support for directory uploads. |
| 1295 | return |
| 1296 | |
| 1297 | bucket, key, _ = self.parse_path(rpath) |
| 1298 | if not key: |
| 1299 | # No support for bucket copy. |
| 1300 | return |
| 1301 | |
| 1302 | size = os.path.getsize(lpath) |
| 1303 | callback.set_size(size) |
| 1304 | if "ContentType" not in kwargs: |
| 1305 | content_type, _ = mimetypes.guess_type(lpath) |
| 1306 | if content_type is not None: |
| 1307 | kwargs["ContentType"] = content_type |
| 1308 | |
| 1309 | with ( |
| 1310 | self.open(rpath, "wb", s3_additional_kwargs=kwargs) as remote, |
| 1311 | open(lpath, "rb") as local, |
| 1312 | ): |
| 1313 | while data := local.read(remote.blocksize): |
| 1314 | remote.write(data) |
| 1315 | callback.relative_update(len(data)) |
| 1316 | |
| 1317 | self.invalidate_cache(rpath) |
| 1318 | |
| 1319 | def get_file(self, rpath: str, lpath: str, callback=_DEFAULT_CALLBACK, outfile=None, **kwargs): |
| 1320 | """Download an S3 file to local filesystem. |