Skip to content

Commit

Permalink
make block.Size int64, add CreateBlockBlob
Browse files Browse the repository at this point in the history
Signed-off-by: Ahmet Alp Balkan <[email protected]>
  • Loading branch information
ahmetb committed Feb 4, 2015
1 parent 9abf8bf commit c99fb1d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
21 changes: 20 additions & 1 deletion clients/storage/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 31 additions & 1 deletion clients/storage/blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c99fb1d

Please sign in to comment.