I am working on a project where I need to enable versioning of bucket on Google Cloud Storage using Golang. To be more specific :
- How to enable versioning on Already created bucket ?
- How to enable versioning on new buckets ?
Note : I have already googled and gone through the documentation of Google Cloud Platform and found the code example of versioning in Java only. Even I have explored the package "cloud.google.com/go/storage" to understand the code but no help.
I am using below library and functions in my code to update buckets :
import "cloud.google.com/go/storage"
func configureStorage(bucketID string) (*storage.BucketHandle, error) {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
log.Printf("An error happened: %v", err)
return nil, err
}
return client.Bucket(bucketID), nil
}
func saveFileToStorage(bucketID string, fileName string, contentType string, file multipart.File) {
StorageBucket, err := configureStorage(bucketID)
if err != nil {
log.Fatal(err)
}
if StorageBucket == nil {
log.Printf("Storage Bucket is nil")
return
}
ctx := context.Background()
wr := StorageBucket.Object(fileName).NewWriter(ctx)
wr.ACL = []storage.ACLRule{{Entity: storage.AllUsers, Role: storage.RoleReader}}
wr.ContentType = contentType
// Entries are immutable, be aggressive about caching (1 day).
wr.CacheControl = "public, max-age=86400"
if _, err := io.Copy(wr, file); err != nil {
log.Fatal(err)
}
if err := wr.Close(); err != nil {
log.Fatal(err)
}
}