fakestorage

package
v1.48.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2024 License: BSD-2-Clause Imports: 37 Imported by: 15

Documentation

Overview

Package fakestorage provides the server that can be used as a target on GCS-dependent tests.

The server provides a method that returns an instance of the storage client that can be used in tests.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommonPrefix added in v1.45.0

type CommonPrefix struct {
	Prefix string `xml:"Prefix"`
}

type Contents added in v1.45.0

type Contents struct {
	XMLName      xml.Name `xml:"Contents"`
	Key          string   `xml:"Key"`
	Generation   int64    `xml:"Generation"`
	LastModified string   `xml:"LastModified"`
	ETag         ETag
	Size         int64 `xml:"Size"`
}

type CreateBucketOpts added in v1.16.0

type CreateBucketOpts struct {
	Name                  string
	VersioningEnabled     bool
	DefaultEventBasedHold bool
}

CreateBucketOpts defines the properties of a bucket you can create with CreateBucketWithOpts.

type ETag added in v1.45.0

type ETag struct {
	Value string `xml:",innerxml"`
}

func (*ETag) Equals added in v1.45.0

func (e *ETag) Equals(etag string) bool

type ListBucketResult added in v1.45.0

type ListBucketResult struct {
	XMLName        xml.Name       `xml:"ListBucketResult"`
	Name           string         `xml:"Name"`
	CommonPrefixes []CommonPrefix `xml:"CommonPrefixes,omitempty"`
	Delimiter      string         `xml:"Delimiter"`
	Prefix         string         `xml:"Prefix"`
	KeyCount       int            `xml:"KeyCount"`
	Contents       []Contents     `xml:"Contents"`
}

type ListOptions added in v1.28.0

type ListOptions struct {
	Prefix                   string
	Delimiter                string
	Versions                 bool
	StartOffset              string
	EndOffset                string
	IncludeTrailingDelimiter bool
}

type Object

type Object struct {
	ObjectAttrs
	Content []byte `json:"-"`
}

Object represents an object that is stored within the fake server. The content of this type is stored is buffered, i.e. it's stored in memory. Use StreamingObject to stream the content from a reader, e.g a file.

func (Object) StreamingObject added in v1.40.0

func (o Object) StreamingObject() StreamingObject

type ObjectAttrs added in v1.30.0

type ObjectAttrs struct {
	BucketName      string
	Name            string
	Size            int64
	ContentType     string
	ContentEncoding string
	// Crc32c checksum of Content. calculated by server when it's upload methods are used.
	Crc32c  string
	Md5Hash string
	Etag    string
	ACL     []storage.ACLRule
	// Dates and generation can be manually injected, so you can do assertions on them,
	// or let us fill these fields for you
	Created    time.Time
	Updated    time.Time
	Deleted    time.Time
	CustomTime time.Time
	Generation int64
	Metadata   map[string]string
}

ObjectAttrs returns only the meta-data about an object without its contents.

func (ObjectAttrs) MarshalJSON added in v1.40.0

func (o ObjectAttrs) MarshalJSON() ([]byte, error)

MarshalJSON for ObjectAttrs to use ACLRule instead of storage.ACLRule

func (*ObjectAttrs) UnmarshalJSON added in v1.40.0

func (o *ObjectAttrs) UnmarshalJSON(data []byte) error

UnmarshalJSON for ObjectAttrs to use ACLRule instead of storage.ACLRule

type Options added in v1.1.0

type Options struct {
	InitialObjects []Object
	StorageRoot    string
	Seed           string
	Scheme         string
	Host           string
	Port           uint16

	// when set to true, the server will not actually start a TCP listener,
	// client requests will get processed by an internal mocked transport.
	NoListener bool

	// Optional external URL, such as https://gcs.127.0.0.1.nip.io:4443
	// Returned in the Location header for resumable uploads
	// The "real" value is https://www.googleapis.com, the JSON API
	// The default is whatever the server is bound to, such as https://0.0.0.0:4443
	ExternalURL string

	// Optional URL for public access
	// An example is "storage.gcs.127.0.0.1.nip.io:4443", which will configure
	// the server to serve objects at:
	// https://storage.gcs.127.0.0.1.nip.io:4443/<bucket>/<object>
	// https://<bucket>.storage.gcs.127.0.0.1.nip.io:4443>/<object>
	// If unset, the default is "storage.googleapis.com", the XML API
	PublicHost string

	// Optional list of headers to add to the CORS header allowlist
	// An example is "X-Goog-Meta-Uploader", which will allow a
	// custom metadata header named "X-Goog-Meta-Uploader" to be
	// sent through the browser
	AllowedCORSHeaders []string

	// Destination for writing log.
	Writer io.Writer

	// EventOptions contains the events that should be published and the URL
	// of the Google cloud function such events should be published to.
	EventOptions notification.EventManagerOptions

	// Location used for buckets in the server.
	BucketsLocation string

	CertificateLocation string

	PrivateKeyLocation string
}

Options are used to configure the server on creation.

type Server

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

Server is the fake server.

It provides a fake implementation of the Google Cloud Storage API.

Example (With_host_port)
package main

import (
	"context"
	"fmt"
	"io"

	"github.com/fsouza/fake-gcs-server/fakestorage"
)

func main() {
	server, err := fakestorage.NewServerWithOptions(fakestorage.Options{
		InitialObjects: []fakestorage.Object{
			{
				ObjectAttrs: fakestorage.ObjectAttrs{
					BucketName: "some-bucket",
					Name:       "some/object/file.txt",
				},
				Content: []byte("inside the file"),
			},
		},
		Host: "127.0.0.1",
		Port: 8081,
	})
	if err != nil {
		panic(err)
	}
	defer server.Stop()
	client := server.Client()
	object := client.Bucket("some-bucket").Object("some/object/file.txt")
	reader, err := object.NewReader(context.Background())
	if err != nil {
		panic(err)
	}
	defer reader.Close()
	data, err := io.ReadAll(reader)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s", data)
}
Output:

