def put( bucket: s3.Bucket, source: pathlib.Path | bytes, target: pathlib.Path, # directory *, name: str = "", cache: bool = False, content_type: str = "", ) -> s3.Object: ctx: ContextManager[Any] if isinstance(source, pathlib.Path): ctx = open(source, "rb") name = name or source.name elif not name: raise ValueError(f"Name not given for target {target}") else: ctx = contextlib.nullcontext(source) if not content_type: ct, _ = mimetypes.guess_type(name) if ct is not None and "/" in ct: content_type = ct print("put", name, bucket, target) with ctx as body: result = bucket.put_object( Key=str(target / name), Body=body, CacheControl=CACHE if cache else NO_CACHE, ContentType=content_type, ACL="public-read", ) print(result) return result
def upload_png_from_dir(bucket: Bucket, png_files_dir: Path) -> None: for p in sorted(png_files_dir.glob('*.png')): with p.open(mode='rb') as f: bucket.put_object(Key=f'custom_label/{p.parent}/{p.name}', Body=f.read())