lf215
lf215

Reputation: 959

Azure blob first write

This official example for writing blob blocks has a step where it checks which blocks have not been committed:

    fmt.Println("Get uncommitted blocks list...")
    list, err := b.GetBlockList(storage.BlockListTypeUncommitted, nil)
    if err != nil {
        return fmt.Errorf("get block list failed: %v", err)
    }
    uncommittedBlocksList := make([]storage.Block, len(list.UncommittedBlocks))
    for i := range list.UncommittedBlocks {
        uncommittedBlocksList[i].ID = list.UncommittedBlocks[i].Name
        uncommittedBlocksList[i].Status = storage.BlockStatusUncommitted
    }

If I'm creating a blob (with multiple blocks) that definitely doesn't yet exist. Is there any problem with skipping that code?

The code would be something like:

    b := cnt.GetBlobReference(blockBlobName)
    err := b.CreateBlockBlob(nil)

    blockID := "00000"
    data := randomData(1984)
    err = b.PutBlock(blockID, data, nil)

    blockID2 := "00001"
    data2 := randomData(6542)
    err = b.PutBlock(blockID2, data2, nil)

    var uncommittedBlocksList []storage.Block
    uncommittedBlocksList = append(uncommittedBlocksList, 
        Block{
           ID:"00000"
           Status:BlockStatusUncommitted,
        },
        Block{
           ID:"00001"
           Status:BlockStatusUncommitted,
        },
    )
    err = b.PutBlockList(uncommittedBlocksList, nil)

Upvotes: 1

Views: 89

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136336

If I'm creating a blob (with multiple blocks) that definitely doesn't yet exist. Is there any problem with skipping that code?

Absolutely not. You can certainly skip the code for fetching uncommitted block list. This scenario for fetching uncommitted list is useful when you tried to upload a blob and it failed in between and you want to resume the upload from the last failed block. By skipping this code, you are essentially telling Azure Storage to discard any other uncommitted blocks and use the blocks specified in the block list to create the blob.

Upvotes: 3

Related Questions