tessl install tessl/golang-github-com--azure--azure-sdk-for-go--sdk--storage--azblob@1.6.0Azure Blob Storage SDK for Go providing comprehensive blob storage operations including uploads, downloads, container management, and advanced features like leases and SAS generation
The Azure Blob Storage SDK for Go provides a comprehensive API for managing blob storage operations in Azure. It supports all blob types (block, append, and page blobs), container management, authentication methods, and advanced features like leases, SAS generation, and batch operations.
go get github.com/Azure/azure-sdk-for-go/sdk/storage/azblob@v1.6.4import (
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/service"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/appendblob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/pageblob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/lease"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/sas"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror"
)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"
)
func main() {
// Create client with Azure AD credential
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatal(err)
}
serviceURL := "https://<account>.blob.core.windows.net/"
client, err := azblob.NewClient(serviceURL, cred, nil)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
// Create a container
containerName := "mycontainer"
_, err = client.CreateContainer(ctx, containerName, nil)
if err != nil {
log.Fatal(err)
}
// Upload a blob from buffer
blobName := "myblob.txt"
data := []byte("Hello, Azure Blob Storage!")
_, err = client.UploadBuffer(ctx, containerName, blobName, data, nil)
if err != nil {
log.Fatal(err)
}
// Download blob to buffer
buffer := make([]byte, len(data))
_, err = client.DownloadBuffer(ctx, containerName, blobName, buffer, nil)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Downloaded: %s\n", string(buffer))
}The azblob SDK follows a hierarchical client structure:
Service → Container → Blob → Blob Type (Block/Append/Page)
azblob.Client): Convenience methods for common operationsservice.Client): Account-level operations, container managementcontainer.Client): Container-level operations, blob listingblob.Client): Generic blob operations (all blob types)blockblob.Client: Block blob operations (upload, stage blocks, commit)appendblob.Client: Append blob operations (create, append, seal)pageblob.Client: Page blob operations (create, upload pages, manage ranges)The SDK supports multiple authentication methods:
// Azure AD Token Credential (recommended)
func NewClient(serviceURL string, cred azcore.TokenCredential, options *ClientOptions) (*Client, error)
// Shared Key Credential
func NewClientWithSharedKeyCredential(serviceURL string, cred *SharedKeyCredential, options *ClientOptions) (*Client, error)
// Connection String
func NewClientFromConnectionString(connectionString string, options *ClientOptions) (*Client, error)
// No Credential (for anonymous access or SAS URLs)
func NewClientWithNoCredential(serviceURL string, options *ClientOptions) (*Client, error)type SharedKeyCredential = service.SharedKeyCredential
func NewSharedKeyCredential(accountName, accountKey string) (*SharedKeyCredential, error)The root azblob.Client provides convenient methods for common operations without navigating the client hierarchy.
Service clients manage account-level operations including properties, statistics, container listing, and batch operations.
type Client struct {
// contains filtered or unexported fields
}
// Get embedded service client
func (c *Client) ServiceClient() *service.ClientContainer clients manage container lifecycle, properties, metadata, access policies, and blob listing.
// Create container client from service
func (c *service.Client) NewContainerClient(containerName string) *container.Client
// Container operations
func (c *container.Client) Create(ctx context.Context, options *CreateOptions) (CreateResponse, error)
func (c *container.Client) Delete(ctx context.Context, options *DeleteOptions) (DeleteResponse, error)The blob client handles operations common to all blob types: properties, metadata, tags, downloads, copying, and deletion.
// Create blob client from container
func (c *container.Client) NewBlobClient(blobName string) *blob.Client
// Common blob operations
func (c *blob.Client) GetProperties(ctx context.Context, options *GetPropertiesOptions) (GetPropertiesResponse, error)
func (c *blob.Client) DownloadStream(ctx context.Context, o *DownloadStreamOptions) (DownloadStreamResponse, error)
func (c *blob.Client) Delete(ctx context.Context, o *DeleteOptions) (DeleteResponse, error)Block blobs support uploading large files by breaking them into blocks, which can be uploaded separately and then committed.
// Create block blob client
func (c *container.Client) NewBlockBlobClient(blobName string) *blockblob.Client
// Block blob operations
func (c *blockblob.Client) Upload(ctx context.Context, body io.ReadSeekCloser, options *UploadOptions) (UploadResponse, error)
func (c *blockblob.Client) StageBlock(ctx context.Context, base64BlockID string, body io.ReadSeekCloser, options *StageBlockOptions) (StageBlockResponse, error)
func (c *blockblob.Client) CommitBlockList(ctx context.Context, base64BlockIDs []string, options *CommitBlockListOptions) (CommitBlockListResponse, error)Append blobs are optimized for append operations, making them ideal for scenarios like logging.
// Create append blob client
func (c *container.Client) NewAppendBlobClient(blobName string) *appendblob.Client
// Append blob operations
func (c *appendblob.Client) Create(ctx context.Context, o *CreateOptions) (CreateResponse, error)
func (c *appendblob.Client) AppendBlock(ctx context.Context, body io.ReadSeekCloser, o *AppendBlockOptions) (AppendBlockResponse, error)
func (c *appendblob.Client) Seal(ctx context.Context, o *SealOptions) (SealResponse, error)Page blobs provide random read/write access for 512-byte pages, used for VHD files and other scenarios requiring random access.
// Create page blob client
func (c *container.Client) NewPageBlobClient(blobName string) *pageblob.Client
// Page blob operations
func (c *pageblob.Client) Create(ctx context.Context, size int64, o *CreateOptions) (CreateResponse, error)
func (c *pageblob.Client) UploadPages(ctx context.Context, body io.ReadSeekCloser, contentRange blob.HTTPRange, options *UploadPagesOptions) (UploadPagesResponse, error)
func (c *pageblob.Client) ClearPages(ctx context.Context, rnge blob.HTTPRange, options *ClearPagesOptions) (ClearPagesResponse, error)Leases provide exclusive locks on blobs and containers to prevent concurrent modifications.
// Create blob lease client
func NewBlobClient[T appendblob.Client | blob.Client | blockblob.Client | pageblob.Client](client *T, options *BlobClientOptions) (*BlobClient, error)
// Create container lease client
func NewContainerClient(client *container.Client, options *ContainerClientOptions) (*ContainerClient, error)
// Lease operations
func (c *lease.BlobClient) AcquireLease(ctx context.Context, duration int32, o *BlobAcquireOptions) (BlobAcquireResponse, error)
func (c *lease.BlobClient) ReleaseLease(ctx context.Context, o *BlobReleaseOptions) (BlobReleaseResponse, error)Shared Access Signatures (SAS) provide delegated access to storage resources with fine-grained control over permissions and expiry.
// Account-level SAS
type AccountSignatureValues struct {
Version string
Protocol Protocol
StartTime time.Time
ExpiryTime time.Time
Permissions AccountPermissions
IPRange IPRange
ResourceTypes AccountResourceTypes
EncryptionScope string
}
func (v AccountSignatureValues) SignWithSharedKey(sharedKeyCredential *SharedKeyCredential) (QueryParameters, error)
// Blob-level SAS
type BlobSignatureValues struct {
Version string
Protocol Protocol
StartTime time.Time
ExpiryTime time.Time
Permissions string
ContainerName string
BlobName string
Identifier string
// ... additional fields
}
func (v BlobSignatureValues) SignWithSharedKey(sharedKeyCredential *SharedKeyCredential) (QueryParameters, error)The bloberror package provides error codes and helpers for handling Azure Blob Storage errors.
// Check if error contains specific error code
func HasCode(err error, codes ...Code) bool
// Error codes
type Code string
const (
BlobAlreadyExists Code
BlobNotFound Code
ContainerAlreadyExists Code
ContainerNotFound Code
AuthenticationFailed Code
// ... 100+ additional error codes
)Parse and manipulate blob URLs:
// Parse blob URL into components
func ParseURL(u string) (URLParts, error)
type URLParts struct {
Scheme string
Host string
ContainerName string
BlobName string
Snapshot string
SAS sas.QueryParameters
VersionID string
UnparsedParams string
}
func (up URLParts) String() stringtype HTTPRange = blob.HTTPRange
// Represents a byte range for partial operations
type HTTPRange struct {
Offset int64
Count int64
}// Client-provided encryption key
type CPKInfo = blob.CPKInfo
type CPKInfo struct {
EncryptionKey *string
EncryptionKeySHA256 *string
EncryptionAlgorithm *EncryptionAlgorithmType
}
// Encryption scope
type CPKScopeInfo = container.CPKScopeInfo
type CPKScopeInfo struct {
EncryptionScope *string
}type AccessConditions = exported.BlobAccessConditions
type BlobAccessConditions struct {
ModifiedAccessConditions *ModifiedAccessConditions
LeaseAccessConditions *LeaseAccessConditions
}
type ModifiedAccessConditions struct {
IfMatch *azcore.ETag
IfNoneMatch *azcore.ETag
IfModifiedSince *time.Time
IfUnmodifiedSince *time.Time
IfTags *string
}
type LeaseAccessConditions struct {
LeaseID *string
}type PublicAccessType string
const (
PublicAccessTypeBlob PublicAccessType // Public read access for blobs only
PublicAccessTypeContainer PublicAccessType // Public read and list access
)
func PossiblePublicAccessTypeValues() []PublicAccessTypetype DeleteSnapshotsOptionType string
const (
DeleteSnapshotsOptionTypeInclude DeleteSnapshotsOptionType // Delete blob and all snapshots
DeleteSnapshotsOptionTypeOnly DeleteSnapshotsOptionType // Delete only snapshots
)
func PossibleDeleteSnapshotsOptionTypeValues() []DeleteSnapshotsOptionTypeAll operations return response types containing headers, metadata, and operation-specific data:
// Container responses
type CreateContainerResponse = service.CreateContainerResponse
type DeleteContainerResponse = service.DeleteContainerResponse
// Blob responses
type DeleteBlobResponse = blob.DeleteResponse
type DownloadStreamResponse = blob.DownloadStreamResponse
// Upload responses
type UploadBufferResponse = blockblob.UploadBufferResponse
type UploadFileResponse = blockblob.UploadFileResponse
type UploadStreamResponse = blockblob.UploadStreamResponse
// List responses
type ListBlobsFlatResponse = container.ListBlobsFlatResponse
type ListContainersResponse = service.ListContainersResponse// Container listing options
type ListContainersOptions = service.ListContainersOptions
type ListContainersInclude = service.ListContainersInclude
// Blob listing options
type ListBlobsFlatOptions = container.ListBlobsFlatOptions
type ListBlobsInclude = container.ListBlobsIncludetype ObjectReplicationPolicy = blob.ObjectReplicationPolicytype RetryReaderOptions = blob.RetryReaderOptions