Skip to content

Instantly share code, notes, and snippets.

@NordomWhistleklik
Created November 2, 2012 16:59
Show Gist options
  • Select an option

  • Save NordomWhistleklik/4002692 to your computer and use it in GitHub Desktop.

Select an option

Save NordomWhistleklik/4002692 to your computer and use it in GitHub Desktop.
Simple script to batch upload to Rackspace's Cloud Files
## Written for Python 2.7.3
## Anthony Allan
## Free to use and reproduce
## This script is built for batch uploading
## files to your Rackspace Cloud Files account.
## Requires bindings for cloudfiles found here,
## https://github.com/rackspace/python-cloudfiles
# os, sys, and msvcrt are default libraries
# cloudfiles library nees to be installed
import os, sys, msvcrt, cloudfiles
username = '' # Rackspace account username
key = '' # Rackspace account api key
localDir = 'C:/Users/Anthony/Desktop/test' # No trailing slash
containerName = 'test' # Cloud Files container name
connection = cloudfiles.get_connection(username,key) # Start connection
container = connection.create_container(containerName) # Create or find container
# Locate all files to upload
# Currently set to only grab tifs and jpgs
# Does not crawl through subfolders
files = (localDir+'/%s'%x for x in os.listdir(localDir) if x.endswith('.tif') or x.endswith('.jpg'))
# Main upload loop
for file in files:
success = False
while success == False:
try:
currentObj = container.create_object(file) # Create new object
currentObj.load_from_filename(file) # Upload
print "Successfully uploaded", file # Yay!
success = True
except:
# This type of error catching is not best practice, however
# I had close to 700 GB of files to upload and I could not
# have the script breaking when I wasn't around to catch it.
print "***Failed to upload", file
print "***Error:", sys.exc_info()[0] # Displays last system exception
print "***Trying again..."
print "DONE... Press any key to exit."
msvcrt.getch() # Cool little feature in Windows to return any keypress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment