azureblob

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package azureblob is a storage.Store on one container of Azure Blob Storage with a flat namespace: every object is a block blob named by its key.

Client and credentials

New takes a container.Client that the caller has built for the container, so the package does not choose a credential. With Entra ID, including workload identity, build the client with container.NewClient and any azcore.TokenCredential; with a shared key or a SAS, use the other constructors of package container. The container must exist, and nothing else may write to it. The client's transport must pass the request's context on to net/http, as the SDK's default transport, any *http.Transport and any *http.Client do: the backend proves that a request was sent once from net/http's client trace (see "Outcomes"). A transport that drops the context is not supported. Through it, a credential that fetches its token over HTTP with the call's context puts its own connections in the trace, and a failed mutation that took effect can then be reported as having none. The transport must also not send a request again on its own; net/http's own re-sends show in the trace. A refused connection is a definite error only if the transport returns net/http's dial error. The transport must also close an idle connection before the service, or anything between (a load balancer, a proxy), does: set http.Transport.IdleConnTimeout below the shortest idle timeout on the path (the SDK's default transport keeps idle connections 90 s; Azurite closes them after 5 s). A mutation is never retried, so one sent on a pooled connection that the other end is closing fails, often with an unknown outcome.

Writes

Create and Replace each publish with a single Put Blob request; the backend never stages blocks. Create sends If-None-Match: *, so the service creates the blob only if none exists; Replace sends If-Match with the version token, so the service overwrites it only if its ETag still matches. Both conditions are evaluated by the service inside that one request, so among concurrent writers exactly one succeeds. Staged blocks are avoided because Azurite counts a blob that has only uncommitted blocks as existing for If-None-Match: *, so an interrupted staged upload could block a later Create, and the real service's behaviour there is not documented.

Put Blob needs the body's length before it is sent, and a write must read its body to its end before it publishes anything, so every write first reads the whole body. Up to 4 MiB of it is held in memory; a longer body is written to a spool file in the directory given to New instead, so a write holds at most 4 MiB (plus a 32 KiB copy buffer) of its body in memory whatever its size. The spool file is unlinked right after it is created and closed when the write returns, on every path, so it never outlives the write, even if the process is killed (this relies on a Linux or macOS filesystem, where an open file can be unlinked). A write holds no lock while it reads its body, so it blocks no other call.

A body longer than 5,000 MiB, the largest Put Blob the service accepts, is rejected with storage.ErrInvalidArgument before anything is sent.

Version tokens

The version token is the blob's ETag, as the service returns it, quotes included. Replace sends it unchanged in If-Match, so the token and the write condition name the same thing. A version that is not a quoted entity tag ("*" and an empty string included) can never be a token this backend returned, so Replace fails its precondition without sending a request.

The service documents that a blob's ETag changes on every write. The storage contract also needs an ETag never to recur for a key, also across a Delete and a later Create of the same name. The backend uses the ETag as the token on the assumption, not a documented guarantee, that the service never issues the same ETag twice for one blob name. That has not been verified against the service. If it proves false, the backend must change to a per-write identity stored with the blob and compared under a blob lease. Azurite derives its ETags from the time in milliseconds multiplied by a random factor, so on the emulator a repeat is possible, though unlikely, for two writes in the same millisecond.

Outcomes

The SDK does not retry a mutation: Create, Replace and Delete run with azcore retries disabled for that call (its bearer token policy still sends the request again after a 401 challenge it handled, which shows in the trace), so a lost response is never turned into a precondition failure by a retry that meets the write's own effect. net/http itself can send a request again, and an answer or a failed dial may belong to that second attempt, after a first that took effect: over HTTP/2 it re-sends a request whose stream the server reset with PROTOCOL_ERROR, which does not say whether the server processed it, and it retries on a new connection a request that it wrote nothing of on a reused one. The backend proves a single attempt from net/http's client trace of the call's context: the connections net/http hands the request (GotConn) and the connections it asks for (GetConn). A failed mutation is definite, with no effect, only when the error and that trace prove it:

  • if the transport did not read a write's body to its end, no request with the whole body was completed, so the service cannot have applied it (a 409 or 412 that answers a re-send whose body was not read to its end is therefore a definite error, but not storage.ErrPreconditionFailed);
  • the SDK failed before its transport with an error azcore marks non-retriable, and the trace saw at most one connection handed to the request and none asked for after it: its bearer token policy marks so, before the transport, a credential that got no token and its refusal to send a token over plain HTTP, and after it only a failed 401 challenge, which has no effect unless it answers a re-send;
  • the trace saw exactly one connection handed to the request and none asked for after it, and the service answered with a status that refuses the request: 409 BlobAlreadyExists or 412 (Create or Replace), or 404 BlobNotFound (Replace), is storage.ErrPreconditionFailed, and any other 4xx except 408 is a definite error (the status comes from the *azcore.ResponseError in the error). The backend assumes that a 4xx other than 408 comes from the service, or from an intermediary that refused the request itself, never from one that forwarded the request and replaced the answer;
  • the trace saw a connection asked for and none handed to the request, and the error holds net/http's *net.OpError for a dial ("dial", or "proxyconnect" through a proxy): no connection was made, so nothing of the request was sent.

A credential that calls its token endpoint with the call's context shares the trace, so its connections count too. With a transport that passes the context on, they can only turn an answer into unknown.

Everything else wraps storage.ErrOutcomeUnknown: a lost response, a reset connection, EOF, a body that fails after a response arrived, a header that does not parse, the context ending, any other transport error, a 5xx, 408, and a 2xx or 3xx other than the one the request expects (from a non-conforming proxy). Some of these come from requests that were never sent, or that had no effect, which the contract allows when the backend cannot tell: for Delete and a write with an empty body when the context ends while a credential waits for a token, or before the transport dials (the SDK then returns the context's error alone); when a credential fails with an error azcore does not mark non-retriable, or with one it does mark after its own connections put more than one in the trace, or the SDK fails before its transport with such an error, for example a shared key that cannot sign the request; when a connection fails with an error other than net/http's dial error, such as a TLS handshake failure or any failure of a transport that hides net/http's errors; when a credential's own connection in the call's trace makes a refused connection look like a re-send; when net/http retries, on a new connection, a request it wrote nothing of on a reused one (an HTTP/2 connection found unusable, or one the server closed with GOAWAY), and the new dial is refused; and, through a transport that does not pass the request's context on, for a refused connection and for any 4xx answer.

Once a mutation call returns, its request body refuses further reads, so a request that net/http is still sending when the call returns can never be completed and applied afterwards.

Delete of a missing blob (404 BlobNotFound) succeeds. A blob that has snapshots cannot be deleted; this backend never creates snapshots.

Reads (Get, GetRange, Head and List) keep the client's retry policy, since repeating them has no effect on the store.

Reads

Get is one Get Blob request and GetRange one ranged Get Blob, so a body and its Info come from one response and therefore one version. A GetRange of length 0 is a Get Blob Properties request. The service answers a range starting at or past the end with 416; the backend then reads the blob's properties and returns an empty body if the offset is still at or past the end, or repeats the ranged read if the blob has grown in between.

Listing

List uses List Blobs with the prefix and follows NextMarker until it has limit entries or the listing ends, since the service may return fewer entries than requested. List Blobs has no start-after parameter for a flat namespace, so the backend skips the entries up to startAfter itself: shortcut: a List with startAfter reads every key under the prefix up to startAfter, up to 5,000 per request; if that becomes a hot path, use the service's startFrom parameter once it is verified for flat namespaces.

Errors

Errors from the service keep only the HTTP status and Azure's error code. Other SDK and transport errors keep their text with every URL query string removed, since a SAS URL carries its signature there.

Tested

Only against Azurite 3.37.0, with its well-known development account and a container SAS, and token credentials against local HTTP servers: one that fails without a request, some that call a local token endpoint, and one that gets a token for a server whose answers' bodies fail, and transports that refuse a connection, report a re-send in the trace, or drop the trace while a credential fails to reach a closed proxy, and a local HTTP/2 server that resets a request's stream with PROTOCOL_ERROR after applying it. The server's acceptance suite has been run once by hand against the real service with an Entra ID service principal; this package's own tests have not, and real throttling, HTTP/2 against the service and a real proxy are not tested.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

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

Store is a storage.Store on one Azure Blob Storage container.

func New

func New(c *container.Client, spoolDir string) *Store

New returns a Store on the container that c addresses. Bodies longer than 4 MiB are spooled to files in spoolDir, or in os.TempDir if it is empty.

func (*Store) Create

func (s *Store) Create(ctx context.Context, key string, body io.Reader) (string, error)

Create implements storage.Store.

func (*Store) Delete

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

Delete implements storage.Store.

func (*Store) Get

func (s *Store) Get(ctx context.Context, key string) (io.ReadCloser, storage.Info, error)

Get implements storage.Store.

func (*Store) GetRange

func (s *Store) GetRange(ctx context.Context, key string, offset, length int64) (io.ReadCloser, storage.Info, error)

GetRange implements storage.Store.

func (*Store) Head

func (s *Store) Head(ctx context.Context, key string) (storage.Info, error)

Head implements storage.Store.

func (*Store) List

func (s *Store) List(ctx context.Context, prefix, startAfter string, limit int) ([]storage.Entry, error)

List implements storage.Store.

func (*Store) Replace

func (s *Store) Replace(ctx context.Context, key, version string, body io.Reader) (string, error)

Replace implements storage.Store.

Jump to

Keyboard shortcuts

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