diff --git a/clients/storage/blob.go b/clients/storage/blob.go index c01bfbe535fa..f451e51fe108 100644 --- a/clients/storage/blob.go +++ b/clients/storage/blob.go @@ -235,7 +235,7 @@ type BlockListResponse struct { // in the GetBlockListCall. type BlockResponse struct { Name string `xml:"Name"` - Size uint64 `xml:"Size"` + Size int64 `xml:"Size"` } // GetPageRangesResponse contains the reponse fields from @@ -502,6 +502,25 @@ func (b BlobStorageClient) GetBlobProperties(container, name string) (*BlobPrope }, nil } +// CreateBlockBlob initializes an empty block blob with no blocks. +// See https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx +func (b BlobStorageClient) CreateBlockBlob(container, name string) error { + path := fmt.Sprintf("%s/%s", container, name) + uri := b.client.getEndpoint(blobServiceName, path, url.Values{}) + headers := b.client.getStandardHeaders() + headers["x-ms-blob-type"] = string(BlobTypeBlock) + headers["Content-Length"] = fmt.Sprintf("%v", 0) + + resp, err := b.client.exec("PUT", uri, headers, nil) + if err != nil { + return err + } + if resp.statusCode != http.StatusCreated { + return ErrNotCreated + } + return nil +} + // PutBlockBlob uploads given stream into a block blob by splitting // data stream into chunks and uploading as blocks. Commits the block // list at the end. This is a helper method built on top of PutBlock diff --git a/clients/storage/blob_test.go b/clients/storage/blob_test.go index cb9313462cf3..33e3d173dbf3 100644 --- a/clients/storage/blob_test.go +++ b/clients/storage/blob_test.go @@ -843,11 +843,41 @@ func TestGetBlockList_PutBlockList(t *testing.T) { if expected := blockId; expected != thatBlock.Name { t.Fatalf("Wrong block name. Expected: %s, got: %s", expected, thatBlock.Name) } - if expected := uint64(len(chunk)); expected != thatBlock.Size { + if expected := int64(len(chunk)); expected != thatBlock.Size { t.Fatalf("Wrong block name. Expected: %d, got: %d", expected, thatBlock.Size) } } +func TestCreateBlockBlob(t *testing.T) { + cli, err := getBlobClient() + if err != nil { + t.Fatal(err) + } + + cnt := randContainer() + if err := cli.CreateContainer(cnt, ContainerAccessTypePrivate); err != nil { + t.Fatal(err) + } + defer cli.deleteContainer(cnt) + + blob := randString(20) + if err := cli.CreateBlockBlob(cnt, blob); err != nil { + t.Fatal(err) + } + + // Verify + blocks, err := cli.GetBlockList(cnt, blob, BlockListTypeAll) + if err != nil { + t.Fatal(err) + } + if expected, got := 0, len(blocks.CommittedBlocks); expected != got { + t.Fatalf("Got wrong committed block count. Expected: %v, Got:%v ", expected, got) + } + if expected, got := 0, len(blocks.UncommittedBlocks); expected != got { + t.Fatalf("Got wrong uncommitted block count. Expected: %v, Got:%v ", expected, got) + } +} + func TestPutPageBlob(t *testing.T) { cli, err := getBlobClient() if err != nil {