Skip to content

Commit

Permalink
add prototype gcp cs storage backend support
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Oct 14, 2024
1 parent 0a889f0 commit c46c52f
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions storage/gcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package storage

import (
"context"
"io"
"log"

"cloud.google.com/go/storage"
)

// snapshot upload to gcp cloud storage
func snapshotCSUpload(csBucket string, snapshotFile io.Reader, snapshotName string) error {
// gcp client
ctx := context.Background()
gcpClient, err := storage.NewClient(ctx)
if err != nil {
log.Print("unable to initialize GCP client for transfer to cloud storage bucket")
return err
}
defer gcpClient.Close()

// initialize target in cloud storage bucket
uploadTarget := gcpClient.Bucket(csBucket).Object(snapshotName)
// only upload if does not exist (TODO feature all below)
// uploadTarget = uploadTarget.If(storage.Conditions{DoesNotExist: true})
// If the live object already exists in your csBucket, set instead a
// generation-match precondition using the live object's generation number.
// attrs, err := o.Attrs(ctx)
// if err != nil {
// return fmt.Errorf("object.Attrs: %w", err)
// }
// o = o.If(storage.Conditions{GenerationMatch: attrs.Generation})

// write snapshotfile to upload target in cloud storage bucket
uploadTransfer := uploadTarget.NewWriter(ctx)
if _, err = io.Copy(uploadTransfer, snapshotFile); err != nil {
log.Printf("failed to upload snapshot file %s to bucket %s", uploadTarget.ObjectName(), uploadTarget.BucketName())
return err
}
if err := uploadTransfer.Close(); err != nil {
log.Printf("failed to close snapshot file %s upload transfer to %s", uploadTarget.ObjectName(), uploadTarget.BucketName())
return err
}

return nil
}

0 comments on commit c46c52f

Please sign in to comment.