inside the file

func NewServer

func NewServer(objects []Object) *Server

NewServer creates a new instance of the server, pre-loaded with the given objects.

func NewServerWithHostPort deprecated

func NewServerWithHostPort(objects []Object, host string, port uint16) (*Server, error)

NewServerWithHostPort creates a new server that listens on a custom host and port

Deprecated: use NewServerWithOptions.

func NewServerWithOptions added in v1.1.0

func NewServerWithOptions(options Options) (*Server, error)

NewServerWithOptions creates a new server configured according to the provided options.

func (*Server) Backend added in v1.45.0

func (s *Server) Backend() backend.Storage

func (*Server) Client

func (s *Server) Client() *storage.Client

Client returns a GCS client configured to talk to the server.

Example
package main

import (
	"context"
	"fmt"
	"io"

	"github.com/fsouza/fake-gcs-server/fakestorage"
)

func main() {
	server := fakestorage.NewServer([]fakestorage.Object{
		{
			ObjectAttrs: fakestorage.ObjectAttrs{
				BucketName: "some-bucket",
				Name:       "some/object/file.txt",
			},
			Content: []byte("inside the file"),
		},
	})
	defer server.Stop()
	client := server.Client()
	object := client.Bucket("some-bucket").Object("some/object/file.txt")
	reader, err := object.NewReader(context.Background())
	if err != nil {
		panic(err)
	}
	defer reader.Close()
	data, err := io.ReadAll(reader)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s", data)
}
Output:

inside the file

func (*Server) CreateBucket deprecated

func (s *Server) CreateBucket(name string)

CreateBucket creates a bucket inside the server, so any API calls that require the bucket name will recognize this bucket.

If the bucket already exists, this method does nothing.

Deprecated: use CreateBucketWithOpts.

func (*Server) CreateBucketWithOpts added in v1.16.0

func (s *Server) CreateBucketWithOpts(opts CreateBucketOpts)

CreateBucketWithOpts creates a bucket inside the server, so any API calls that require the bucket name will recognize this bucket. Use CreateBucketOpts to customize the options for this bucket

If the underlying backend returns an error, this method panics.

func (*Server) CreateObject

func (s *Server) CreateObject(obj Object)

CreateObject is the non-streaming version of CreateObjectStreaming.

In addition to streaming, CreateObjectStreaming returns an error instead of panicking when an error occurs.

func (*Server) CreateObjectStreaming added in v1.40.0

func (s *Server) CreateObjectStreaming(obj StreamingObject) error

CreateObjectStreaming stores the given object internally.

If the bucket within the object doesn't exist, it also creates it. If the object already exists, it overwrites the object.

func (*Server) GetObject

func (s *Server) GetObject(bucketName, objectName string) (Object, error)

GetObject is the non-streaming version of GetObjectStreaming.

func (*Server) GetObjectStreaming added in v1.40.0

func (s *Server) GetObjectStreaming(bucketName, objectName string) (StreamingObject, error)

GetObjectStreaming returns the object with the given name in the given bucket, or an error if the object doesn't exist.

func (*Server) GetObjectWithGeneration added in v1.16.0

func (s *Server) GetObjectWithGeneration(bucketName, objectName string, generation int64) (Object, error)

GetObjectWithGeneration is the non-streaming version of GetObjectWithGenerationStreaming.

func (*Server) GetObjectWithGenerationStreaming added in v1.40.0

func (s *Server) GetObjectWithGenerationStreaming(bucketName, objectName string, generation int64) (StreamingObject, error)

GetObjectWithGenerationStreaming returns the object with the given name and given generation ID in the given bucket, or an error if the object doesn't exist.

If versioning is enabled, archived versions are considered.

func (*Server) HTTPClient added in v1.4.0

func (s *Server) HTTPClient() *http.Client

HTTPClient returns an HTTP client configured to talk to the server.

func (*Server) HTTPHandler added in v1.45.1

func (s *Server) HTTPHandler() http.Handler

HTTPHandler returns an HTTP handler that behaves like GCS.

func (*Server) ListObjects deprecated

func (s *Server) ListObjects(bucketName, prefix, delimiter string, versions bool) ([]ObjectAttrs, []string, error)

ListObjects returns a sorted list of objects that match the given criteria, or an error if the bucket doesn't exist.

Deprecated: use ListObjectsWithOptions.

func (*Server) ListObjectsWithOptions added in v1.28.0

func (s *Server) ListObjectsWithOptions(bucketName string, options ListOptions) ([]ObjectAttrs, []string, error)

func (*Server) PublicURL added in v1.9.0

func (s *Server) PublicURL() string

PublicURL returns the server's public download URL.

func (*Server) Stop

func (s *Server) Stop()

Stop stops the server, closing all connections.

func (*Server) URL

func (s *Server) URL() string

URL returns the server URL.

type StreamingObject added in v1.40.0

type StreamingObject struct {
	ObjectAttrs
	Content io.ReadSeekCloser `json:"-"`
}

StreamingObject is the streaming version of Object.

func (*StreamingObject) BufferedObject added in v1.40.0

func (o *StreamingObject) BufferedObject() (Object, error)

func (*StreamingObject) Close added in v1.40.0

func (o *StreamingObject) Close() error

Jump to

Keyboard shortcuts

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