create temp url operation and test
diff --git a/acceptance/rackspace/objectstorage/v1/objects_test.go b/acceptance/rackspace/objectstorage/v1/objects_test.go
index 96a7d8c..585dea7 100644
--- a/acceptance/rackspace/objectstorage/v1/objects_test.go
+++ b/acceptance/rackspace/objectstorage/v1/objects_test.go
@@ -30,6 +30,7 @@
options := &osObjects.CreateOpts{ContentType: "text/plain"}
createres := raxObjects.Create(c, "gophercloud-test", "o1", content, options)
th.AssertNoErr(t, createres.Err)
+
defer func() {
t.Logf("Deleting object o1...")
res := raxObjects.Delete(c, "gophercloud-test", "o1", nil)
@@ -112,4 +113,12 @@
th.AssertNoErr(t, err)
t.Logf("Metadata from Get Account request (after update): %+v\n", metadata)
th.CheckEquals(t, "mountains", metadata["White"])
+
+ createTempURLOpts := osObjects.CreateTempURLOpts{
+ Method: osObjects.GET,
+ TTL: 600,
+ }
+ tempURL, err := raxObjects.CreateTempURL(c, "gophercloud-test", "o1", createTempURLOpts)
+ th.AssertNoErr(t, err)
+ t.Logf("TempURL for object (%s): %s", "o1", tempURL)
}
diff --git a/openstack/objectstorage/v1/objects/requests.go b/openstack/objectstorage/v1/objects/requests.go
index 0fdb041..7acff11 100644
--- a/openstack/objectstorage/v1/objects/requests.go
+++ b/openstack/objectstorage/v1/objects/requests.go
@@ -1,12 +1,16 @@
package objects
import (
+ "crypto/hmac"
+ "crypto/sha1"
"fmt"
"io"
+ "strings"
"time"
"github.com/racker/perigee"
"github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/openstack/objectstorage/v1/accounts"
"github.com/rackspace/gophercloud/pagination"
)
@@ -424,3 +428,44 @@
res.Err = err
return res
}
+
+// HTTPMethod represents an HTTP method string (e.g. "GET").
+type HTTPMethod string
+
+var (
+ // GET represents an HTTP "GET" method.
+ GET HTTPMethod = "GET"
+ // POST represents an HTTP "POST" method.
+ POST HTTPMethod = "POST"
+)
+
+// CreateTempURLOpts are options for creating a temporary URL for an object.
+type CreateTempURLOpts struct {
+ // Method is the HTTP method to allow for users of the temp URL. Valid values
+ // are "GET" and "POST".
+ Method HTTPMethod
+ // TTL is the amount of time the temp URL should be active (in seconds).
+ TTL int
+}
+
+// CreateTempURL is a function for creating a temporary URL for an object. It
+// allows users to have "GET" or "POST" access to a particular tenant's object
+// for a limited amount of time.
+func CreateTempURL(c *gophercloud.ServiceClient, containerName, objectName string, opts CreateTempURLOpts) (string, error) {
+ duration := time.Duration(opts.TTL) * time.Second
+ expiry := time.Now().Add(duration).Unix()
+ getHeader, err := accounts.Get(c, nil).Extract()
+ if err != nil {
+ return "", err
+ }
+ secretKey := []byte(getHeader.TempURLKey)
+ url := getURL(c, containerName, objectName)
+ splitPath := strings.Split(url, "/v1/")
+ baseURL, objectPath := splitPath[0], splitPath[1]
+ objectPath = "/v1/" + objectPath
+ body := fmt.Sprintf("%s\n%d\n%s", opts.Method, expiry, objectPath)
+ hash := hmac.New(sha1.New, secretKey)
+ hash.Write([]byte(body))
+ hexsum := fmt.Sprintf("%x", hash.Sum(nil))
+ return fmt.Sprintf("%s%s?temp_url_sig=%s&temp_url_expires=%d", baseURL, objectPath, hexsum, expiry), nil
+}
diff --git a/rackspace/objectstorage/v1/objects/delegate.go b/rackspace/objectstorage/v1/objects/delegate.go
index bd4a4f0..028d66a 100644
--- a/rackspace/objectstorage/v1/objects/delegate.go
+++ b/rackspace/objectstorage/v1/objects/delegate.go
@@ -88,3 +88,7 @@
func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts os.UpdateOptsBuilder) os.UpdateResult {
return os.Update(c, containerName, objectName, opts)
}
+
+func CreateTempURL(c *gophercloud.ServiceClient, containerName, objectName string, opts os.CreateTempURLOpts) (string, error) {
+ return os.CreateTempURL(c, containerName, objectName, opts)
+}