gcs

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 19 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 gcs is a storage.Store on a Google Cloud Storage bucket, through the JSON API and the official client, cloud.google.com/go/storage.

Mapping

A key is the object name, unchanged. The version token is the object's generation, in decimal. Google documents that a generation is unique to one version of an object and that a new object with the same name has a different generation, so a token never repeats for a key. The store never changes object metadata, so the metageneration plays no part.

Create uploads with ifGenerationMatch=0 and Replace with ifGenerationMatch=<generation>; the service answers 412 with reason conditionNotMet when the condition does not hold. Exactly one of concurrent Creates of a key, or of Replaces from one version, succeeds across all processes and hosts that share the bucket if the service checks the condition and commits the object in one step. For a resumable upload (below) that is an assumption, observed on fake-gcs-server and not documented by Google; see Limits. A Replace token that is not a generation this store could have returned fails its precondition without a request. Delete is unconditional and treats 404 as success.

Uploads

A write streams its body through the client's Writer in chunks of ChunkSize bytes. A body that ends within the first chunk is sent in one request (uploadType=multipart) once the body has ended. A longer body goes through a resumable upload session: every chunk but the last is sent while the body is read, and the object exists only once the last request of the session, sent after the body has ended, completes. Memory per write is bounded by one chunk plus a copy buffer: ChunkSize + 256 KiB. A body with a Len method (bytes.Reader, strings.Reader, bytes.Buffer) shorter than a chunk gets a chunk of its length plus one, which the client rounds up to a multiple of 256 KiB. There is no size limit beyond the service's own (5 TiB per object).

The request that can make a write take effect is the commit request: the single-request upload, the last request of a resumable session (whose Content-Range names the total size), or the DELETE. The store marks, in its own http.RoundTripper, below authentication, when a call's commit request starts, so a failed token fetch is an error before that point. An error before that point means the call had no effect; an error after it wraps storage.ErrOutcomeUnknown unless the service answered 412 with reason conditionNotMet (precondition failed) or another 4xx other than 408, or the connection was never made. A 412 with another reason, such as orgPolicyConstraintFailed, is the service's error, with no effect.

Retries

The client retries on its own. The store allows that only where a retry cannot change the outcome: reads, listings, and the request that opens a resumable session, which publishes nothing and carries none of the body. The client never retries a commit request, so a lost response is reported as storage.ErrOutcomeUnknown. net/http can still send a request twice on its own, below the store's transport: 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. The store counts the attempts net/http makes at a commit request (each connection it hands the request, and each connection it asks for after one); after more than one, every error of the call, a 412, another 4xx or a refused connection included, wraps storage.ErrOutcomeUnknown, because the answer may be to the re-send of an attempt that took effect. The client does not retry a chunk of a resumable session either: a retried chunk whose first attempt the emulator had stored was stored twice, giving an object longer than the body, and the service's documentation does not say how it treats a chunk it already has. A failed chunk fails the write, with no effect.

Configuration

Open takes a bucket, which must exist, and an optional endpoint. The credentials keep the values of Open's context but not its cancellation, so the Store keeps working after that context ends. Without an endpoint the store uses Application Default Credentials with the devstorage.read_write scope: a GKE workload identity, a service-account key named by GOOGLE_APPLICATION_CREDENTIALS, or the metadata server. With an endpoint, such as an emulator's, it sends no credentials. The client reads STORAGE_EMULATOR_HOST from the environment; Open refuses to run with credentials while it is set, so that they are never sent to the emulator.

Limits

  • Tested against the fake-gcs-server emulator only (see the package tests), not against the real service.
  • Exactly one winner among concurrent conditional writes of ChunkSize bytes or more assumes that the service evaluates ifGenerationMatch at the last request of a resumable session, as the emulator does. Google does not document when it does; if it checked only when the session opens, two writers could both succeed. This must be checked against the real service before the server may use this backend.
  • Version tokens of one bucket are not portable to another.
  • The bucket must not have object versioning, retention policies or holds that make a Delete fail; a Delete that the service refuses is returned as an error.

Index

Constants

View Source
const ChunkSize = 16 << 20

ChunkSize is the size of the chunks of a resumable upload, the client's default. A body of at most ChunkSize bytes is uploaded in one request.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Bucket is the name of an existing bucket.
	Bucket string
	// Endpoint, if set, is the JSON API base URL, for example
	// "http://127.0.0.1:4443/storage/v1/" for an emulator. Requests to it carry
	// no credentials. If empty, the store uses Google's endpoint and
	// Application Default Credentials.
	Endpoint string
}

Config configures a Store.

type Store

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

Store is a storage.Store on one bucket. It is safe for concurrent use.

func Open

func Open(ctx context.Context, cfg Config) (*Store, error)

Open returns a Store on cfg.Bucket. It does not contact the service. Its HTTP transport, a clone of http.DefaultTransport, waits storage.ResponseHeaderTimeout for each response's headers.

func (*Store) Close

func (s *Store) Close() error

Close releases the client's resources.

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. The service may return short pages; the client's iterator keeps requesting pages until limit entries have been collected or the listing ends.

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