Last active
May 17, 2016 12:54
-
-
Save makeittotop/50ae17b4b876b90f4070 to your computer and use it in GitHub Desktop.
uploading a file to aws s3 using the Python boto API.
This file contains 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 python | |
import sys, os, glob | |
# amazon web services api | |
import boto | |
AWS_ACCESS_KEY_ID='****' | |
AWS_SECRET_ACCESS_KEY='****' | |
def main(): | |
# Get all files matching a pattern | |
dump_file_glob = "/tmp/inventory_db*" | |
# Grab the latest off 'em | |
newest_dump_file = max(glob.iglob(dump_file_glob), key=os.path.getctime) | |
dump_file_basename = os.path.basename(newest_dump_file) | |
# Form a connection with the s3 | |
conn = boto.connect_s3(aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY) | |
# Get the relevant bucket | |
bucket = conn.get_bucket('com.amazon.s3.abhishekpareek1983.barajoun.inventory_db') | |
# Make a new file | |
k = boto.s3.key.Key(bucket) | |
k.key = os.path.basename(dump_file_basename) | |
# Set the contents to the source file | |
k.set_contents_from_filename(newest_dump_file, replace=False) | |
print >>sys.stderr, "File {0} successfully uploaded to s3!".format(newest_dump_file) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment