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 ¶
- type Object
- type Server
- func (s *Server) Client() *storage.Client
- func (s *Server) CreateBucket(name string)
- func (s *Server) CreateObject(obj Object)
- func (s *Server) GetObject(bucketName, objectName string) (Object, error)
- func (s *Server) ListObjects(bucketName, prefix, delimiter string) ([]Object, []string, error)
- func (s *Server) Stop()
- func (s *Server) URL() string
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"`
Content []byte `json:"-"`
}
Object represents the object that is stored within the fake server.
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 (
"fmt"
"io/ioutil"
"github.com/fsouza/fake-gcs-server/fakestorage"
"golang.org/x/net/context"
)
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 NewServerWithHostPort ¶
NewServerWithHostPort creates a new server that listens on a custom host and port
func (*Server) Client ¶
Client returns a GCS client configured to talk to the server.
Example ¶
package main
import (
"fmt"
"io/ioutil"
"github.com/fsouza/fake-gcs-server/fakestorage"
"golang.org/x/net/context"
)
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 ¶
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 ¶
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 ¶
GetObject returns the object with the given name in the given bucket, or an error if the object doesn't exist.
func (*Server) ListObjects ¶
ListObjects returns a sorted list of objects that match the given criteria, or an error if the bucket doesn't exist.