go_gcs_wrapper

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2024 License: MIT Imports: 7 Imported by: 0

README

go-gcs-wrapper

go-gcs-wrapper is a library that makes it easy for Go applications to interact with Google Cloud Storage. It provides simple functions for tasks like uploading files and generating signed URLs for secure access. This wrapper handles the complex details of the Google Cloud Storage API, allowing developers to use storage features with less code and effort.

installation
go get -d github.com/michaelwp/go-gcs-wrapper
basic of use
  • Upload file
package main

import (
	"context"
	"github.com/joho/godotenv"
	gogcswrapper "github.com/michaelwp/go-gcs-wrapper"
	"log"
	"os"
)

func init() {
	err := godotenv.Load(".env")
	if err != nil {
		log.Fatal("Error loading .env file", err)
	}
}

func main() {
	projectId := os.Getenv("GOOGLE_APPLICATION_PROJECT_ID")
	bucket := os.Getenv("GOOGLE_APPLICATION_BUCKET")
	object := "go_gcs.png"
	uploadObjPath := "upload_tes"
	localObjPath := "."

	ctx := context.Background()
	gcs := gogcswrapper.NewGCS(ctx, projectId)

	params := &gogcswrapper.UploadParams{
		BucketAndObject: &gogcswrapper.BucketAndObject{
			Bucket: bucket,
			Object: object,
		},
		LocalObjPath:  localObjPath,
		UploadObjPath: uploadObjPath,
	}

	err := gcs.Upload(ctx, params)
	if err != nil {
		log.Fatal("error uploading file", err)
	}
}
  • Generate signed Url
package main

import (
	"context"
	"fmt"
	"github.com/joho/godotenv"
	gogcswrapper "github.com/michaelwp/go-gcs-wrapper"
	"log"
	"os"
	"time"
)

func init() {
	err := godotenv.Load(".env")
	if err != nil {
		log.Fatal("Error loading .env file", err)
	}
}

func main() {
	projectId := os.Getenv("GOOGLE_APPLICATION_PROJECT_ID")
	bucket := os.Getenv("GOOGLE_APPLICATION_BUCKET")
	object := "go_gcs.png"
	uploadObjPath := "upload_tes"

	ctx := context.Background()
	gcs := gogcswrapper.NewGCS(ctx, projectId)

	params := &gogcswrapper.GenerateSignedURLParams{
		BucketAndObject: &gogcswrapper.BucketAndObject{
			Bucket: bucket,
			Object: object,
		},
		UploadObjPath:  uploadObjPath,
		ExpirationTime: time.Now().Add(time.Minute * 10),
	}

	signedUrl, err := gcs.GenerateSignedURL(params)
	if err != nil {
		log.Fatal("error generating sign url", err)
	}

	fmt.Printf("Signed URL: %s\n", signedUrl)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BucketAndObject

type BucketAndObject struct {
	Bucket string
	Object string
}

type Gcs

type Gcs interface {
	Upload(ctx context.Context, params *UploadParams) error
	GenerateSignedURL(params *GenerateSignedURLParams) (string, error)
}

func NewGCS

func NewGCS(ctx context.Context, projectId string) Gcs

type GenerateSignedURLParams

type GenerateSignedURLParams struct {
	*BucketAndObject
	ExpirationTime time.Time
	UploadObjPath  string
}

type UploadParams

type UploadParams struct {
	LocalObjPath  string
	UploadObjPath string
	*BucketAndObject
}

Directories

Path Synopsis
example
generateSignedUrl command
author: Michael Putong @2024 This is an example on how to implement go-gcs-wrapper to generate a Google Cloud Storage's signedUrl visit the code repository in github.com/michaelwp/go-gcs-wrapper
author: Michael Putong @2024 This is an example on how to implement go-gcs-wrapper to generate a Google Cloud Storage's signedUrl visit the code repository in github.com/michaelwp/go-gcs-wrapper
upload command

Jump to

Keyboard shortcuts

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