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

ExampleAppendBlobClient shows how to append data (in blocks) to an append blob. An append blob can have a maximum of 50,000 blocks; each block can have a maximum of 100MB. The maximum size of an append blob is slightly more than 4.75 TB (100 MB X 50,000 blocks).

package main

import ( "bytes" "context" "fmt" "log" "os" "strings"

"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/appendblob"

)

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

func main() { accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME") if !ok { panic("AZURE_STORAGE_ACCOUNT_NAME could not be found") } blobName := "test_append_blob.txt" blobURL := fmt.Sprintf("https://%s.blob.core.windows.net/testcontainer/%s", accountName, blobName)

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

appendBlobClient, err := appendblob.NewClient(blobURL, cred, nil)
handleError(err)

_, err = appendBlobClient.Create(context.TODO(), nil)
handleError(err)

for i := 0; i < 5; i++ { // Append 5 blocks to the append blob
    _, err := appendBlobClient.AppendBlock(context.TODO(), streaming.NopCloser(strings.NewReader(fmt.Sprintf("Appending block #%d\n", i))), nil)
    if err != nil {
        log.Fatal(err)
    }
}

// Download the entire append blob's contents and read into a bytes.Buffer.
get, err := appendBlobClient.DownloadStream(context.TODO(), nil)
handleError(err)
b := bytes.Buffer{}
reader := get.Body
_, err = b.ReadFrom(reader)
if err != nil {
    return
}
err = reader.Close()
if err != nil {
    return
}
fmt.Println(b.String())

}

Output:

package main

import ( "context" "fmt" "log" "os"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/appendblob"

)

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

func main() { accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME") if !ok { panic("AZURE_STORAGE_ACCOUNT_NAME could not be found") } blobName := "test_append_blob_seal.txt" blobURL := fmt.Sprintf("https://%s.blob.core.windows.net/testcontainer/%s", accountName, blobName)

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

appendBlobClient, err := appendblob.NewClient(blobURL, cred, nil)
handleError(err)

_, err = appendBlobClient.Seal(context.Background(), nil)
handleError(err)

}

Output:

This example shows how to set an expiry time on an existing blob This operation is only allowed on Hierarchical Namespace enabled accounts.

package main

import ( "context" "fmt" "log" "net/http" "os" "time"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/appendblob"

)

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

func main() { accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME") if !ok { panic("AZURE_STORAGE_ACCOUNT_NAME could not be found") } blobName := "test_append_blob_set_expiry.txt" blobURL := fmt.Sprintf("https://%s.blob.core.windows.net/testcontainer/%s", accountName, blobName)

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

appendBlobClient, err := appendblob.NewClient(blobURL, cred, nil)
handleError(err)

// set expiry on append blob to an absolute time
expiryTimeAbsolute := time.Now().Add(8 * time.Hour)
_, err = appendBlobClient.SetExpiry(context.TODO(), appendblob.ExpiryTypeAbsolute(expiryTimeAbsolute), nil)
handleError(err)

// validate set expiry operation
resp, err := appendBlobClient.GetProperties(context.TODO(), nil)
handleError(err)
if resp.ExpiresOn == nil || expiryTimeAbsolute.UTC().Format(http.TimeFormat) != resp.ExpiresOn.UTC().Format(http.TimeFormat) {
    return
}

}

Output:

This section is empty.

This section is empty.

This section is empty.

AppendBlockFromURLOptions contains the optional parameters for the Client.AppendBlockFromURL method.

AppendBlockFromURLResponse contains the response from method Client.AppendBlockFromURL.

AppendBlockOptions contains the optional parameters for the Client.AppendBlock method.

AppendBlockResponse contains the response from method Client.AppendBlock.

AppendPositionAccessConditions contains a group of parameters for the Client.AppendBlock method.

Client represents a client to an Azure Storage append 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.

AppendBlock writes a stream to a new block of data to the end of the existing append blob. 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/append-block.

BlobClient returns the embedded blob client for this AppendBlob client.

CopyFromURL Deprecated: CopyFromURL works only with block blob

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 append blob. It can only be used if the credential supplied during creation was a SharedKeyCredential.

SetTier Deprecated: SetTier only works for page blob in premium storage account and block blob in blob storage account.

URL returns the URL endpoint used by the Client object.

WithSnapshot creates a new AppendBlobURL 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 AppendBlobURL object identical to the source but with the specified version id. Pass "" to remove the versionID returning a URL to the base blob.

ClientOptions contains the optional parameters when creating a Client.

CreateOptions provides set of configurations for Create Append Blob operation

CreateResponse contains the response from method Client.Create.

ExpiryType defines values for ExpiryType

ExpiryTypeAbsolute defines the absolute time for the blob expiry

ExpiryTypeNever defines that the blob will be set to never expire

ExpiryTypeRelativeToCreation defines the duration relative to creation for the blob expiry

ExpiryTypeRelativeToNow defines the duration relative to now for the blob expiry

SealOptions provides set of configurations for SealAppendBlob operation

SealResponse contains the response from method Client.Seal.

SetExpiryOptions contains the optional parameters for the Client.SetExpiry method.

SetExpiryResponse contains the response from method Client.SetExpiry.