Created
November 30, 2011 19:34
-
-
Save jmhobbs/1410450 to your computer and use it in GitHub Desktop.
The tools for Rackspace Cloud Files suck, so here is a non-overwrite recursive directory uploader in Python.
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
# SET THESE | |
USERNAME = '' | |
API_KEY = '' | |
CONTAINER_NAME = '' | |
LOCAL_DIRECTORY_NAME = '' | |
# FORGET THESE | |
import cloudfiles | |
import os | |
import sys | |
import time | |
def transfer_callback(tfd, size): | |
print "%s / %s\r" % (tfd, size), | |
sys.stdout.flush() | |
connection = cloudfiles.get_connection(USERNAME, API_KEY) | |
container = connection.get_container(CONTAINER_NAME) | |
all_objects = container.list_objects() | |
errors = [] | |
uploads = [] | |
total_start = time.time() | |
try: | |
for root, dirs, files in os.walk(LOCAL_DIRECTORY_NAME): | |
for f in files: | |
local_path = os.path.join(root, f) | |
remote_path = local_path[len(LOCAL_DIRECTORY_NAME):] | |
if remote_path not in all_objects: | |
print "Uploading:", remote_path | |
sys.stdout.flush() | |
try: | |
start = time.time() | |
obj = container.create_object(remote_path) | |
obj.load_from_filename( | |
local_path, | |
callback=transfer_callback) | |
elapsed = time.time() - start | |
uploads.append( ( remote_path, elapsed ) ) | |
print "\rDone - %f" % elapsed | |
except Exception, e: | |
print "\rError: %s" % e | |
errors.append((remote_path, e)) | |
sys.stdout.flush() | |
except KeyboardInterrupt: | |
pass | |
print "=" * 80 | |
print "Uploaded %d in %f seconds" % (len(uploads), time.time() - total_start) | |
print "\nErrors:" | |
for error in errors: | |
print "\t%s: %s" % error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PEP8 - what wha!