Skip to content

Commit

Permalink
Merge pull request #19 from jf/master
Browse files Browse the repository at this point in the history
blob.go: use helper methods for creating paths
  • Loading branch information
ahmetb committed Feb 3, 2015
2 parents 24f03c1 + 612a5f5 commit 0d03132
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
50 changes: 25 additions & 25 deletions clients/storage/blob.go
Expand Up @@ -305,7 +305,7 @@ func (b BlobStorageClient) CreateContainerIfNotExists(name string, access Contai

func (b BlobStorageClient) createContainer(name string, access ContainerAccessType) (*storageResponse, error) {
verb := "PUT"
uri := b.client.getEndpoint(blobServiceName, name, url.Values{"restype": {"container"}})
uri := b.client.getEndpoint(blobServiceName, pathForContainer(name), url.Values{"restype": {"container"}})

headers := b.client.getStandardHeaders()
headers["Content-Length"] = "0"
Expand All @@ -319,8 +319,7 @@ func (b BlobStorageClient) createContainer(name string, access ContainerAccessTy
// on the storage account, otherwise returns false.
func (b BlobStorageClient) ContainerExists(name string) (bool, error) {
verb := "HEAD"
path := fmt.Sprintf("%s", name)
uri := b.client.getEndpoint(blobServiceName, path, url.Values{"restype": {"container"}})
uri := b.client.getEndpoint(blobServiceName, pathForContainer(name), url.Values{"restype": {"container"}})
headers := b.client.getStandardHeaders()

resp, err := b.client.exec(verb, uri, headers, nil)
Expand Down Expand Up @@ -358,7 +357,7 @@ func (b BlobStorageClient) DeleteContainerIfExists(name string) (bool, error) {

func (b BlobStorageClient) deleteContainer(name string) (*storageResponse, error) {
verb := "DELETE"
uri := b.client.getEndpoint(blobServiceName, name, url.Values{"restype": {"container"}})
uri := b.client.getEndpoint(blobServiceName, pathForContainer(name), url.Values{"restype": {"container"}})

headers := b.client.getStandardHeaders()
return b.client.exec(verb, uri, headers, nil)
Expand All @@ -371,7 +370,7 @@ func (b BlobStorageClient) ListBlobs(container string, params ListBlobsParameter
q := mergeParams(params.getParameters(), url.Values{
"restype": {"container"},
"comp": {"list"}})
uri := b.client.getEndpoint(blobServiceName, container, q)
uri := b.client.getEndpoint(blobServiceName, pathForContainer(container), q)
headers := b.client.getStandardHeaders()

var out BlobListResponse
Expand All @@ -388,8 +387,7 @@ func (b BlobStorageClient) ListBlobs(container string, params ListBlobsParameter
// specified container of the storage account.
func (b BlobStorageClient) BlobExists(container, name string) (bool, error) {
verb := "HEAD"
path := fmt.Sprintf("%s/%s", container, name)
uri := b.client.getEndpoint(blobServiceName, path, url.Values{})
uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{})

headers := b.client.getStandardHeaders()
resp, err := b.client.exec(verb, uri, headers, nil)
Expand All @@ -407,8 +405,7 @@ func (b BlobStorageClient) GetBlobUrl(container, name string) string {
if container == "" {
container = "$root"
}
path := fmt.Sprintf("%s/%s", container, name)
return b.client.getEndpoint(blobServiceName, path, url.Values{})
return b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{})
}

// GetBlob downloads a blob to a stream. See https://msdn.microsoft.com/en-us/library/azure/dd179440.aspx
Expand Down Expand Up @@ -441,8 +438,7 @@ func (b BlobStorageClient) GetBlobRange(container, name, bytesRange string) (io.

func (b BlobStorageClient) getBlobRange(container, name, bytesRange string) (*storageResponse, error) {
verb := "GET"
path := fmt.Sprintf("%s/%s", container, name)
uri := b.client.getEndpoint(blobServiceName, path, url.Values{})
uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{})

headers := b.client.getStandardHeaders()
if bytesRange != "" {
Expand All @@ -459,8 +455,7 @@ func (b BlobStorageClient) getBlobRange(container, name, bytesRange string) (*st
// blob. See https://msdn.microsoft.com/en-us/library/azure/dd179394.aspx
func (b BlobStorageClient) GetBlobProperties(container, name string) (*BlobProperties, error) {
verb := "HEAD"
path := fmt.Sprintf("%s/%s", container, name)
uri := b.client.getEndpoint(blobServiceName, path, url.Values{})
uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{})

headers := b.client.getStandardHeaders()
resp, err := b.client.exec(verb, uri, headers, nil)
Expand Down Expand Up @@ -566,8 +561,7 @@ func (b BlobStorageClient) putSingleBlockBlob(container, name string, chunk []by
return fmt.Errorf("storage: provided chunk (%d bytes) cannot fit into single-block blob (max %d bytes)", len(chunk), MaxBlobBlockSize)
}

path := fmt.Sprintf("%s/%s", container, name)
uri := b.client.getEndpoint(blobServiceName, path, url.Values{})
uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{})
headers := b.client.getStandardHeaders()
headers["x-ms-blob-type"] = string(BlobTypeBlock)
headers["Content-Length"] = fmt.Sprintf("%v", len(chunk))
Expand All @@ -594,8 +588,7 @@ func (b BlobStorageClient) PutBlock(container, name, blockId string, chunk []byt
// It is an alternative to PutBlocks where data comes as stream but the length is
// known in advance.
func (b BlobStorageClient) PutBlockWithLength(container, name, blockId string, size uint64, blob io.Reader) error {
path := fmt.Sprintf("%s/%s", container, name)
uri := b.client.getEndpoint(blobServiceName, path, url.Values{"comp": {"block"}, "blockid": {blockId}})
uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{"comp": {"block"}, "blockid": {blockId}})
headers := b.client.getStandardHeaders()
headers["x-ms-blob-type"] = string(BlobTypeBlock)
headers["Content-Length"] = fmt.Sprintf("%v", size)
Expand All @@ -616,8 +609,7 @@ func (b BlobStorageClient) PutBlockWithLength(container, name, blockId string, s
func (b BlobStorageClient) PutBlockList(container, name string, blocks []Block) error {
blockListXml := prepareBlockListRequest(blocks)

path := fmt.Sprintf("%s/%s", container, name)
uri := b.client.getEndpoint(blobServiceName, path, url.Values{"comp": {"blocklist"}})
uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{"comp": {"blocklist"}})
headers := b.client.getStandardHeaders()
headers["Content-Length"] = fmt.Sprintf("%v", len(blockListXml))

Expand All @@ -635,7 +627,7 @@ func (b BlobStorageClient) PutBlockList(container, name string, blocks []Block)
// https://msdn.microsoft.com/en-us/library/azure/dd179400.aspx
func (b BlobStorageClient) GetBlockList(container, name string, blockType BlockListType) (BlockListResponse, error) {
params := url.Values{"comp": {"blocklist"}, "blocklisttype": {string(blockType)}}
uri := b.client.getEndpoint(blobServiceName, fmt.Sprintf("%s/%s", container, name), params)
uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params)
headers := b.client.getStandardHeaders()

var out BlockListResponse
Expand Down Expand Up @@ -738,8 +730,7 @@ func (b BlobStorageClient) CopyBlob(container, name, sourceBlob string) error {
}

func (b BlobStorageClient) startBlobCopy(container, name, sourceBlob string) (string, error) {
path := fmt.Sprintf("%s/%s", container, name)
uri := b.client.getEndpoint(blobServiceName, path, url.Values{})
uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{})

headers := b.client.getStandardHeaders()
headers["Content-Length"] = "0"
Expand Down Expand Up @@ -813,13 +804,22 @@ func (b BlobStorageClient) DeleteBlobIfExists(container, name string) (bool, err

func (b BlobStorageClient) deleteBlob(container, name string) (*storageResponse, error) {
verb := "DELETE"
path := fmt.Sprintf("%s/%s", container, name)
uri := b.client.getEndpoint(blobServiceName, path, url.Values{})

uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{})
headers := b.client.getStandardHeaders()

return b.client.exec(verb, uri, headers, nil)
}

// helper method to construct the path to a container given its name
func pathForContainer(name string) string {
return fmt.Sprintf("/%s", name)
}

// helper method to construct the path to a blob given its container and blob name
func pathForBlob(container, name string) string {
return fmt.Sprintf("/%s/%s", container, name)
}

// GetBlobSASURI creates an URL to the specified blob which contains the Shared Access Signature
// with specified permissions and expiration time. See https://msdn.microsoft.com/en-us/library/azure/ee395415.aspx
func (b BlobStorageClient) GetBlobSASURI(container, name string, expiry time.Time, permissions string) (string, error) {
Expand Down
18 changes: 16 additions & 2 deletions clients/storage/blob_test.go
Expand Up @@ -20,7 +20,21 @@ import (

const testContainerPrefix = "zzzztest-"

func TestblobSASStringToSign(t *testing.T) {
func Test_pathForContainer(t *testing.T) {
out := pathForContainer("foo")
if expected := "/foo"; out != expected {
t.Errorf("Wrong pathForContainer. Expected: '%s', got: '%s'", expected, out)
}
}

func Test_pathForBlob(t *testing.T) {
out := pathForBlob("foo", "blob")
if expected := "/foo/blob"; out != expected {
t.Errorf("Wrong pathForBlob. Expected: '%s', got: '%s'", expected, out)
}
}

func Test_blobSASStringToSign(t *testing.T) {
_, err := blobSASStringToSign("2012-02-12", "CS", "SE", "SP")
if err == nil {
t.Fatal("Expected error, got nil")
Expand All @@ -30,7 +44,7 @@ func TestblobSASStringToSign(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if expected := "SP\n\nSE\nCS\n\n\n2013-08-15\n%s\n%s\n%s\n%s\n%s"; out != expected {
if expected := "SP\n\nSE\nCS\n\n2013-08-15\n\n\n\n\n"; out != expected {
t.Errorf("Wrong stringToSign. Expected: '%s', got: '%s'", expected, out)
}
}
Expand Down

0 comments on commit 0d03132

Please sign in to comment.