commonblobgo

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2020 License: Apache-2.0 Imports: 20 Imported by: 2

README

Build Status

common-blob-go

Go library to work with AWS(amazon web services) S3 and GCP(google cloud platform) cloud storage

Usage

Install
go get -u github.com/AccelByte/common-blob-go
Importing
commonblobgo "github.com/AccelByte/common-blob-go"

To create a new storage client, use this function:

storage, err := storage, err := NewCloudStorage(
    ctx,
    isTesting,
    bucketProvider,
    bucketName,
    awsS3Endpoint,
    awsS3Region,
    awsS3AccessKeyID,
    awsS3SecretAccessKey,
    gcpCredentialsJSON,
    gcpStorageEmulatorHost,
)

NewCloudStorage requires such parameters :

  • ctx context.Context : a context that could be cancelled to force-stop the initialization

  • isTesting bool : a flag to switch between external and in-docker-compose dependencies. Used from tests

  • bucketProvider string : provider type. Could be aws or gcp

  • bucketName string : the name of a bucket

  • awsS3Endpoint string : S3 endpoint. Used only from tests(required if bucketProvider==aws and isTesting == true)

  • awsS3Region string : S3 region(required if bucketProvider==aws)

  • awsS3AccessKeyID string : S3 Access key(required if bucketProvider==aws)

  • awsS3SecretAccessKey string : S3 secret key(required if bucketProvider==aws)

  • gcpCredentialsJSON string : GCP JSON credentials(required if bucketProvider==gcp)

  • gcpStorageEmulatorHost string : GCP storage host. Used only from tests(required if bucketProvider==gcp and isTesting == true)

Available methods :
type CloudStorage interface {
	List(ctx context.Context, prefix string) *ListIterator // iterate over all objects in the folder
	Get(ctx context.Context, key string) ([]byte, error) // get the object by a name
	GetReader(ctx context.Context, key string) (io.ReadCloser, error) // get reader to operate with io.ReadCloser
	Delete(ctx context.Context, key string) error // delete the object by a name
	CreateBucket(ctx context.Context, bucketPrefix string, expirationTimeDays int64) error // create a bucket. Used only from tests
	Close() // close connection
	GetSignedURL(ctx context.Context, key string, expiry time.Duration) (string, error) // create signed URL
	Write(ctx context.Context, key string, body []byte, contentType *string) error // write the object a file-name
	GetWriter(ctx context.Context, key string) (io.WriteCloser, error) // get writer to operate with io.WriteCloser
	Attributes(ctx context.Context, key string) (*Attributes, error) // get object attributes
}
Examples:
List(ctx context.Context, prefix string) *ListIterator
    list := storage.List(ctx, bucketPrefix)

    for {
        item, err := list.Next(ctx)
        if err == io.EOF {
            break // no more object
        }

        // ...

        if item.Key == fileName {
            fileFound = true
        }
    }
Get(ctx context.Context, key string) ([]byte, error)
    storedBody, err := storage.Get(ctx, fileName)
    if err != nil { 
        return nil, err
    }   

    fmt.Println(string(storedBody))
GetReader(ctx context.Context, key string) (io.ReadCloser, error)
    reader, err := storage.GetReader(ctx, fileName)
    if err != nil { 
        return nil, err
    }
    defer reader.Close() // Important to prevent memory leaks

    storedBody, err := ioutil.ReadAll(reader)
    fmt.Println(string(storedBody))
Delete(ctx context.Context, key string) error
    err = storage.Delete(ctx, fileName)
    if err != nil { 
        return nil, err
    }   
CreateBucket(ctx context.Context, bucketPrefix string, expirationTimeDays int64) error
    err = storage.CreateBucket(ctx, bucketPrefix, 1)
    if err != nil { 
        return nil, err
    }   
Close()
    storage, err := storage, err := NewCloudStorage(
        ctx,
        isTesting,
        bucketProvider,
        bucketName,
        awsS3Endpoint,
        awsS3Region,
        awsS3AccessKeyID,
        awsS3SecretAccessKey,
        gcpCredentialsJSON,
        gcpStorageEmulatorHost,
    )

    defer storage.Close()
GetSignedURL(ctx context.Context, key string, expiry time.Duration) (string, error)
    url, err := storage.GetSignedURL(ctx, fileName, time.Hour)
    if err != nil { 
        return nil, err
    }   

    fmt.Println(url)
Write(ctx context.Context, key string, body []byte, contentType *string) error
    err := storage.Write(ctx, fileName, bodyBytes, nil)
    if err != nil { 
        return nil, err
    }   
GetWriter(ctx context.Context, key string) (io.WriteCloser, error)
	body := []byte(`{"key": "value", "key2": "value2"}`)

	writer, err := storage.GetWriter(ctx, fileName)
    if err != nil { 
        return nil, err
    }   

	_, err = writer.Write(body[:10])
    if err != nil { 
        return nil, err
    }   

	_, err = writer.Write(body[10:20])
    if err != nil { 
        return nil, err
    }   

	_, err = writer.Write(body[20:])
    if err != nil { 
        return nil, err
    }   

	err = writer.Close()
    if err != nil { 
        return nil, err
    }   
