Last active
July 20, 2022 21:39
-
-
Save saulcosta/ec1bc5c9db6fff962d6a to your computer and use it in GitHub Desktop.
A quick Python script for uploading an archive file to an AWS Glacier storage vault.
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
''' | |
A quick Python uploader script for pushing files to AWS Glacier storage. | |
Assumptions: | |
1) You have boto installed (eg. with pip) | |
2) You have AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY set as environment variables | |
Based (very loosely) on some code found here: | |
http://forrestbao.blogspot.com/2013/06/amazon-glacier-in-python-with-boto-part.html | |
''' | |
import __main__ | |
import argparse | |
import os.path | |
import sys | |
from datetime import datetime | |
from boto.glacier.concurrent import ConcurrentUploader | |
from boto.glacier.layer1 import Layer1 | |
def upload_archive(archive, description, vault): | |
''' | |
Upload the archive. | |
@archive: absolute path to archive file to upload | |
@description: description for the archive file you want to upload | |
@vault: vault you want to upload the archive to | |
''' | |
# Make sure the archive exists | |
if not os.path.isfile(archive): | |
raise ValueError('%s does not exist!') | |
# Try to load the environment variables | |
try: | |
aws_access_key_id = os.environ['AWS_ACCESS_KEY_ID'] | |
aws_secret_access_key = os.environ['AWS_SECRET_ACCESS_KEY'] | |
except KeyError as ex: | |
raise ValueError('%s environment variable is not set!' % ex) | |
# Upload the archive | |
print('Started upload for %s to %s at %s.' % (archive, vault, datetime.utcnow())) | |
glacier_layer = Layer1( | |
aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) | |
uploader = ConcurrentUploader(glacier_layer, vault, part_size=128*1024*1024, num_threads=4) | |
archive_id = uploader.upload(archive, description) | |
print('%s was uploaded to %s (archive ID: %s)' % (archive, vault, archive_id)) | |
def main(): | |
arg_parser = argparse.ArgumentParser(description=__main__.__doc__) | |
arg_parser.add_argument( | |
'-a', dest='archive', required=True, help='Absolute path for archive file to upload.') | |
arg_parser.add_argument( | |
'-d', dest='description', required=True, help='Description for archive file.') | |
arg_parser.add_argument( | |
'-v', dest='vault', required=True, help='Vault to store the archive in.') | |
parsed_args = arg_parser.parse_args() | |
upload_archive(parsed_args.archive, parsed_args.description, parsed_args.vault) | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment