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:
- func NewClient(blobURL string, cred azcore.TokenCredential, options *ClientOptions) (*Client, error)
- func NewClientFromConnectionString(connectionString, containerName, blobName string, options *ClientOptions) (*Client, error)
- func NewClientWithNoCredential(blobURL string, options *ClientOptions) (*Client, error)
- func NewClientWithSharedKeyCredential(blobURL string, cred *blob.SharedKeyCredential, options *ClientOptions) (*Client, error)
- func (ab *Client) AbortCopyFromURL(ctx context.Context, copyID string, o *blob.AbortCopyFromURLOptions) (blob.AbortCopyFromURLResponse, error)
- func (ab *Client) AppendBlock(ctx context.Context, body io.ReadSeekCloser, o *AppendBlockOptions) (AppendBlockResponse, error)
- func (ab *Client) AppendBlockFromURL(ctx context.Context, source string, o *AppendBlockFromURLOptions) (AppendBlockFromURLResponse, error)
- func (ab *Client) BlobClient() *blob.Client
- func (ab *Client) CopyFromURL(ctx context.Context, copySource string, o *blob.CopyFromURLOptions) (blob.CopyFromURLResponse, error)
- func (ab *Client) Create(ctx context.Context, o *CreateOptions) (CreateResponse, error)
- func (ab *Client) CreateSnapshot(ctx context.Context, o *blob.CreateSnapshotOptions) (blob.CreateSnapshotResponse, error)
- func (ab *Client) Delete(ctx context.Context, o *blob.DeleteOptions) (blob.DeleteResponse, error)
- func (ab *Client) DeleteImmutabilityPolicy(ctx context.Context, options *blob.DeleteImmutabilityPolicyOptions) (blob.DeleteImmutabilityPolicyResponse, error)
- func (ab *Client) DownloadBuffer(ctx context.Context, buffer []byte, o *blob.DownloadBufferOptions) (int64, error)
- func (ab *Client) DownloadFile(ctx context.Context, file *os.File, o *blob.DownloadFileOptions) (int64, error)
- func (ab *Client) DownloadStream(ctx context.Context, o *blob.DownloadStreamOptions) (blob.DownloadStreamResponse, error)
- func (ab *Client) GetAccountInfo(ctx context.Context, o *blob.GetAccountInfoOptions) (blob.GetAccountInfoResponse, error)
- func (ab *Client) GetProperties(ctx context.Context, o *blob.GetPropertiesOptions) (blob.GetPropertiesResponse, error)
- func (ab *Client) GetSASURL(permissions sas.BlobPermissions, expiry time.Time, o *blob.GetSASURLOptions) (string, error)
- func (ab *Client) GetTags(ctx context.Context, o *blob.GetTagsOptions) (blob.GetTagsResponse, error)
- func (ab *Client) Seal(ctx context.Context, o *SealOptions) (SealResponse, error)
- func (ab *Client) SetExpiry(ctx context.Context, expiryType ExpiryType, o *SetExpiryOptions) (SetExpiryResponse, error)
- func (ab *Client) SetHTTPHeaders(ctx context.Context, httpHeaders blob.HTTPHeaders, ...) (blob.SetHTTPHeadersResponse, error)
- func (ab *Client) SetImmutabilityPolicy(ctx context.Context, expiryTime time.Time, ...) (blob.SetImmutabilityPolicyResponse, error)
- func (ab *Client) SetLegalHold(ctx context.Context, legalHold bool, options *blob.SetLegalHoldOptions) (blob.SetLegalHoldResponse, error)
- func (ab *Client) SetMetadata(ctx context.Context, metadata map[string]*string, o *blob.SetMetadataOptions) (blob.SetMetadataResponse, error)
- func (ab *Client) SetTags(ctx context.Context, tags map[string]string, o *blob.SetTagsOptions) (blob.SetTagsResponse, error)
- func (ab *Client) SetTier(ctx context.Context, tier blob.AccessTier, o *blob.SetTierOptions) (blob.SetTierResponse, error)
- func (ab *Client) StartCopyFromURL(ctx context.Context, copySource string, o *blob.StartCopyFromURLOptions) (blob.StartCopyFromURLResponse, error)
- func (ab *Client) URL() string
- func (ab *Client) Undelete(ctx context.Context, o *blob.UndeleteOptions) (blob.UndeleteResponse, error)
- func (ab *Client) WithSnapshot(snapshot string) (*Client, error)
- func (ab *Client) WithVersionID(versionID string) (*Client, error)
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.
- blobURL - the URL of the blob e.g. https://.blob.core.windows.net/container/blob.txt
- cred - an Azure AD credential, typically obtained via the azidentity module
- options - client options; pass nil to accept the default values
func NewClientFromConnectionString(connectionString, containerName, blobName string, options ClientOptions) (Client, error)
NewClientFromConnectionString creates an instance of Client with the specified values.
- connectionString - a connection string for the desired storage account
- containerName - the name of the container within the storage account
- blobName - the name of the blob within the container
- options - client options; pass nil to accept the default 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.
- blobURL - the URL of the blob e.g. https://.blob.core.windows.net/container/blob.txt?
- options - client options; pass nil to accept the default values
NewClientWithSharedKeyCredential creates an instance of Client with the specified values.
- blobURL - the URL of the blob e.g. https://.blob.core.windows.net/container/blob.txt
- cred - a SharedKeyCredential created with the matching blob's storage account and access key
- options - client options; pass nil to accept the default 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.