gogcs

package module
v0.0.0-...-b8e4fbd Latest Latest
Warning

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

Go to latest
Published: May 27, 2020 License: MIT Imports: 7 Imported by: 0

README

go-gcs

Google Cloud Storage Wrapper in GoLang

Installation

go get github.com/hawow/go-gcs

Preparation

You will need to setup your environment variables see .env.example

GCS_BUCKET=
GCS_PROJECT_ID=
GCS_JSON_PATH=

Usage

package main

import (
 "context"
 "log"
 "github.com/hawow/go-gcs/"
)

ctx := context.Background()
client := NewGCSClient(ctx)
f, err := os.Open("sample.txt")
if err != nil {
    log.Fatal(err)
    return
}
file := File{
    Name:     "test.txt",
    Path:     "new/test/file",
    Body:     f,
    IsPublic: true,
}
result, err := client.UploadSingleFile(ctx, file)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFullPath

func GetFullPath(path string, name string) string

func MD5BytesToString

func MD5BytesToString(bytes []byte) string

func ObjectToUrl

func ObjectToUrl(objAttrs *storage.ObjectAttrs) string

Types

type DownloadedFile

type DownloadedFile struct {
	Object string
	Name   string
	Path   string
	Data   *[]byte
}

DownloadedFile is used when downloading file from Google Could Storage (GCS). Object is the object path that provided from GCS. Name is the file name that you wanted to save locally. Path is where do you want to have your file stored. It should be a full path. Data is raw bytes.

type File

type File struct {
	Path     string
	Name     string
	Body     io.Reader
	IsPublic bool
}

type GCSConfig

type GCSConfig struct {
	Bucket    string `envconfig:"GCS_BUCKET" required:"true"`
	ProjectID string `envconfig:"GCS_PROJECT_ID" required:"true"`
}

func LoadGSCConfig

func LoadGSCConfig() GCSConfig

type GoGCSClient

type GoGCSClient interface {
	UploadFiles(file []File) ([]UploadedFile, error)
	DownloadFiles(downloads []DownloadedFile) error
	RemoveFiles(downloads []DownloadedFile) error
}

type GoGSCClient

type GoGSCClient struct {
	Client    *storage.Client
	ProjectID string
	Bucket    string
	Context   context.Context
}

func NewGCSClient

func NewGCSClient(ctx context.Context) *GoGSCClient

func (GoGSCClient) DownloadFiles

func (gcsClient GoGSCClient) DownloadFiles(downloads []DownloadedFile) error

func (GoGSCClient) RemoveFiles

func (gcsClient GoGSCClient) RemoveFiles(downloads []DownloadedFile) error

func (GoGSCClient) UploadFiles

func (gcsClient GoGSCClient) UploadFiles(files []File) ([]UploadedFile, error)

type UploadedFile

type UploadedFile struct {
	Name        string
	MD5         string
	IsPublic    bool
	Url         string
	Size        int64
	ObjectAttrs *storage.ObjectAttrs
}

Jump to

Keyboard shortcuts

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