Attributes(ctx context.Context, key string) (*Attributes, error)
    attrs, err := storage.Attributes(ctx, fileName)
    if err != nil { 
        return nil, err
    }   
    
    fmt.Println(attrs.Size)
License
Copyright © 2020, AccelByte Inc. Released under the Apache License, Version 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AWSCloudStorage

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

func (*AWSCloudStorage) Attributes

func (ts *AWSCloudStorage) Attributes(ctx context.Context, key string) (*Attributes, error)

func (*AWSCloudStorage) Close

func (ts *AWSCloudStorage) Close()

func (*AWSCloudStorage) CreateBucket

func (ts *AWSCloudStorage) CreateBucket(ctx context.Context, bucketPrefix string, expirationTimeDays int64) error

func (*AWSCloudStorage) Delete

func (ts *AWSCloudStorage) Delete(ctx context.Context, key string) error

func (*AWSCloudStorage) Get

func (ts *AWSCloudStorage) Get(ctx context.Context, key string) ([]byte, error)

func (*AWSCloudStorage) GetReader added in v0.0.3

func (ts *AWSCloudStorage) GetReader(ctx context.Context, key string) (io.ReadCloser, error)

func (*AWSCloudStorage) GetSignedURL

func (ts *AWSCloudStorage) GetSignedURL(ctx context.Context, key string, expiry time.Duration) (string, error)

func (*AWSCloudStorage) GetWriter added in v0.0.3

func (ts *AWSCloudStorage) GetWriter(ctx context.Context, key string) (io.WriteCloser, error)

func (*AWSCloudStorage) List

func (ts *AWSCloudStorage) List(ctx context.Context, prefix string) *ListIterator

func (*AWSCloudStorage) Write

func (ts *AWSCloudStorage) Write(ctx context.Context, key string, body []byte, contentType *string) error

type AWSTestCloudStorage

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

func (*AWSTestCloudStorage) Attributes

func (ts *AWSTestCloudStorage) Attributes(ctx context.Context, key string) (*Attributes, error)

func (*AWSTestCloudStorage) Close

func (ts *AWSTestCloudStorage) Close()

func (*AWSTestCloudStorage) CreateBucket

func (ts *AWSTestCloudStorage) CreateBucket(ctx context.Context, bucketPrefix string, expirationTimeDays int64) error

func (*AWSTestCloudStorage) Delete

func (ts *AWSTestCloudStorage) Delete(ctx context.Context, key string) error

func (*AWSTestCloudStorage) Get

func (ts *AWSTestCloudStorage) Get(ctx context.Context, key string) ([]byte, error)

func (*AWSTestCloudStorage) GetReader added in v0.0.3

func (ts *AWSTestCloudStorage) GetReader(ctx context.Context, key string) (io.ReadCloser, error)

func (*AWSTestCloudStorage) GetSignedURL

func (ts *AWSTestCloudStorage) GetSignedURL(ctx context.Context, key string, expiry time.Duration) (string, error)

func (*AWSTestCloudStorage) GetWriter added in v0.0.3

func (ts *AWSTestCloudStorage) GetWriter(ctx context.Context, key string) (io.WriteCloser, error)

func (*AWSTestCloudStorage) List

func (ts *AWSTestCloudStorage) List(ctx context.Context, prefix string) *ListIterator

func (*AWSTestCloudStorage) Write

func (ts *AWSTestCloudStorage) Write(ctx context.Context, key string, body []byte, contentType *string) error

type Attributes

type Attributes struct {
	// CacheControl specifies caching attributes that services may use
	// when serving the blob.
	// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
	CacheControl string
	// ContentDisposition specifies whether the blob content is expected to be
	// displayed inline or as an attachment.
	// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
	ContentDisposition string
	// ContentEncoding specifies the encoding used for the blob's content, if any.
	// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
	ContentEncoding string
	// ContentLanguage specifies the language used in the blob's content, if any.
	// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Language
	ContentLanguage string
	// ContentType is the MIME type of the blob. It will not be empty.
	// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
	ContentType string
	// Metadata holds key/value pairs associated with the blob.
	// Keys are guaranteed to be in lowercase, even if the backend service
	// has case-sensitive keys (although note that Metadata written via
	// this package will always be lowercased). If there are duplicate
	// case-insensitive keys (e.g., "foo" and "FOO"), only one value
	// will be kept, and it is undefined which one.
	Metadata map[string]string
	// ModTime is the time the blob was last modified.
	ModTime time.Time
	// Size is the size of the blob's content in bytes.
	Size int64
	// MD5 is an MD5 hash of the blob contents or nil if not available.
	MD5 []byte
}

