fakestorage

package
v1.11.1 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2019 License: BSD-2-Clause Imports: 23 Imported by: 18

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 Object

type Object struct {
	BucketName  string `json:"-"`
	Name        string `json:"name"`
	ContentType string `json:"contentType"`
	Content     []byte `json:"-"`
	// Crc32c checksum of Content. calculated by server when it's upload methods are used.
	Crc32c  string `json:"crc32c,omitempty"`
	Md5Hash string `json:"md5hash,omitempty"`
}

Object represents the object that is stored within the fake server.

type Options added in v1.1.0

type Options struct {
	InitialObjects []Object
	StorageRoot    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>/<bucket>/<object>
	// If unset, the default is "storage.googleapis.com", the XML API
	PublicHost 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/ioutil"

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

func main() {
	server, err := fakestorage.NewServerWithHostPort([]fakestorage.Object{
		{
			BucketName: "some-bucket",
			Name:       "some/object/file.txt",
			Content:    []byte("inside the file"),
		},
	}, "127.0.0.1", 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 := ioutil.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

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

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

func NewServerWithOptions added in v1.1.0

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

NewServerWithOptions creates a new server with custom options

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/ioutil"

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

func main() {
	server := fakestorage.NewServer([]fakestorage.Object{
		{
			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 := ioutil.ReadAll(reader)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s", data)
}
Output:

inside the file

func (*Server) CreateBucket

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.

func (*Server) CreateObject

func (s *Server) CreateObject(obj Object)

CreateObject stores the given object internally.

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

func (*Server) GetObject

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

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

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) ListObjects

func (s *Server) ListObjects(bucketName, prefix, delimiter string) ([]Object, []string, error)

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

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.

Jump to

Keyboard shortcuts

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