-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add prototype gcp cs storage backend support
- Loading branch information
1 parent
0a889f0
commit c46c52f
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |