Skip to content

Instantly share code, notes, and snippets.

@lsowen
Last active November 11, 2017 13:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsowen/d432e437ffbffd38c3e2 to your computer and use it in GitHub Desktop.
Save lsowen/d432e437ffbffd38c3e2 to your computer and use it in GitHub Desktop.
Example to create Rackspace Files TempURL with golang
package main
// Based on documentation found here: http://docs.rackspace.com/files/api/v1/cf-devguide/content/Create_TempURL-d1a444.html
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"fmt"
"github.com/ncw/swift"
"path"
"time"
)
func tempUrlSignature(tempKey string, path string, expiration time.Time) string {
body := fmt.Sprintf("GET\n%d\n%s", expiration.Unix(), path)
max := hmac.New(sha1.New, []byte(tempKey))
max.Write([]byte(body))
sig := hex.EncodeToString(max.Sum(nil))
return sig
}
func tempUrl(tempKey string, storageUrl string, containerName string, objectName string, expiration time.Time) string {
_, accountName := path.Split(storageUrl)
requestPath := fmt.Sprintf("/v1/%s/%s/%s", accountName, containerName, objectName)
signature := tempUrlSignature(tempKey, requestPath, expiration)
tempUrl := fmt.Sprintf("%s/%s/%s?temp_url_sig=%s&temp_url_expires=%d", storageUrl, containerName, objectName, signature, expiration.Unix())
return tempUrl
}
func main() {
containerName := "container"
objectName := "filename"
c := swift.Connection{
UserName: "username",
ApiKey: "api_key",
AuthUrl: "https://identity.api.rackspacecloud.com/v2.0/",
Region: "ORD",
}
err := c.Authenticate()
if err != nil {
panic(err)
}
_, headers, err := c.Account()
if err != nil {
panic(err)
}
tempKey, ok := headers["X-Account-Meta-Temp-Url-Key"]
if !ok {
panic("tempKey not found")
}
expiration := time.Now().Add(time.Second * 60)
tempUrl := tempUrl(tempKey, c.StorageUrl, containerName, objectName, expiration)
fmt.Println(tempUrl)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment