azure

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package azure is the Azure Blob Storage blob store (decision 0027). Blobs live at <prefix>sha256/<ab>/<hex> in one container.

Azure block blobs are written as staged blocks that a later Put Block List commits, and staged blocks are server-side state any process can commit, so an upload session maps onto them directly:

  • Appended bytes accumulate in a pending buffer of at most PartSize; each full part is staged as one block. A blob that never fills a part is written with a single Put Blob at commit.
  • Block IDs are derived from the upload ID and the block number, so the session only records how many blocks it staged, and re-staging a block after a failure is idempotent.
  • Staged blocks belong to one blob name and the namespace has no rename. When the client declared the digest up front the blocks are staged on the content key and commit needs no copy. Otherwise they are staged on a temporary blob, which commit finalises and then copies into the content key with parallel Put Block From URL ranges and one atomic Put Block List. Asynchronous Copy Blob is never used: it shows a zero-length blob at the destination while it runs, which would answer Exists for content that is not there.
  • Every commit to a content key carries If-None-Match: *, so a committed blob is never overwritten; losing that race to an identical upload is success.

Index

Constants

View Source
const (
	// DefaultPartSize keeps the pending tail a session stores small while
	// allowing blobs of about 390 GiB within Azure's 50,000 blocks.
	DefaultPartSize = 8 << 20
	// DefaultCopyRangeSize is the range each Put Block From URL copies: the
	// service's limit. The documentation gives both 100 MiB and 4,000 MiB;
	// the real service accepts 4,000 MiB (about 14 s server-side) and refuses
	// 4,001 MiB (research 18 §4), so a commit makes as few calls as it can.
	DefaultCopyRangeSize = 4000 << 20
	// DefaultConcurrency bounds the parallel Put Block From URL calls of one
	// commit.
	DefaultConcurrency = 8
)

Variables

View Source
var (
	// ErrConfig reports an unusable store configuration, including a
	// container that does not exist.
	ErrConfig = errors.New("blob/azure: invalid configuration")
	// ErrPrecondition reports a failed If-None-Match or If-Match condition:
	// at commit, the content key is already committed.
	ErrPrecondition = errors.New("blob/azure: precondition failed")
	// ErrBlockList reports a block list naming a block that is not staged,
	// which happens at commit when a concurrent commit of the same content
	// key discarded this session's blocks.
	ErrBlockList = errors.New("blob/azure: invalid block list")
	// ErrTooManyBlocks reports an upload that would need more blocks than
	// Azure allows; a larger PartSize admits it.
	ErrTooManyBlocks = errors.New("blob/azure: too many blocks")
)

Sentinel errors. Wrapped errors keep the API error; test with errors.Is.

View Source
var ErrSeek = errors.New("blob/azure: invalid seek")

ErrSeek reports an invalid Seek.

Functions

This section is empty.

Types

type API

type API interface {
	// StageBlock stages data as block id of name.
	StageBlock(ctx context.Context, name, id string, data []byte) error
	// StageBlockFromURL stages bytes [offset, offset+count) of the committed
	// blob src as block id of dst, provided src still has srcETag.
	StageBlockFromURL(
		ctx context.Context,
		dst, id, src string,
		offset, count int64,
		srcETag string,
	) error
	// CommitBlockList commits name from the staged blocks ids in order,
	// storing md5 as its content MD5; with ifNoneMatch it fails when name is
	// already committed. It returns the new ETag.
	CommitBlockList(
		ctx context.Context,
		name string,
		ids []string,
		md5 []byte,
		ifNoneMatch bool,
	) (string, error)
	// Upload writes name in one request; with ifNoneMatch it fails when name
	// is already committed.
	Upload(ctx context.Context, name string, data []byte, ifNoneMatch bool) error
	// Properties returns a committed blob's properties.
	Properties(ctx context.Context, name string) (Props, error)
	// Download streams count bytes of name from offset (0 means to the end),
	// failing if name no longer has etag.
	Download(
		ctx context.Context,
		name string,
		offset, count int64,
		etag string,
	) (io.ReadCloser, error)
	// Delete removes name, committed or only staged, with its snapshots.
	Delete(ctx context.Context, name string) error
	// List returns one page of committed blobs under prefix after marker,
	// and the marker of the next page ("" on the last).
	List(ctx context.Context, prefix, marker string) ([]Item, string, error)
	// SignRead returns a URL that reads name without credentials, as o
	// describes.
	SignRead(ctx context.Context, name string, o ReadSAS) (string, error)
	// Container names the container every name lives in, the first segment
	// of a blob's path on the service.
	Container() string
}

API is the blob-level surface the store uses, keyed by blob name within the store's container. NewAPI implements it over the Azure SDK; tests use a fake. Implementations report a missing blob as blob.ErrNotFound, an If-None-Match or If-Match failure as ErrPrecondition, a block list naming a block that is not staged as ErrBlockList, and a missing container as ErrConfig.

func NewAPI

func NewAPI(cfg ClientConfig) (API, error)

NewAPI builds the SDK client for cfg. It makes no request.

type ClientConfig

type ClientConfig struct {
	// ServiceURL is the Blob service endpoint, for example
	// "https://acct.blob.core.windows.net/" or, for Azurite,
	// "http://127.0.0.1:10000/devstoreaccount1/". Empty means the public
	// endpoint of Account.
	ServiceURL string
	// Container must already exist.
	Container string
	// Account names the storage account.
	Account string
	// AccountKey enables shared-key authentication and service SAS.
	AccountKey string
	// ConnectionString carries the endpoint and credentials together.
	ConnectionString string
	// SASToken authorises every request with a pre-issued SAS; presigning
	// is then unavailable.
	SASToken string
	// Credential is an Entra ID credential to use instead of the defaults.
	Credential azcore.TokenCredential
	// ManagedIdentityClientID selects a user-assigned managed identity.
	ManagedIdentityClientID string
	// SASStartOffset backdates SAS start times against clock skew; zero
	// means 15 minutes, as JFrog does.
	SASStartOffset time.Duration
	// Options configure the SDK pipeline (retries, transport).
	Options *container.ClientOptions
}

ClientConfig mirrors the blob.azure section of the application configuration. Exactly one way to authenticate applies, in this order: ConnectionString, AccountKey, SASToken, Credential, and otherwise Entra ID through managed identity (ManagedIdentityClientID) or DefaultAzureCredential.

func (ClientConfig) CanSign

func (c ClientConfig) CanSign() bool

CanSign reports whether a client built from c can sign read URLs, decided from the configuration alone so a store knows its routes when it is built (decision 0029): a shared key (given, or inside the connection string) signs service SAS, an Entra ID credential signs user delegation SAS, and a pre-issued SAS token cannot sign anything.

type Item

type Item struct {
	Name string
	Size int64
}

Item is one listed blob.

type Options

type Options struct {
	// Prefix is prepended to every blob name; a trailing slash is added.
	Prefix string
	// PartSize is the block size; zero means DefaultPartSize.
	PartSize int64
	// CopyRangeSize is the range one Put Block From URL copies; zero means
	// DefaultCopyRangeSize.
	CopyRangeSize int64
	// Concurrency bounds the parallel copies of one commit; zero means
	// DefaultConcurrency.
	Concurrency int
	// Sessions holds upload sessions between requests; session.InUploads
	// with a backend name such as "azure:<store name>" keeps them durable.
	Sessions session.Store
	// Presign makes the store sign storage URLs (blob.Signer). Set it from
	// ClientConfig.CanSign: a store on a pre-issued SAS token cannot sign.
	Presign bool
	// FrontDoor, when set with Presign, makes the store sign CDN URLs: a
	// window-aligned read SAS placed on the Front Door endpoint (decision
	// 0029).
	FrontDoor *cdn.FrontDoor
	// Now is the clock CDN windows are cut from; nil means time.Now.
	Now func() time.Time
}

Options configures New.

type Props

type Props struct {
	Size int64
	ETag string
	MD5  []byte
}

Props are a committed blob's properties.

type ReadSAS

type ReadSAS struct {
	TTL                time.Duration
	ContentDisposition string
	ContentType        string
	CacheControl       string
	// Start and Expiry, when set, fix the SAS's validity instead of now and
	// now plus TTL; the start is still backdated against clock skew. A CDN
	// URL fixes both to its window, so every SAS for a blob in that window
	// is the same string and the edge's cache, keyed on it, stays useful.
	Start  time.Time
	Expiry time.Time
}

ReadSAS describes a read SAS: how long it lasts and the response overrides (rscd, rsct, rscc) it carries, empty when not wanted.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is a blob.Store on an Azure Blob Storage container.

func New

func New(api API, o Options) (*Store, error)

New returns a store over api.

func (*Store) BeginUpload

func (s *Store) BeginUpload(ctx context.Context, opts ...blob.UploadOption) (blob.Upload, error)

BeginUpload starts a session.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, sha string) error

Delete removes the blob; deleting a missing blob succeeds.

func (*Store) Exists

func (s *Store) Exists(ctx context.Context, sha string) (bool, error)

Exists reports whether the blob is stored.

func (*Store) ObjectKey

func (s *Store) ObjectKey(sha string) string

ObjectKey is the blob name a blob is stored under in the container, prefix included, for signers that address it directly, such as a CDN. sha must be a valid digest (blob.ValidateSHA256).

func (*Store) Open

func (s *Store) Open(ctx context.Context, sha string) (io.ReadSeekCloser, blob.Info, error)

Open returns a reader that fetches ranges on demand, pinned to the blob's ETag.

func (*Store) PartSize

func (s *Store) PartSize() int64

PartSize returns the size of the blocks an upload is written in.

func (*Store) ResumeUpload

func (s *Store) ResumeUpload(ctx context.Context, id string) (blob.Upload, error)

ResumeUpload reopens a session.

func (*Store) SignURL

func (s *Store) SignURL(ctx context.Context, sha string, o blob.SignOptions) (string, error)

SignURL returns a time-limited read URL, carrying o's filename and media type as response overrides.

func (*Store) Signs

func (s *Store) Signs(via blob.Via) bool

Signs reports whether the store signs URLs for via: storage URLs when Options.Presign was set, and CDN URLs when it also has a Front Door. A store on a pre-issued SAS signs neither: Front Door passes the SAS through to storage, so a CDN URL needs a SAS the store can mint.

func (*Store) Stat

func (s *Store) Stat(ctx context.Context, sha string) (blob.Info, error)

Stat returns a blob's size and digest.

func (*Store) Walk

func (s *Store) Walk(ctx context.Context, fn func(blob.Info) error) error

Walk lists every blob under the prefix.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL