Skip to content

Instantly share code, notes, and snippets.

@jmhobbs
Created November 30, 2011 19:34
Show Gist options
  • Save jmhobbs/1410450 to your computer and use it in GitHub Desktop.
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.
# 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
print
print "=" * 80
print
print "Uploaded %d in %f seconds" % (len(uploads), time.time() - total_start)
print "\nErrors:"
for error in errors:
print "\t%s: %s" % error
@jmhobbs
Copy link
Author

jmhobbs commented Nov 30, 2011

PEP8 - what wha!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment