In this post we will review the steps required to write files into the Google Cloud Platform (aka GCP) storage using a GO application.
The application itself is simple:
package main
import (
"cloud.google.com/go/storage"
"context"
"fmt"
)
func main() {
client, err := storage.NewClient(context.Background())
if err != nil {
panic(err)
}
bucket := client.Bucket("mybucket")
object := bucket.Object("myfile")
writer := object.NewWriter(context.Background())
data := "this is my file content"
bytes, err := writer.Write([]byte(data))
if err != nil {
panic(err)
}
fmt.Print("wrote %v bytes", bytes)
err = writer.Close()
if err != nil {
panic(err)
}
}
We create a storage client, and write a text data to the file myfile into the mybucket bucket.
Running this application fails with the following error:
could not find default credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
This is since we need to create a service account, and to grant it permissions to this bucket.
To create a service account, open the GCP console, select "IAM & Admin", Service Accounts. Then click on add a new service account, and create it. In this example we create a service account named test-demo-write.
- storage.objects.create
- storage.objects.delete
No comments:
Post a Comment