storage

package
v0.0.0-...-c537d22 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTimeout = errors.New("timeout")
)

Functions

This section is empty.

Types

type ChunkUpload

type ChunkUpload struct {

	// Number of total upload bytes. This is to provide a progress message giving them number of uploaded bytes.
	UploadedBytes int

	// Notifier to pass information back to the user.
	Notifier chan data.UploadStatus

	// Total file size
	FileSize int
	// contains filtered or unexported fields
}

ChunkUpload satisfies the data.Upload interface for GCS chunked uploads.

func (*ChunkUpload) Copy

func (u *ChunkUpload) Copy(r io.Reader) (bool, error)

Copy writes the contents fo the reader into the chunk upload. It also handles all error and success notifications.

func (*ChunkUpload) GetFilepath

func (u *ChunkUpload) GetFilepath() string

GetFilepath returns the filepath. Required to implement Upload interface.

func (*ChunkUpload) GetUploaded

func (u *ChunkUpload) GetUploaded() int

GetUploaded returns the number of bytes already uplaoded. Required to implement Upload interface.

func (*ChunkUpload) IsComplete

func (u *ChunkUpload) IsComplete() bool

func (*ChunkUpload) UpdateUploadedBytes

func (u *ChunkUpload) UpdateUploadedBytes(newPartBytes int)

type Client

type Client struct {
	// The actual storage client for GCS
	*storage.Client

	// A quick way to access the bucket directly.
	Bucket *storage.BucketHandle

	// Timeout for each item.
	Timeout time.Duration
	// contains filtered or unexported fields
}

Client wraps google's *storage.Client to implement: - data.Service - data.HeadService - data.ChunkService - data.SignedUrlService - data.MimeTypeService

Note that it doesn't use data.ChunkManager because the default GCS client is already chunked. Additional chunking would only cause an extra useless goroutine to be run.

Ensure that proper authentication is provided. Note that, if SignedUrl is to be used, that `google auth application-default login` is *not* used as the credential source as it is not adequate.

Instead, use the GOOGLE_APPLICATION_CREDENTIALS environment variable or attach credentials (e.g., via passing in options.WithCredentialsJSON into the New function).

func New

func New(bucket string, opts ...option.ClientOption) (*Client, error)

New generates a new client. option.ClientOption is not necessary unless special credentials are required in order to authenticate against GCS. For example, if the target machine is running on GCP or has the gcloud CLI setup with default authentication credentials, opts is not required.

If nil is provided for context, context.Background is used by default.

func NewClient

func NewClient(creds []byte, bucket string) (*Client, error)

NewClient creates a new GCP storage Client.

DEPRECATED. Use New instead.

func (*Client) Close

func (c *Client) Close() error

Close closes the Client connection

func (*Client) Delete

func (c *Client) Delete(filepath string) error

func (*Client) Download

func (c *Client) Download(filepath string, w io.WriterAt, p *data.DownloadParams) error

Download is supposed to download a file in parallel (by splitting it into different parts to download). However, GCS doesn't seem to have this functionality built in. Thus, we ignore it.

func (*Client) DownloadRange

func (c *Client) DownloadRange(filepath string, w io.WriterAt, start, finish int) error

DownloadRange allows for a part of the file to be downloaded. Dictate the [start, finish) of the download, and the result will be written into io.WriterAt. For example, if start=0 and end=5, bytes 0...4 should be writen.

func (*Client) Exists

func (c *Client) Exists(filepath string) (bool, error)

Exists returns whether the filepath exists in the bucket.

func (*Client) Get

func (c *Client) Get(filepath string) (io.ReadCloser, error)

Get returns a reader that can be used to retrieve a file.

func (*Client) GetChunkSize

func (c *Client) GetChunkSize() int

GetChunkSize returns the ChunkSize so that operators know how much data to provide.

func (*Client) GetIncompleteUpload

func (c *Client) GetIncompleteUpload(filepath string) data.Upload

GetIncompleteUpload returns an incomplete upload based on the filepath.

func (*Client) Head

func (c *Client) Head(filepath string) (*data.HeadInfo, error)

Head returns information regarding the requested file

func (*Client) MD5

func (c *Client) MD5(filepath string) ([]byte, error)

MD5 returns MD5 hash of a GCS object with the given filepath

func (*Client) Objects

func (c *Client) Objects() (data.ObjectIterator, func())

Objects should return an iterator for all objects in a bucket.

func (*Client) Put

func (c *Client) Put(filepath string, r io.Reader) error

func (*Client) PutSignedUrl

func (c *Client) PutSignedUrl(filepath string, tm time.Duration, cfg *data.SignedUrlConfig) (string, error)

PutSignedUrl creates a SignedURL for uploading files directly to the configured GCS bucket. Unfortunately, content-type cannot be inferred from the data.SignedURLConfig object since it requires adding a header into the Signed URL request that needs to be replicated by the one calling the request.

This is burdensome for any application requiring use of this signed URL. Instead, the application can simply include it in its ContentType parameter and signing should still be successful.

func (*Client) PutWithStatus

func (c *Client) PutWithStatus(filepath string, filesize int, r io.Reader) chan data.UploadStatus

PutWithStatus tells GCS to start the upload. It adds the upload to the upload registry and proceeds to send data from the provided io.Reader.

func (*Client) ResumePutWithStatus

func (c *Client) ResumePutWithStatus(filepath string, r io.Reader) (chan data.UploadStatus, error)

ResumePutWithStatus tells CSV to add more data to the upload. If the upload cannot be found on the upload registry, it returns data.ErrUploadNotFound.

func (*Client) SetMimeType

func (c *Client) SetMimeType(filepath, mimeType string) error

SetMimeType sets the MimeType/ContentType with the filepath that has been uploaded.

func (*Client) SetTimeout

func (c *Client) SetTimeout(tm time.Duration)

func (*Client) SignedUrl

func (c *Client) SignedUrl(filepath string, tm time.Duration, cfg *data.SignedUrlConfig) (string, error)

SignedUrl is for the Client to implement the SignedUrlService. SignedUrlService allows a service to return a signed URL for a filepath.

For the implementation below, the GoogleAccessID is required. However, there is a chance that the environment does *not* provide this GoogleAccessID. For example, a system using `google auth application-default login` while looking similar, is *not* a valid credential to use as it does not contain the required `client_email` parameter.

Ensure (in testing and on the server) that proper credentials are provided when creating the client. https://cloud.google.com/docs/authentication/application-default-credentials

func (*Client) Stream

func (c *Client) Stream(filepath string, w io.Writer) error

Stream allows data to be streamed into a writer.

func (*Client) Upload

func (c *Client) Upload(filepath string, filesize int, r io.Reader) error

Upload sends each reader to the chunk uploader for automatic chunked upload.

Jump to

Keyboard shortcuts

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