gogcs

package module
v0.0.0-...-5b629ff Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2023 License: MIT Imports: 13 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

View Source
var (
	MapExtensionWithContentType = map[string]string{
		".csv": "text/csv",
	}
)

Functions

func GenerateUrlFromPath

func GenerateUrlFromPath(baseUrl string, filePath string) string

func GetFileExtension

func GetFileExtension(fileName string) string

func GetFullPath

func GetFullPath(path string, name string) string

func MD5BytesToString

func MD5BytesToString(bytes []byte) string

func ObjectToUrl

func ObjectToUrl(baseUrl string, objAttrs *storage.ObjectAttrs) string

Types

type DownloadedFile

type DownloadedFile struct {
	Object   string
	Location *FileLocation
	Data     []byte
}

type File

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

type FileLocation

type FileLocation struct {
	Name string
	Path string
}

type GCSConfig

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

func LoadGSCConfig

func LoadGSCConfig() GCSConfig

type GoGCSClient

type GoGCSClient interface {
	UploadFiles(file []File) ([]UploadedFile, error)
	DownloadFiles(downloads []DownloadedFile) error
	RemoveFiles(objectNames []string) error
	CloneFile(sourceName, destinationName string, isRemoveSource bool) error
	ListFile(path string) ([]ListFile, error)
	GetBaseUrl() string
	GetSignedURL(objectName string, expired time.Duration) (string, error)
}

type GoGSCClient

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

func NewGCSClient

func NewGCSClient(ctx context.Context) (*GoGSCClient, error)

func (GoGSCClient) CloneFile

func (s GoGSCClient) CloneFile(sourceName, destinationName string, isRemoveSource bool) error

func (GoGSCClient) DownloadFiles

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

func (GoGSCClient) GetBaseUrl

func (s GoGSCClient) GetBaseUrl() string

func (GoGSCClient) GetSignedURL

func (s GoGSCClient) GetSignedURL(objectName string, expired time.Duration) (string, error)

func (GoGSCClient) ListFile

func (s GoGSCClient) ListFile(path string) ([]ListFile, error)

func (GoGSCClient) RemoveFiles

func (s GoGSCClient) RemoveFiles(objectNames []string) error

func (GoGSCClient) UploadFiles

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

type ListFile

type ListFile struct {
	Name string
	Url  string
	Size int64
}

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