Attributes contains attributes about a blob.

type CloudStorage

type CloudStorage interface {
	List(ctx context.Context, prefix string) *ListIterator
	Get(ctx context.Context, key string) ([]byte, error)
	Delete(ctx context.Context, key string) error
	CreateBucket(ctx context.Context, bucketPrefix string, expirationTimeDays int64) error
	Close()
	GetSignedURL(ctx context.Context, key string, expiry time.Duration) (string, error)
	Write(ctx context.Context, key string, body []byte, contentType *string) error
	Attributes(ctx context.Context, key string) (*Attributes, error)
	GetReader(ctx context.Context, key string) (io.ReadCloser, error)
	GetWriter(ctx context.Context, key string) (io.WriteCloser, error)
}

func NewCloudStorage

func NewCloudStorage(
	ctx context.Context,
	isTesting bool,
	bucketProvider string,
	bucketName string,

	awsS3Endpoint string,
	awsS3Region string,
	awsS3AccessKeyID string,
	awsS3SecretAccessKey string,

	gcpCredentialsJSON string,
	gcpStorageEmulatorHost string,
) (CloudStorage, error)

type GCPCloudStorage

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

func (*GCPCloudStorage) Attributes

func (ts *GCPCloudStorage) Attributes(ctx context.Context, key string) (*Attributes, error)

func (*GCPCloudStorage) Close

func (ts *GCPCloudStorage) Close()

func (*GCPCloudStorage) CreateBucket

func (ts *GCPCloudStorage) CreateBucket(ctx context.Context, bucketPrefix string, expirationTimeDays int64) error

func (*GCPCloudStorage) Delete

func (ts *GCPCloudStorage) Delete(ctx context.Context, key string) error

func (*GCPCloudStorage) Get

func (ts *GCPCloudStorage) Get(ctx context.Context, key string) ([]byte, error)

func (*GCPCloudStorage) GetReader added in v0.0.3

func (ts *GCPCloudStorage) GetReader(ctx context.Context, key string) (io.ReadCloser, error)

func (*GCPCloudStorage) GetSignedURL

func (ts *GCPCloudStorage) GetSignedURL(ctx context.Context, key string, expiry time.Duration) (string, error)

func (*GCPCloudStorage) GetWriter added in v0.0.3

func (ts *GCPCloudStorage) GetWriter(ctx context.Context, key string) (io.WriteCloser, error)

func (*GCPCloudStorage) List

func (ts *GCPCloudStorage) List(ctx context.Context, prefix string) *ListIterator

func (*GCPCloudStorage) Write

func (ts *GCPCloudStorage) Write(ctx context.Context, key string, body []byte, contentType *string) error

type GCPTestCloudStorage

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

func (*GCPTestCloudStorage) Attributes

func (ts *GCPTestCloudStorage) Attributes(ctx context.Context, key string) (*Attributes, error)

func (*GCPTestCloudStorage) Close

func (ts *GCPTestCloudStorage) Close()

func (*GCPTestCloudStorage) CreateBucket

func (ts *GCPTestCloudStorage) CreateBucket(ctx context.Context, bucketPrefix string, expirationTimeDays int64) error

Create Create the new bucket

func (*GCPTestCloudStorage) Delete

func (ts *GCPTestCloudStorage) Delete(ctx context.Context, key string) error

func (*GCPTestCloudStorage) Get

func (ts *GCPTestCloudStorage) Get(ctx context.Context, key string) ([]byte, error)

func (*GCPTestCloudStorage) GetReader added in v0.0.3

func (ts *GCPTestCloudStorage) GetReader(ctx context.Context, key string) (io.ReadCloser, error)

func (*GCPTestCloudStorage) GetSignedURL

func (ts *GCPTestCloudStorage) GetSignedURL(ctx context.Context, key string, expiry time.Duration) (string, error)

func (*GCPTestCloudStorage) GetWriter added in v0.0.3

func (ts *GCPTestCloudStorage) GetWriter(ctx context.Context, key string) (io.WriteCloser, error)

func (*GCPTestCloudStorage) List

func (ts *GCPTestCloudStorage) List(ctx context.Context, prefix string) *ListIterator

func (*GCPTestCloudStorage) Write

func (ts *GCPTestCloudStorage) Write(ctx context.Context, key string, body []byte, contentType *string) error

type ListIterator

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

ListIterator iterates over List results.

func (*ListIterator) Next

func (i *ListIterator) Next(ctx context.Context) (*ListObject, error)

type ListObject

type ListObject struct {
	// Key is the key for this blob.
	Key string
	// ModTime is the time the blob was last modified.
	ModTime time.Time
	// Size is the size of the blob's content in bytes.
	Size int64
	// MD5 is an MD5 hash of the blob contents or nil if not available.
	MD5 []byte
}

ListObject represents a single blob returned from List.

Jump to

Keyboard shortcuts

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