问题
I'm new to Python, I got a small script to upload files to S3, at the moment I only hard-code one single file in the script, the bucket name is also hard-coded.
I wanted to merge argparse in this script so that I can add some arguements by myself and upload different files. For example, in the command line I can specify arguments to decide file_name x
upload to bucket_name xxx
.
I've been reading documents about how to set argparse but I can only make small changes and don't know how to implement it with the function in my script (I guess os.rename
will be unnecessary because we'll parse the arguements by ourselves). I know the logic, just having difficulty to implement them in the actual code... Can someone gave me an example or gave me some hint, many thanks.
回答1:
Here is how the script would look taking command line arguments.
import argparse
import datetime
import logging
import os
import boto3
def make_new_key(filename: str):
current_date = datetime.datetime.today().strftime('%Y-%m-%d_%H_%M_%S')
# The next line handles the case where you are passing the
# full path to the file as opposed to just the name
name = os.path.basename(filename)
parts = name.split('.')
new_name = f"{parts[0]}{current_date}.csv"
return new_name
def upload_to_s3(source_filename: str, new_name: str, bucket: str):
logging.info(f"Uploading to S3 from {source_filename} to {bucket} {key}")
s3_client = boto3.client("s3")
with open(source_filename, 'rb') as file:
response = s3_client.put_object(Body=file,
Bucket=bucket,
Key=new_name,
ACL="bucket-owner-full-control")
logging.info(response)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--filename')
parser.add_argument('--bucket')
args = parser.parse_args()
new_name = make_new_key(args.filename)
upload_to_s3(args.filename, new_name, args.bucket)
Then you would call the script like this
python upload.py --filename path/to/file --bucket name_of_bucket
来源:https://stackoverflow.com/questions/61291816/how-to-implement-argparse-in-python