pageblob package - github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/pageblob - Go Packages (original) (raw)

ExamplePageBlobClient shows how to manipulate a page blob with PageBlobClient. A page blob is a collection of 512-byte pages optimized for random read and write operations. The maximum size for a page blob is 8 TB.

//go:build go1.18 // +build go1.18

// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information.

package main

import ( "bytes" "context" "crypto/rand" "fmt" "log" "os"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/pageblob"

)

func handleError(err error) { if err != nil { log.Fatal(err.Error()) } }

// ExamplePageBlobClient shows how to manipulate a page blob with PageBlobClient. // A page blob is a collection of 512-byte pages optimized for random read and write operations. // The maximum size for a page blob is 8 TB. func main() { accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME") if !ok { panic("AZURE_STORAGE_ACCOUNT_NAME could not be found") } blobName := "test_page_blob.vhd" blobURL := fmt.Sprintf("https://%s.blob.core.windows.net/testcontainer/%s", accountName, blobName)

cred, err := azidentity.NewDefaultAzureCredential(nil)
handleError(err)

pageBlobClient, err := pageblob.NewClient(blobURL, cred, nil)
handleError(err)

_, err = pageBlobClient.Create(context.TODO(), pageblob.PageBytes*4, nil)
handleError(err)

// Upload 5 pages to the page blob
for i := 0; i < 5; i++ {
    count := int64(1024)
    page := make([]byte, 2*pageblob.PageBytes)
    _, err := rand.Read(page)
    if err != nil {
        return
    }
    _, err = pageBlobClient.UploadPages(context.Background(), streaming.NopCloser(bytes.NewReader(page)),
        blob.HTTPRange{Offset: int64(i) * count, Count: count}, nil)
    handleError(err)
}

pager := pageBlobClient.NewGetPageRangesPager(&pageblob.GetPageRangesOptions{
    Range: blob.HTTPRange{
        Count: int64(10 * pageblob.PageBytes),
    },
})

for pager.More() {
    resp, err := pager.NextPage(context.TODO())
    if err != nil {
        log.Fatal(err)
    }
    for _, pr := range resp.PageList.PageRange {
        fmt.Printf("Start=%d, End=%d\n", pr.Start, pr.End)
    }
}

get, err := pageBlobClient.DownloadStream(context.TODO(), nil)
handleError(err)
blobData := &bytes.Buffer{}
reader := get.Body
_, err = blobData.ReadFrom(reader)
if err != nil {
    return
}
err = reader.Close()
if err != nil {
    return
}
fmt.Println(blobData.String())

}

Output:

This section is empty.

This section is empty.

ClearPagesOptions contains the optional parameters for the Client.ClearPages operation

ClearPagesResponse contains the response from method Client.ClearPages.

ClearRange defines a range of pages.

Client represents a client to an Azure Storage page blob;

NewClient creates an instance of Client with the specified values.

func NewClientFromConnectionString(connectionString, containerName, blobName string, options ClientOptions) (Client, error)

NewClientFromConnectionString creates an instance of Client with the specified values.

func NewClientWithNoCredential(blobURL string, options ClientOptions) (Client, error)

NewClientWithNoCredential creates an instance of Client with the specified values. This is used to anonymously access a blob or with a shared access signature (SAS) token.

NewClientWithSharedKeyCredential creates an instance of Client with the specified values.

BlobClient returns the embedded blob client for this AppendBlob client.

DownloadBuffer downloads an Azure blob to a buffer with parallel.

DownloadFile downloads an Azure blob to a local file. The file would be truncated if the size doesn't match.

GetSASURL is a convenience method for generating a SAS token for the currently pointed at Page blob. It can only be used if the credential supplied during creation was a SharedKeyCredential.

SetTier operation sets the tier on a blob. The operation is allowed on a page blob in a premium storage account and on a block blob in a blob storage account (locally redundant storage only). A premium page blob's tier determines the allowed size, IOPs, and bandwidth of the blob. A block blob's tier determines Hot/Cool/Archive storage type. This operation does not update the blob's ETag. For detailed information about block blob level tier-ing see https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-storage-tiers.

URL returns the URL endpoint used by the Client object.

UpdateSequenceNumber sets the page blob's sequence number.

UploadPages writes 1 or more pages to the page blob. The start offset and the stream size must be a multiple of 512 bytes. This method panics if the stream is not at position 0. Note that the http client closes the body stream after the request is sent to the service. For more information, see https://docs.microsoft.com/rest/api/storageservices/put-page.

UploadPagesFromURL copies 1 or more pages from a source URL to the page blob. The sourceOffset specifies the start offset of source data to copy from. The destOffset specifies the start offset of data in page blob will be written to. The count must be a multiple of 512 bytes. For more information, see https://docs.microsoft.com/rest/api/storageservices/put-page-from-url.

WithSnapshot creates a new PageBlobURL object identical to the source but with the specified snapshot timestamp. Pass "" to remove the snapshot returning a URL to the base blob.

WithVersionID creates a new PageBlobURL object identical to the source but with the specified snapshot timestamp. Pass "" to remove the version returning a URL to the base blob.

ClientOptions contains the optional parameters when creating a Client.

CopyIncrementalOptions contains the optional parameters for the Client.StartCopyIncremental method.

CopyIncrementalResponse contains the response from method Client.StartCopyIncremental.

CopyStatusType defines values for CopyStatusType

func PossibleCopyStatusTypeValues() []CopyStatusType

PossibleCopyStatusTypeValues returns the possible values for the CopyStatusType const type.

CreateOptions contains the optional parameters for the Client.Create method.

CreateResponse contains the response from method Client.Create.

GetPageRangesDiffOptions contains the optional parameters for the Client.NewGetPageRangesDiffPager method.

GetPageRangesDiffResponse contains the response from method Client.NewGetPageRangesDiffPager.

GetPageRangesOptions contains the optional parameters for the Client.NewGetPageRangesPager method.

GetPageRangesResponse contains the response from method Client.NewGetPageRangesPager.

PageList - the list of pages.

PageRange defines a range of pages.

PremiumPageBlobAccessTier defines values for Premium PageBlob's AccessTier.

func PossiblePremiumPageBlobAccessTierValues() []PremiumPageBlobAccessTier

PossiblePremiumPageBlobAccessTierValues returns the possible values for the PremiumPageBlobAccessTier const type.

ResizeOptions contains the optional parameters for the Client.Resize method.

ResizeResponse contains the response from method Client.Resize.

SequenceNumberAccessConditions contains a group of parameters for the Client.UploadPages method.

SequenceNumberActionType defines values for SequenceNumberActionType.

func PossibleSequenceNumberActionTypeValues() []SequenceNumberActionType

PossibleSequenceNumberActionTypeValues returns the possible values for the SequenceNumberActionType const type.

UpdateSequenceNumberOptions contains the optional parameters for the Client.UpdateSequenceNumber method.

UpdateSequenceNumberResponse contains the response from method Client.UpdateSequenceNumber.

UploadPagesFromURLOptions contains the optional parameters for the Client.UploadPagesFromURL method.

UploadPagesFromURLResponse contains the response from method Client.UploadPagesFromURL.

UploadPagesOptions contains the optional parameters for the Client.UploadPages method.

UploadPagesResponse contains the response from method Client.UploadPages.