Created
April 25, 2025 07:45
-
-
Save max747/8537a5ad51f4ba3f355976126fc16577 to your computer and use it in GitHub Desktop.
put a file to S3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import argparse | |
| import logging | |
| import boto3 | |
| logger = logging.getLogger(__name__) | |
| def get_s3_resource(profile_name: str): | |
| if not profile_name: | |
| return boto3.resource("s3") | |
| session = boto3.Session(profile_name=profile_name) | |
| return session.resource("s3") | |
| def main(args: argparse.Namespace): | |
| s3 = get_s3_resource(args.profile) | |
| bucket = s3.Bucket(args.bucket) | |
| if args.folder: | |
| key = f"{args.folder}/{args.file}" | |
| else: | |
| key = args.file | |
| resp = bucket.upload_file(args.file, key) | |
| logger.info(resp) | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("file", help="File to upload") | |
| parser.add_argument("-b", "--bucket", help="S3 bucket name", required=True) | |
| parser.add_argument("-f", "--folder", help="S3 folder path") | |
| parser.add_argument("--profile", help="AWS profile name") | |
| return parser.parse_args() | |
| if __name__ == "__main__": | |
| args = parse_args() | |
| logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s") | |
| main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment