2019-06-27 19:48:54 +03:00
|
|
|
package publish
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"cloud.google.com/go/storage"
|
|
|
|
)
|
|
|
|
|
2019-07-09 01:32:33 +03:00
|
|
|
// GCSDestination represents the Google Cloud Storage destination
|
2019-06-27 19:48:54 +03:00
|
|
|
type GCSDestination struct {
|
|
|
|
gcsBucket string
|
|
|
|
gcsPrefix string
|
|
|
|
}
|
|
|
|
|
2019-07-11 20:29:52 +03:00
|
|
|
// NewGCSDestination is the constructor for a Google Cloud Storage destination
|
2019-07-17 00:33:59 +03:00
|
|
|
func NewGCSDestination(gcsBucket, gcsPrefix string) *GCSDestination {
|
2019-06-27 19:48:54 +03:00
|
|
|
return &GCSDestination{gcsBucket, gcsPrefix}
|
|
|
|
}
|
|
|
|
|
2019-07-09 01:32:33 +03:00
|
|
|
// Path returns a the GCS path for a file within this GCS Destination
|
2019-06-27 19:48:54 +03:00
|
|
|
func (gcsd *GCSDestination) Path(fileName string) string {
|
|
|
|
return fmt.Sprintf("gcs://%s/%s%s", gcsd.gcsBucket, gcsd.gcsPrefix, fileName)
|
|
|
|
}
|
|
|
|
|
2019-07-11 20:29:52 +03:00
|
|
|
// Upload uploads contents to a file in GCS
|
2019-06-27 19:48:54 +03:00
|
|
|
func (gcsd *GCSDestination) Upload(fileContents []byte, fileName string) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
client, err := storage.NewClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
wc := client.Bucket(gcsd.gcsBucket).Object(gcsd.gcsPrefix + fileName).NewWriter(ctx)
|
|
|
|
defer wc.Close()
|
|
|
|
_, err = wc.Write(fileContents)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-17 00:33:59 +03:00
|
|
|
|
|
|
|
// Returns NotImplementedError
|
|
|
|
func (gcsd *GCSDestination) UploadUnencrypted(data map[string]interface{}, fileName string) error {
|
|
|
|
return &NotImplementedError{"GCS does not support uploading the unencrypted file contents."}
|
|
|
|
}
|