Using Google Cloud Storage with Go

Boilerplate

Creating a client

package mainimport (
"context"
"log"

"cloud.google.com/go/storage"
)
const dirType = `inode/directory`func main() {
ctx := context.Background()
cli, err := storage.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
defer cli.Close()
}

Listing bucket contents

bkt := cli.Bucket("j18e-gcs-bucket")it := bkt.Objects(ctx, nil)
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Println(attrs.Name, attrs.ContentType)
}

Creating a directory

contentType := `inode/directory`
obj := bkt.Object("data/myfolder")
writer := obj.NewWriter(ctx)
writer.ObjectAttrs.ContentType = contentType
if _, err := writer.Write(nil); err != nil {
log.Fatal(err)
}
if err := writer.Close(); err != nil {
log.Fatalf("closing writer: %v", err)
}

Creating a file

fileWriter := bkt.Object("data/myfolder/myfile").NewWriter(ctx)
msg := `this is a test`
if _, err := fileWriter.Write([]byte(msg)); err != nil {
log.Fatal(err)
}
if err := fileWriter.Close(); err != nil {
log.Fatalf("closing writer: %v", err)
}

Putting it all together

package mainimport (
"context"
"fmt"
"log"
"cloud.google.com/go/storage"
"google.golang.org/api/iterator"
)
func main() {
ctx := context.Background()
cli, err := storage.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
defer cli.Close()
bkt := cli.Bucket("j18e-gcs-bucket")it := bkt.Objects(ctx, nil)
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Println(attrs.Name, attrs.ContentType)
}
contentType := `inode/directory`
obj := bkt.Object("data/myfolder")
writer := obj.NewWriter(ctx)
writer.ObjectAttrs.ContentType = contentType
if _, err := writer.Write(nil); err != nil {
log.Fatal(err)
}
if err := writer.Close(); err != nil {
log.Fatalf("closing writer: %v", err)
}
fileWriter := bkt.Object("data/myfolder/myfile").NewWriter(ctx)
msg := `this is a test`
if _, err := fileWriter.Write([]byte(msg)); err != nil {
log.Fatal(err)
}
if err := fileWriter.Close(); err != nil {
log.Fatalf("closing writer: %v", err)
}
}

--

--

--

Cloud architect, Kubernetes enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using AnimatedPositioned Widget In Flutter

Introducing the Standard for Authentication Solutions: OAuth 2.0

How To Register Nameservers? -

Homework 01

Providing offline capabilities to your Android app

Legends of Runeterra Deckbuilder Project

Boxed Solution Providers: Buying Pre-packaged IoT Deals

␡Docker — How to Delete Everything

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jamie Wiebe

Jamie Wiebe

Cloud architect, Kubernetes enthusiast

More from Medium

Development Containers Templates

Application Frameworks

Tutorial: IBM Cloud Pak single sign-on (SSO) integration with Ping Identity and Microsoft Active…

Client-side CORS pains solved: serverless cloud functions 🚀