Categorygithub.com/ahmadissa/gcp_storage
modulepackage
2.0.2+incompatible
Repository: https://github.com/ahmadissa/gcp_storage.git
Documentation: pkg.go.dev

# 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.

# Packages

No description provided by the author

# Functions

Attrs returns the metadata for the bucket.
CopyFile copy cloud storage file to another dst.
CopyFolder copy cloud storage folder to another dst.
Delete storage file from the current bucket.
DeleteFolder delete all files under folder.
DeleteOldFiles delete files from folder based on their age, time from created date.
Download file from source (src) to local destination (dst).
Exists check if file exists.
GetFileReader get file reader from gcp bucket.
GetMeta get size.
GetSignedURL get signed url with expire time.
Init storage instance.
List all files in a bucket with a prefixprefix can be a folder, if prefix is empty string the function will return all files in the bucketlimit is number of files to retrive, 0 means all.
MakePublic make file public (readonly) and retrive the download url.
MD5 get the md5 checksum of a file in a bucket.
ReadFile into object.
Size get the size of the file in int64.
Upload local file to the current bucket.
UploadFromReader upload from reader to GCP file.

# Structs

Meta holds important meta about a file.