1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # Copyright (c) 2012 Miguel Olivares http://moliware.com/ 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining a 6 # copy of this software and associated documentation files (the 7 # "Software"), to deal in the Software without restriction, including 8 # without limitation the rights to use, copy, modify, merge, publish, dis- 9 # tribute, sublicense, and/or sell copies of the Software, and to permit 10 # persons to whom the Software is furnished to do so, subject to the fol- 11 # lowing conditions: 12 # 13 # The above copyright notice and this permission notice shall be included 14 # in all copies or substantial portions of the Software. 15 # 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- 18 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 19 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 # IN THE SOFTWARE. 23 # 24 """ 25 glacier 26 ~~~~~~~ 27 28 Amazon Glacier tool built on top of boto. Look at the usage method to see 29 how to use it. 30 31 Author: Miguel Olivares <miguel (at] moliware.com> 32 """ 33 import sys 34 35 from boto.glacier import connect_to_region 36 from getopt import getopt, GetoptError 37 from os.path import isfile, basename 38 39 40 COMMANDS = ('vaults', 'jobs', 'upload') 41 42 43 def usage(): 44 print(""" 45 glacier <command> [args] 46 47 Commands 48 vaults - Operations with vaults 49 jobs - Operations with jobs 50 upload - Upload files to a vault. If the vault doesn't exits, it is 51 created 52 53 Common args: 54 --access_key - Your AWS Access Key ID. If not supplied, boto will 55 use the value of the environment variable 56 AWS_ACCESS_KEY_ID 57 --secret_key - Your AWS Secret Access Key. If not supplied, boto 58 will use the value of the environment variable 59 AWS_SECRET_ACCESS_KEY 60 --region - AWS region to use. Possible values: us-east-1, us-west-1, 61 us-west-2, ap-northeast-1, eu-west-1. 62 Default: us-east-1 63 64 Vaults operations: 65 66 List vaults: 67 glacier vaults 68 69 Jobs operations: 70 71 List jobs: 72 glacier jobs <vault name> 73 74 Uploading files: 75 76 glacier upload <vault name> <files> 77 78 Examples : 79 glacier upload pics *.jpg 80 glacier upload pics a.jpg b.jpg 81 """) 82 sys.exit() 83 84 85 def connect(region, debug_level=0, access_key=None, secret_key=None): 86 """ Connect to a specific region """ 87 layer2 = connect_to_region(region, 88 aws_access_key_id=access_key, 89 aws_secret_access_key=secret_key, 90 debug=debug_level) 91 if layer2 is None: 92 print('Invalid region (%s)' % region) 93 sys.exit(1) 94 return layer2 95 96 97 def list_vaults(region, access_key=None, secret_key=None): 98 layer2 = connect(region, access_key = access_key, secret_key = secret_key) 99 for vault in layer2.list_vaults(): 100 print(vault.arn) 101 102 103 def list_jobs(vault_name, region, access_key=None, secret_key=None): 104 layer2 = connect(region, access_key = access_key, secret_key = secret_key) 105 print(layer2.layer1.list_jobs(vault_name)) 106 107 108 def upload_files(vault_name, filenames, region, access_key=None, secret_key=None): 109 layer2 = connect(region, access_key = access_key, secret_key = secret_key) 110 layer2.create_vault(vault_name) 111 glacier_vault = layer2.get_vault(vault_name) 112 for filename in filenames: 113 if isfile(filename): 114 sys.stdout.write('Uploading %s to %s...' % (filename, vault_name)) 115 sys.stdout.flush() 116 archive_id = glacier_vault.upload_archive( 117 filename, 118 description = basename(filename)) 119 print(' done. Vault returned ArchiveID %s' % archive_id) 120 121 def main(): 122 if len(sys.argv) < 2: 123 usage() 124 125 command = sys.argv[1] 126 if command not in COMMANDS: 127 usage() 128 129 argv = sys.argv[2:] 130 options = 'a:s:r:' 131 long_options = ['access_key=', 'secret_key=', 'region='] 132 try: 133 opts, args = getopt(argv, options, long_options) 134 except GetoptError as e: 135 usage() 136 137 # Parse agument 138 access_key = secret_key = None 139 region = 'us-east-1' 140 for option, value in opts: 141 if option in ('-a', '--access_key'): 142 access_key = value 143 elif option in ('-s', '--secret_key'): 144 secret_key = value 145 elif option in ('-r', '--region'): 146 region = value 147 # handle each command 148 if command == 'vaults': 149 list_vaults(region, access_key, secret_key) 150 elif command == 'jobs': 151 if len(args) != 1: 152 usage() 153 list_jobs(args[0], region, access_key, secret_key) 154 elif command == 'upload': 155 if len(args) < 2: 156 usage() 157 upload_files(args[0], args[1:], region, access_key, secret_key) 158 159 160 if __name__ == '__main__': 161 main() 162