Skip to content

Commit af6985d

Browse files
committed
Merge pull request #35 from ahmetalpbalkan/CreateBlockBlob
make block.Size int64, add CreateBlockBlob
2 parents 9abf8bf + c99fb1d commit af6985d

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

clients/storage/blob.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ type BlockListResponse struct {
235235
// in the GetBlockListCall.
236236
type BlockResponse struct {
237237
Name string `xml:"Name"`
238-
Size uint64 `xml:"Size"`
238+
Size int64 `xml:"Size"`
239239
}
240240

241241
// GetPageRangesResponse contains the reponse fields from
@@ -502,6 +502,25 @@ func (b BlobStorageClient) GetBlobProperties(container, name string) (*BlobPrope
502502
}, nil
503503
}
504504

505+
// CreateBlockBlob initializes an empty block blob with no blocks.
506+
// See https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx
507+
func (b BlobStorageClient) CreateBlockBlob(container, name string) error {
508+
path := fmt.Sprintf("%s/%s", container, name)
509+
uri := b.client.getEndpoint(blobServiceName, path, url.Values{})
510+
headers := b.client.getStandardHeaders()
511+
headers["x-ms-blob-type"] = string(BlobTypeBlock)
512+
headers["Content-Length"] = fmt.Sprintf("%v", 0)
513+
514+
resp, err := b.client.exec("PUT", uri, headers, nil)
515+
if err != nil {
516+
return err
517+
}
518+
if resp.statusCode != http.StatusCreated {
519+
return ErrNotCreated
520+
}
521+
return nil
522+
}
523+
505524
// PutBlockBlob uploads given stream into a block blob by splitting
506525
// data stream into chunks and uploading as blocks. Commits the block
507526
// list at the end. This is a helper method built on top of PutBlock

clients/storage/blob_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,11 +843,41 @@ func TestGetBlockList_PutBlockList(t *testing.T) {
843843
if expected := blockId; expected != thatBlock.Name {
844844
t.Fatalf("Wrong block name. Expected: %s, got: %s", expected, thatBlock.Name)
845845
}
846-
if expected := uint64(len(chunk)); expected != thatBlock.Size {
846+
if expected := int64(len(chunk)); expected != thatBlock.Size {
847847
t.Fatalf("Wrong block name. Expected: %d, got: %d", expected, thatBlock.Size)
848848
}
849849
}
850850

851+
func TestCreateBlockBlob(t *testing.T) {
852+
cli, err := getBlobClient()
853+
if err != nil {
854+
t.Fatal(err)
855+
}
856+
857+
cnt := randContainer()
858+
if err := cli.CreateContainer(cnt, ContainerAccessTypePrivate); err != nil {
859+
t.Fatal(err)
860+
}
861+
defer cli.deleteContainer(cnt)
862+
863+
blob := randString(20)
864+
if err := cli.CreateBlockBlob(cnt, blob); err != nil {
865+
t.Fatal(err)
866+
}
867+
868+
// Verify
869+
blocks, err := cli.GetBlockList(cnt, blob, BlockListTypeAll)
870+
if err != nil {
871+
t.Fatal(err)
872+
}
873+
if expected, got := 0, len(blocks.CommittedBlocks); expected != got {
874+
t.Fatalf("Got wrong committed block count. Expected: %v, Got:%v ", expected, got)
875+
}
876+
if expected, got := 0, len(blocks.UncommittedBlocks); expected != got {
877+
t.Fatalf("Got wrong uncommitted block count. Expected: %v, Got:%v ", expected, got)
878+
}
879+
}
880+
851881
func TestPutPageBlob(t *testing.T) {
852882
cli, err := getBlobClient()
853883
if err != nil {

0 commit comments

Comments
 (0)