Skip to content

Instantly share code, notes, and snippets.

@stewmi
Created May 27, 2020 21:30
Show Gist options
  • Save stewmi/5b697bd1b5c0261d9db16ddb5b142b16 to your computer and use it in GitHub Desktop.
Save stewmi/5b697bd1b5c0261d9db16ddb5b142b16 to your computer and use it in GitHub Desktop.
An Example of wrapping s3 operations with a CLI utility
import argparse
import boto3
s3 = boto3.client('s3')
def get_file(bucket, file):
s3.download_file(bucket, file, file)
return
def delete_file(bucket, file):
if input("Are you sure you want to delete {}?".format(file)).lower() == "yes":
try:
s3.delete_object(Bucket=bucket, Key=file)
except Exception as e:
print("File Not Found")
else:
return
def main():
parser = argparse.ArgumentParser(description="Build Metadata files.")
parser.add_argument('--operation', required=True)
parser.add_argument('--bucket', required=True)
parser.add_argument('--key', required=True)
args = parser.parse_args()
if args.operation == "delete":
delete_file(args.bucket, args.key)
elif args.operation == "get":
get_file(args.bucket, args.key)
else:
print("Operation not supported. try delete or get.")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment