Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

So I want to automate the process of uploading a file to Rackspace. Here's what I need to do:

First, I type:

 curl -D - \
        -H "X-Auth-Key: 123456789abcdefghijk" \
        -H "X-Auth-User: MyUsername" \
        https://auth.api.rackspacecloud.com/v1.0

This gives me back a response such as:

HTTP/1.1 204 No Content
Server: Apache/2.2.3 (Red Hat)
vary: X-Auth-Token,X-Auth-Key,X-Storage-User,X-Storage-Pass
X-Storage-Url: https://storage101.ord1.clouddrive.com/v1/BlahBlah
Cache-Control: s-maxage=46818
Content-Type: text/xml
Date: Tue, 07 Feb 2012 05:07:09 GMT
X-Auth-Token: SOME_AUTH_TOKEN_I_NEED
X-Storage-Token: blahblah
X-Server-Management-Url: https://servers.api.rackspacecloud.com/v1.0/123456
Connection: Keep-Alive
X-CDN-Management-Url: https://cdn2.clouddrive.com/v1/BlahBlah

Next, I need to upload a file:

curl -X PUT -T foo.txt -D - \
     -H "ETag: 7849eb8d56581fa7c4896bb0db64892c" \
     -H "Content-Type: text/plain" \
     -H "X-Auth-Token: SOME_AUTH_TOKEN_I_NEED" \
     -H "X-Object-Meta-Screenie: Test" \
     https://storage101.ord1.clouddrive.com/v1/BlahBlah/MyBackup/foo.txt

So basically, I need to parse the auth token (X-Auth-Token) as well as the storage URL (X-Storage-Url) and build the next command dynamically.

Is there a way to do that with a shell script, perhaps using regular expressions or something? Any pointers would be much appreciated!

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

There's surely a more elegant solution than this, but off the top of my head, a quick and dirty bash way of doing it would be something like:

#!/bin/bash

xauthtoken=`curl -D - \
        -H "X-Auth-Key: 123456789abcdefghijk" \
        -H "X-Auth-User: MyUsername" \
        https://auth.api.rackspacecloud.com/v1.0 | grep X\-Auth\-Token | awk '{print $2}'`

xstorageurl=`curl -D - \
        -H "X-Auth-Key: 123456789abcdefghijk" \
        -H "X-Auth-User: MyUsername" \
        https://auth.api.rackspacecloud.com/v1.0 | grep X\-Storage\-Url | awk '{print $2}'`

curl -X PUT -T foo.txt -D - \
     -H "ETag: 7849eb8d56581fa7c4896bb0db64892c" \
     -H "Content-Type: text/plain" \
     -H "X-Auth-Token: ${xauthtoken}" \
     -H "X-Object-Meta-Screenie: Test" \
     $xstorageurl
share|improve this answer
    
Thanks! I ended up just writing this as a Python script instead and it works much better. I'll save this for future reference though! –  Mike Christensen Feb 7 '12 at 7:25
    
please post your python solution as well! –  ThorstenS Feb 7 '12 at 8:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.