GCPStorage

package module
v0.0.0-...-7beb34b Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2022 License: GPL-3.0 Imports: 17 Imported by: 0

README

Google Cloud Platform Storage Bucket helper

Installation

go get -u github.com/ahmadissa/gcp_storage

Example

you need to export GOOGLE_APPLICATION_CREDENTIALS

for more information how to get service account json key file check:

https://cloud.google.com/iam/docs/creating-managing-service-account-keys

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json

to run example


cd example
go run example.go


package main

import (
	"fmt"
	"time"

	"github.com/ahmadissa/gcp_storage"
)

func main() {
	GCPStorage.Init("your_bucket_name")
	localFile := "../testFiles/localfile.txt"
	cloudFile := "test.txt"

	//upload file
	err := GCPStorage.Upload(localFile, cloudFile)
	if err != nil {
		//handle error
		panic(err)
	}

	//get file siza
	size, err := GCPStorage.Size(cloudFile)
	if err != nil {
		//handle error
		panic(err)
	}
	fmt.Printf("size in Bytes: %v\n", size)

	//get file md5 hash
	hash, err := GCPStorage.MD5(cloudFile)
	if err != nil {
		//handle error
		panic(err)
	}
	fmt.Printf("md5 hash: %v\n", hash)

	//check if file exists
	exists, _ := GCPStorage.Exists(cloudFile)
	fmt.Printf("file exists: %v\n", exists)

	//download file
	err = GCPStorage.Download(cloudFile, "localFile.txt")
	if err != nil {
		//handle error
		panic(err)
	}

	//get all file meta data
	attrs, err := GCPStorage.Attrs(cloudFile)
	if err != nil {
		//handle error
		panic(err)
	}
	fmt.Printf("file attrs: %v\n", attrs)

	//list all files
	files, err := GCPStorage.List("")
	if err != nil {
		//handle error
		panic(err)
	}
	fmt.Printf("files: %v\n", files)
	//make public and get download url
	// if you want to test the download url make sure you dont delete the file in last example function
	url, err := GCPStorage.MakePublic(cloudFile)
	if err != nil {
		//handle error
		panic(err)
	}
	fmt.Printf("download url: %v\n", url)
	//delete file
	err = GCPStorage.Delete(cloudFile)
	if err != nil {
		//handle error
		panic(err)
	}

	//Delete folder
	err = GCPStorage.DeleteFolder("instagram/cache")
	if err != nil {
		//handle error
		panic(err)
	}
	//Delete files inside a folder which is one hour old or more
	err = GCPStorage.DeleteOldFiles("instagram/", time.Hour)
	if err != nil {
		//handle error
		panic(err)
	}
}



Test

go test

License

GNU GENERAL PUBLIC LICENSE. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Attrs

func Attrs(filePath string) (attrs *storage.ObjectAttrs, err error)

Attrs returns the metadata for the bucket.

func CopyFile

func CopyFile(src, dst string) error

CopyFile copy cloud storage file to another dst

func CopyFolder

func CopyFolder(srcFolder, dstFolder string, multiple bool) error

CopyFolder copy cloud storage folder to another dst

func Delete

func Delete(filePath string) error

Delete storage file from the current bucket

func DeleteFolder

func DeleteFolder(folder string) error

DeleteFolder delete all files under folder

func DeleteOldFiles

func DeleteOldFiles(folder string, fileAge time.Duration) error

DeleteOldFiles delete files from folder based on their age, time from created date

func Download

func Download(src, dst string) error

Download file from source (src) to local destination (dst)

func Exists

func Exists(filePath string) (bool, error)

Exists check if file exists

func GetFileReader

func GetFileReader(object string, optionalBucket ...string) (reader io.Reader, err error)

GetFileReader get file reader from gcp bucket

func GetSignedURL

func GetSignedURL(objectPath string, duration time.Duration, optionalBucket ...string) (string, error)

GetSignedURL get signed url with expire time

func Init

func Init(bucket string)

Init storage instance

func List

func List(prefix string, limit int) (files []string, err error)

List all files in a bucket with a prefix prefix can be a folder, if prefix is empty string the function will return all files in the bucket limit is number of files to retrive, 0 means all

func MD5

func MD5(filePath string) (md5String string, err error)

MD5 get the md5 checksum of a file in a bucket

func MakePublic

func MakePublic(filePath string) (downloadURL string, err error)

MakePublic make file public (readonly) and retrive the download url

func ReadFile

func ReadFile(filepath string, obj interface{}) (err error)

ReadFile into object

func Size

func Size(filePath string) (size int64, err error)

Size get the size of the file in int64

func Upload

func Upload(localFile, dst string) error

Upload local file to the current bucket

func UploadFromReader

func UploadFromReader(reader io.Reader, dst string, optionalBucket ...string) error

UploadFromReader upload from reader to GCP file

Types

type Meta

type Meta struct {
	MD5         string
	Size        int64
	SizeStr     string
	LastUpdate  time.Time
	Created     time.Time
	ContentType string
}

Meta holds important meta about a file

func GetMeta

func GetMeta(src string, optionalBucket ...string) (Meta, error)

GetMeta get size

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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