Last active
December 26, 2015 03:59
-
-
Save siso/7089444 to your computer and use it in GitHub Desktop.
How to upload a local file to CloudFiles.
Remote container and file name can be specified on the CLI. $ python upload_file.py -u USERNAME -k APIKEY --local-file /PATH/TO/LOCAL/FILE --container CONTAINER --remote-file PATH/TO/REMOTE/FILE
This file contains hidden or 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
import argparse | |
import pyrax | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-k', '--api-key', help='Authentication api-key', required=True) | |
parser.add_argument('-i', '--identity-type', | |
help='identity type (default: \'rackspace\')', | |
default='rackspace') | |
parser.add_argument('--container', help='remote container', required=True) | |
parser.add_argument('--local-file', help='local file to upload', required=True) | |
parser.add_argument('--remote-file', help='remote filename', required=True) | |
parser.add_argument('-r', '--region', help='region (default: \'LON\')', default='LON') | |
parser.add_argument('-u', '--username', help='Authentication username', required=True) | |
args = parser.parse_args() | |
print args | |
pyrax.set_setting('identity_type', args.identity_type) | |
pyrax.set_credentials(args.username, args.api_key, region=args.region) | |
print "authenticated: ", pyrax.identity.authenticated | |
cf = pyrax.cloudfiles | |
# check container | |
container = None | |
for c in cf.get_all_containers(): | |
if c.name == args.container: | |
container = c | |
break | |
if container == None: | |
cf.create_container(args.container) | |
container = cf.get_container(args.container) | |
print "created remote container: ", container.name | |
# upload file | |
container.upload_file(args.local_file, args.remote_file) | |
# verify file checksum | |
checksum_local = pyrax.utils.get_checksum(args.local_file) | |
checksum_remote = container.get_object(args.remote_file).etag | |
if checksum_local == checksum_remote: | |
print "file copied successfully" | |
else: | |
print "error copying file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment