Created
April 30, 2011 02:27
-
-
Save scvalex/949349 to your computer and use it in GitHub Desktop.
Simple Python script to upload a file to Rackspace CloudFiles
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
CONFIGURATION | |
------------- | |
To setup, simply set your username and API access key in the | |
cloudfiles.get_conenction call. If you're not using the UK | |
version of Rackspace, remove the third parameter from the above | |
call. | |
EXAMPLE USAGE | |
------------- | |
To upload the local file README.txt to the container "readmes", | |
just run: | |
python cloudfiles-upload README.txt readmes | |
Note that the container must have already been created. |
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
#!/usr/bin/env python2.7 | |
import cloudfiles | |
import math | |
import os.path | |
import sys | |
def main(args): | |
def usage(why): | |
print why | |
print "USAGE:" | |
print "%s <local-file> <existing-container>" % (sys.argv[0],) | |
def show_progress(transferred, size): | |
done = 50 * float(transferred) / size | |
print "\r%.1f%% [%s>%s] %d/%d " % (2 * done, | |
"#" * int(math.ceil(done)), | |
" " * int(50 - done), | |
transferred, size), | |
if len(args) != 2: | |
usage("wrong number of arguments") | |
sys.exit(1) | |
if not os.path.isfile(args[0]): | |
usage("file does not exist") | |
sys.exit(3) | |
print "Uploading %s to %s" % (args[0], args[1]) | |
conn = cloudfiles.get_connection('<your username>', | |
'<your api key>', | |
authurl = cloudfiles.uk_authurl) # remove this if you're NOT | |
# an UK customer | |
cont = conn.get_container(args[1]) | |
if not cont: | |
usage("container does not exist") | |
sys.exit(2) | |
obj = cont.create_object(os.path.basename(args[0])) | |
obj.load_from_filename(args[0], callback=show_progress) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A quick question. I have used this code to upload files into the home or base directory in rackspace. However, I cannot use it to upload files along a directory path, for example /home/dir_path/to/path_where_files_are. Am I missing something? Thanks.