8 November 2013

Using cURL to upload to Rackspace Cloud Files

Posted by Christoph under: Rackspace .

In this post, I will show you how to quickly upload files to Rackspace Cloud Files using only the Linux CLI (i.e., no SDKs, Rackspace-specific tools, or anything that is not already installed on a standard Linux distro).

This post will assume you already have some basic understanding of how application programming interfaces (APIs) work, you have a Rackspace Cloud account (w/Cloud Files), and understand the basics of the Linux CLI (e.g., you know how to use cURL). See my Rackspace API article on my wiki for more in-depth explanations and examples.

Note: The following commands use Python. It should work with any version of Python, however, it was tested with Python 2.7.4.

First get your 24-hour-valid token:

$ MYRAXTOKEN=`curl -s -XPOST https://identity.api.rackspacecloud.com/v2.0/tokens -d'{"auth":{"RAX-KSKEY:apiKeyCredentials":{"username":"$MYRAXUSERNAME","apiKey":"'$MYRAXAPIKEY'"}}}' -H"Content-type:application/json" | python -c 'import sys,json;data=json.loads(sys.stdin.read());print data["access"]["token"]["id"]'`

Then, use a for-loop to upload your files (e.g., all of the PNG files in you present working direcory) to a given container (e.g., “sandbox”):

$ REGION=dfw; CONTAINER_NAME=sandbox; for i in *.png; do curl -XPUT -T $i -v -H "X-Auth-Token:$MYRAXTOKEN" -H"Content-Type: text/plain" "https://storage101.${REGION}1.clouddrive.com/v1/MossoCloudFS_ffff-ffff-ffff-ffff-ffff/${CONTAINER_NAME}/$i"; done

Note: Obviously, you will need to replace MossoCloudFS_ffff-ffff-ffff-ffff-ffff with your actual unique container URL.

Share Button