2015-07-20 17:45:15 +00:00
|
|
|
// Package gcs provides a storagedriver.StorageDriver implementation to
|
|
|
|
// store blobs in Google cloud storage.
|
|
|
|
//
|
|
|
|
// This package leverages the google.golang.org/cloud/storage client library
|
2022-11-02 21:05:45 +00:00
|
|
|
// for interfacing with gcs.
|
2015-07-20 17:45:15 +00:00
|
|
|
//
|
|
|
|
// Because gcs is a key, value store the Stat call does not support last modification
|
|
|
|
// time for directories (directories are an abstraction for key, value stores)
|
|
|
|
//
|
2016-02-12 13:30:57 +00:00
|
|
|
// Note that the contents of incomplete uploads are not accessible even though
|
|
|
|
// Stat returns their length
|
2015-07-20 17:45:15 +00:00
|
|
|
package gcs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2016-08-08 22:43:12 +00:00
|
|
|
"encoding/json"
|
2023-05-23 08:42:39 +00:00
|
|
|
"errors"
|
2015-07-20 17:45:15 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2022-11-02 21:55:22 +00:00
|
|
|
"os"
|
2016-02-14 18:15:15 +00:00
|
|
|
"reflect"
|
2016-02-12 13:30:57 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2015-07-20 17:45:15 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-05-23 08:42:39 +00:00
|
|
|
"cloud.google.com/go/storage"
|
2020-08-24 11:18:39 +00:00
|
|
|
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver/base"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver/factory"
|
2017-08-11 22:31:16 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-07-20 17:45:15 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/google"
|
2016-01-06 22:46:25 +00:00
|
|
|
"golang.org/x/oauth2/jwt"
|
2015-07-20 17:45:15 +00:00
|
|
|
"google.golang.org/api/googleapi"
|
2023-05-23 08:42:39 +00:00
|
|
|
"google.golang.org/api/iterator"
|
|
|
|
"google.golang.org/api/option"
|
2015-07-20 17:45:15 +00:00
|
|
|
)
|
|
|
|
|
2016-02-12 13:30:57 +00:00
|
|
|
const (
|
|
|
|
driverName = "gcs"
|
|
|
|
dummyProjectID = "<unknown>"
|
|
|
|
|
2023-10-20 13:53:41 +00:00
|
|
|
minChunkSize = 256 * 1024
|
|
|
|
defaultChunkSize = 16 * 1024 * 1024
|
|
|
|
defaultMaxConcurrency = 50
|
|
|
|
minConcurrency = 25
|
|
|
|
|
2016-02-12 13:30:57 +00:00
|
|
|
uploadSessionContentType = "application/x-docker-upload-session"
|
2023-10-20 13:53:41 +00:00
|
|
|
blobContentType = "application/octet-stream"
|
2016-02-12 13:30:57 +00:00
|
|
|
|
|
|
|
maxTries = 5
|
|
|
|
)
|
|
|
|
|
|
|
|
var rangeHeader = regexp.MustCompile(`^bytes=([0-9])+-([0-9]+)$`)
|
2015-07-20 17:45:15 +00:00
|
|
|
|
2023-05-23 08:42:39 +00:00
|
|
|
var _ storagedriver.FileWriter = &writer{}
|
|
|
|
|
2016-01-06 22:46:25 +00:00
|
|
|
// driverParameters is a struct that encapsulates all of the driver parameters after all values have been set
|
2015-07-20 17:45:15 +00:00
|
|
|
type driverParameters struct {
|
|
|
|
bucket string
|
2016-01-06 22:46:25 +00:00
|
|
|
email string
|
|
|
|
privateKey []byte
|
|
|
|
client *http.Client
|
2015-07-20 17:45:15 +00:00
|
|
|
rootDirectory string
|
2016-02-14 18:15:15 +00:00
|
|
|
chunkSize int
|
2023-05-23 08:42:39 +00:00
|
|
|
gcs *storage.Client
|
2016-12-06 23:50:39 +00:00
|
|
|
|
|
|
|
// maxConcurrency limits the number of concurrent driver operations
|
|
|
|
// to GCS, which ultimately increases reliability of many simultaneous
|
|
|
|
// pushes by ensuring we aren't DoSing our own server with many
|
|
|
|
// connections.
|
|
|
|
maxConcurrency uint64
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
factory.Register(driverName, &gcsDriverFactory{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// gcsDriverFactory implements the factory.StorageDriverFactory interface
|
|
|
|
type gcsDriverFactory struct{}
|
|
|
|
|
|
|
|
// Create StorageDriver from parameters
|
2023-10-27 21:33:55 +00:00
|
|
|
func (factory *gcsDriverFactory) Create(ctx context.Context, parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
|
|
|
|
return FromParameters(ctx, parameters)
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 10:41:22 +00:00
|
|
|
var _ storagedriver.StorageDriver = &driver{}
|
|
|
|
|
2015-07-20 17:45:15 +00:00
|
|
|
// driver is a storagedriver.StorageDriver implementation backed by GCS
|
|
|
|
// Objects are stored at absolute keys in the provided bucket.
|
|
|
|
type driver struct {
|
|
|
|
client *http.Client
|
2023-10-20 13:53:41 +00:00
|
|
|
bucket *storage.BucketHandle
|
2015-07-20 17:45:15 +00:00
|
|
|
email string
|
|
|
|
privateKey []byte
|
|
|
|
rootDirectory string
|
2016-02-14 18:15:15 +00:00
|
|
|
chunkSize int
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 23:50:39 +00:00
|
|
|
// Wrapper wraps `driver` with a throttler, ensuring that no more than N
|
|
|
|
// GCS actions can occur concurrently. The default limit is 75.
|
|
|
|
type Wrapper struct {
|
|
|
|
baseEmbed
|
|
|
|
}
|
|
|
|
|
|
|
|
type baseEmbed struct {
|
|
|
|
base.Base
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:45:15 +00:00
|
|
|
// FromParameters constructs a new Driver with a given parameters map
|
|
|
|
// Required parameters:
|
|
|
|
// - bucket
|
2023-10-27 21:33:55 +00:00
|
|
|
func FromParameters(ctx context.Context, parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
|
2015-07-20 17:45:15 +00:00
|
|
|
bucket, ok := parameters["bucket"]
|
|
|
|
if !ok || fmt.Sprint(bucket) == "" {
|
|
|
|
return nil, fmt.Errorf("No bucket parameter provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
rootDirectory, ok := parameters["rootdirectory"]
|
|
|
|
if !ok {
|
|
|
|
rootDirectory = ""
|
|
|
|
}
|
2016-01-06 22:46:25 +00:00
|
|
|
|
2016-02-14 18:15:15 +00:00
|
|
|
chunkSize := defaultChunkSize
|
|
|
|
chunkSizeParam, ok := parameters["chunksize"]
|
|
|
|
if ok {
|
|
|
|
switch v := chunkSizeParam.(type) {
|
|
|
|
case string:
|
|
|
|
vv, err := strconv.Atoi(v)
|
|
|
|
if err != nil {
|
2023-10-20 13:53:41 +00:00
|
|
|
return nil, fmt.Errorf("chunksize must be an integer, %v invalid", chunkSizeParam)
|
2016-02-14 18:15:15 +00:00
|
|
|
}
|
|
|
|
chunkSize = vv
|
|
|
|
case int, uint, int32, uint32, uint64, int64:
|
|
|
|
chunkSize = int(reflect.ValueOf(v).Convert(reflect.TypeOf(chunkSize)).Int())
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid valud for chunksize: %#v", chunkSizeParam)
|
|
|
|
}
|
|
|
|
|
|
|
|
if chunkSize < minChunkSize {
|
2023-10-20 13:53:41 +00:00
|
|
|
return nil, fmt.Errorf("chunksize %#v must be larger than or equal to %d", chunkSize, minChunkSize)
|
2016-02-14 18:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if chunkSize%minChunkSize != 0 {
|
|
|
|
return nil, fmt.Errorf("chunksize should be a multiple of %d", minChunkSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 22:46:25 +00:00
|
|
|
var ts oauth2.TokenSource
|
|
|
|
jwtConf := new(jwt.Config)
|
2023-05-23 08:42:39 +00:00
|
|
|
var err error
|
|
|
|
var gcs *storage.Client
|
2023-12-19 14:34:01 +00:00
|
|
|
var options []option.ClientOption
|
2016-01-06 22:46:25 +00:00
|
|
|
if keyfile, ok := parameters["keyfile"]; ok {
|
2022-11-02 21:55:22 +00:00
|
|
|
jsonKey, err := os.ReadFile(fmt.Sprint(keyfile))
|
2016-01-06 22:46:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jwtConf, err = google.JWTConfigFromJSON(jsonKey, storage.ScopeFullControl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-23 08:42:39 +00:00
|
|
|
ts = jwtConf.TokenSource(ctx)
|
2023-12-19 14:34:01 +00:00
|
|
|
options = append(options, option.WithCredentialsFile(fmt.Sprint(keyfile)))
|
2016-08-08 22:43:12 +00:00
|
|
|
} else if credentials, ok := parameters["credentials"]; ok {
|
|
|
|
credentialMap, ok := credentials.(map[interface{}]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("The credentials were not specified in the correct format")
|
|
|
|
}
|
|
|
|
|
|
|
|
stringMap := map[string]interface{}{}
|
|
|
|
for k, v := range credentialMap {
|
|
|
|
key, ok := k.(string)
|
|
|
|
if !ok {
|
2016-08-15 21:35:18 +00:00
|
|
|
return nil, fmt.Errorf("One of the credential keys was not a string: %s", fmt.Sprint(k))
|
2016-08-08 22:43:12 +00:00
|
|
|
}
|
|
|
|
stringMap[key] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(stringMap)
|
2016-08-15 21:35:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to marshal gcs credentials to json")
|
|
|
|
}
|
|
|
|
|
2018-05-07 22:04:06 +00:00
|
|
|
jwtConf, err = google.JWTConfigFromJSON(data, storage.ScopeFullControl)
|
2016-08-08 22:43:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-23 08:42:39 +00:00
|
|
|
ts = jwtConf.TokenSource(ctx)
|
2023-12-19 14:34:01 +00:00
|
|
|
options = append(options, option.WithCredentialsJSON(data))
|
2016-01-06 22:46:25 +00:00
|
|
|
} else {
|
|
|
|
var err error
|
2023-10-20 13:53:41 +00:00
|
|
|
// DefaultTokenSource is a convenience method. It first calls FindDefaultCredentials,
|
|
|
|
// then uses the credentials to construct an http.Client or an oauth2.TokenSource.
|
|
|
|
// https://pkg.go.dev/golang.org/x/oauth2/google#hdr-Credentials
|
2023-05-23 08:42:39 +00:00
|
|
|
ts, err = google.DefaultTokenSource(ctx, storage.ScopeFullControl)
|
2016-01-06 22:46:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-19 14:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if userAgent, ok := parameters["useragent"]; ok {
|
|
|
|
if ua, ok := userAgent.(string); ok && ua != "" {
|
|
|
|
options = append(options, option.WithUserAgent(ua))
|
2023-10-20 13:53:41 +00:00
|
|
|
}
|
2016-01-06 22:46:25 +00:00
|
|
|
}
|
|
|
|
|
2023-12-19 14:34:01 +00:00
|
|
|
gcs, err = storage.NewClient(ctx, options...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-06 23:50:39 +00:00
|
|
|
maxConcurrency, err := base.GetLimitFromParameter(parameters["maxconcurrency"], minConcurrency, defaultMaxConcurrency)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("maxconcurrency config error: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:45:15 +00:00
|
|
|
params := driverParameters{
|
2016-12-06 23:50:39 +00:00
|
|
|
bucket: fmt.Sprint(bucket),
|
|
|
|
rootDirectory: fmt.Sprint(rootDirectory),
|
|
|
|
email: jwtConf.Email,
|
|
|
|
privateKey: jwtConf.PrivateKey,
|
2023-05-23 08:42:39 +00:00
|
|
|
client: oauth2.NewClient(ctx, ts),
|
2016-12-06 23:50:39 +00:00
|
|
|
chunkSize: chunkSize,
|
|
|
|
maxConcurrency: maxConcurrency,
|
2023-05-23 08:42:39 +00:00
|
|
|
gcs: gcs,
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
2023-10-27 21:33:55 +00:00
|
|
|
return New(ctx, params)
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New constructs a new driver
|
2023-10-27 21:33:55 +00:00
|
|
|
func New(ctx context.Context, params driverParameters) (storagedriver.StorageDriver, error) {
|
2015-07-20 17:45:15 +00:00
|
|
|
rootDirectory := strings.Trim(params.rootDirectory, "/")
|
|
|
|
if rootDirectory != "" {
|
|
|
|
rootDirectory += "/"
|
|
|
|
}
|
2016-02-14 18:15:15 +00:00
|
|
|
if params.chunkSize <= 0 || params.chunkSize%minChunkSize != 0 {
|
|
|
|
return nil, fmt.Errorf("Invalid chunksize: %d is not a positive multiple of %d", params.chunkSize, minChunkSize)
|
|
|
|
}
|
2015-07-20 17:45:15 +00:00
|
|
|
d := &driver{
|
2023-10-20 13:53:41 +00:00
|
|
|
bucket: params.gcs.Bucket(params.bucket),
|
2015-07-20 17:45:15 +00:00
|
|
|
rootDirectory: rootDirectory,
|
2016-01-06 22:46:25 +00:00
|
|
|
email: params.email,
|
|
|
|
privateKey: params.privateKey,
|
|
|
|
client: params.client,
|
2016-02-14 18:15:15 +00:00
|
|
|
chunkSize: params.chunkSize,
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
2016-01-06 22:46:25 +00:00
|
|
|
|
2016-12-06 23:50:39 +00:00
|
|
|
return &Wrapper{
|
|
|
|
baseEmbed: baseEmbed{
|
|
|
|
Base: base.Base{
|
|
|
|
StorageDriver: base.NewRegulator(d, params.maxConcurrency),
|
|
|
|
},
|
|
|
|
},
|
2015-07-20 17:45:15 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement the storagedriver.StorageDriver interface
|
|
|
|
|
|
|
|
func (d *driver) Name() string {
|
|
|
|
return driverName
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContent retrieves the content stored at "path" as a []byte.
|
|
|
|
// This should primarily be used for small objects.
|
2023-05-23 08:42:39 +00:00
|
|
|
func (d *driver) GetContent(ctx context.Context, path string) ([]byte, error) {
|
2023-10-20 13:53:41 +00:00
|
|
|
r, err := d.bucket.Object(d.pathToKey(path)).NewReader(ctx)
|
2015-07-20 17:45:15 +00:00
|
|
|
if err != nil {
|
2023-10-20 13:53:41 +00:00
|
|
|
if err == storage.ErrObjectNotExist {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
2015-07-20 17:45:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
defer r.Close()
|
2015-07-20 17:45:15 +00:00
|
|
|
|
2023-10-20 13:53:41 +00:00
|
|
|
return io.ReadAll(r)
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutContent stores the []byte content at a location designated by "path".
|
|
|
|
// This should primarily be used for small objects.
|
2023-05-23 08:42:39 +00:00
|
|
|
func (d *driver) PutContent(ctx context.Context, path string, contents []byte) error {
|
2023-06-28 10:41:22 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
2023-10-20 13:53:41 +00:00
|
|
|
|
|
|
|
object := d.bucket.Object(d.pathToKey(path))
|
|
|
|
err := d.putContent(ctx, object, contents, blobContentType, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 13:30:57 +00:00
|
|
|
// Reader retrieves an io.ReadCloser for the content stored at "path"
|
2015-07-20 17:45:15 +00:00
|
|
|
// with a given byte offset.
|
|
|
|
// May be used to resume reading a stream by providing a nonzero offset.
|
2023-05-23 08:42:39 +00:00
|
|
|
func (d *driver) Reader(ctx context.Context, path string, offset int64) (io.ReadCloser, error) {
|
2023-10-20 13:53:41 +00:00
|
|
|
obj := d.bucket.Object(d.pathToKey(path))
|
|
|
|
// NOTE(milosgajdos): If length is negative, the object is read until the end
|
|
|
|
// See: https://pkg.go.dev/cloud.google.com/go/storage#ObjectHandle.NewRangeReader
|
|
|
|
r, err := obj.NewRangeReader(ctx, offset, -1)
|
2016-02-12 13:30:57 +00:00
|
|
|
if err != nil {
|
2023-10-20 13:53:41 +00:00
|
|
|
if err == storage.ErrObjectNotExist {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
var status *googleapi.Error
|
|
|
|
if errors.As(err, &status) {
|
|
|
|
switch status.Code {
|
|
|
|
case http.StatusNotFound:
|
2016-02-12 13:30:57 +00:00
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
2023-10-20 13:53:41 +00:00
|
|
|
case http.StatusRequestedRangeNotSatisfiable:
|
|
|
|
attrs, err := obj.Attrs(ctx)
|
2016-02-12 13:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
if offset == attrs.Size {
|
2022-11-02 21:55:22 +00:00
|
|
|
return io.NopCloser(bytes.NewReader([]byte{})), nil
|
2016-02-12 13:30:57 +00:00
|
|
|
}
|
|
|
|
return nil, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
if r.Attrs.ContentType == uploadSessionContentType {
|
|
|
|
r.Close()
|
2016-02-12 13:30:57 +00:00
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
return r, nil
|
2016-02-12 13:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writer returns a FileWriter which will store the content written to it
|
|
|
|
// at the location designated by "path" after the call to Commit.
|
2023-10-20 13:53:41 +00:00
|
|
|
func (d *driver) Writer(ctx context.Context, path string, appendMode bool) (storagedriver.FileWriter, error) {
|
|
|
|
w := &writer{
|
|
|
|
ctx: ctx,
|
|
|
|
driver: d,
|
|
|
|
object: d.bucket.Object(d.pathToKey(path)),
|
2016-02-14 18:15:15 +00:00
|
|
|
buffer: make([]byte, d.chunkSize),
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
2016-02-12 13:30:57 +00:00
|
|
|
|
2023-10-20 13:53:41 +00:00
|
|
|
if appendMode {
|
|
|
|
err := w.init(ctx)
|
2015-07-20 17:45:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
return w, nil
|
2016-02-12 13:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type writer struct {
|
2023-10-20 13:53:41 +00:00
|
|
|
ctx context.Context
|
|
|
|
object *storage.ObjectHandle
|
|
|
|
driver *driver
|
2016-02-12 13:30:57 +00:00
|
|
|
size int64
|
|
|
|
offset int64
|
|
|
|
closed bool
|
2023-10-20 13:53:41 +00:00
|
|
|
cancelled bool
|
|
|
|
committed bool
|
2016-02-12 13:30:57 +00:00
|
|
|
sessionURI string
|
|
|
|
buffer []byte
|
|
|
|
buffSize int
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 13:30:57 +00:00
|
|
|
// Cancel removes any written content from this FileWriter.
|
2023-05-22 13:54:35 +00:00
|
|
|
func (w *writer) Cancel(ctx context.Context) error {
|
2016-02-12 13:30:57 +00:00
|
|
|
w.closed = true
|
2023-10-20 13:53:41 +00:00
|
|
|
w.cancelled = true
|
|
|
|
|
|
|
|
err := w.object.Delete(ctx)
|
2016-02-12 13:30:57 +00:00
|
|
|
if err != nil {
|
2023-05-23 08:42:39 +00:00
|
|
|
if err == storage.ErrObjectNotExist {
|
|
|
|
err = nil
|
2016-02-12 13:30:57 +00:00
|
|
|
}
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
2016-02-12 13:30:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-07-20 17:45:15 +00:00
|
|
|
|
2016-02-12 13:30:57 +00:00
|
|
|
func (w *writer) Close() error {
|
|
|
|
if w.closed {
|
|
|
|
return nil
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
2016-02-12 13:30:57 +00:00
|
|
|
w.closed = true
|
2015-07-20 17:45:15 +00:00
|
|
|
|
2023-10-20 13:53:41 +00:00
|
|
|
err := w.writeChunk(w.ctx)
|
2015-07-20 17:45:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-12 13:30:57 +00:00
|
|
|
// Copy the remaining bytes from the buffer to the upload session
|
|
|
|
// Normally buffSize will be smaller than minChunkSize. However, in the
|
|
|
|
// unlikely event that the upload session failed to start, this number could be higher.
|
|
|
|
// In this case we can safely clip the remaining bytes to the minChunkSize
|
|
|
|
if w.buffSize > minChunkSize {
|
|
|
|
w.buffSize = minChunkSize
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 13:30:57 +00:00
|
|
|
// commit the writes by updating the upload session
|
2023-10-20 13:53:41 +00:00
|
|
|
metadata := map[string]string{
|
|
|
|
"Session-URI": w.sessionURI,
|
|
|
|
"Offset": strconv.FormatInt(w.offset, 10),
|
|
|
|
}
|
2016-02-12 13:30:57 +00:00
|
|
|
err = retry(func() error {
|
2023-10-20 13:53:41 +00:00
|
|
|
err := w.driver.putContent(w.ctx, w.object, w.buffer[0:w.buffSize], uploadSessionContentType, metadata)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-02-12 13:30:57 +00:00
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
w.size = w.offset + int64(w.buffSize)
|
|
|
|
w.buffSize = 0
|
|
|
|
return nil
|
2016-02-12 13:30:57 +00:00
|
|
|
})
|
2023-10-20 13:53:41 +00:00
|
|
|
return err
|
2016-02-12 13:30:57 +00:00
|
|
|
}
|
2015-07-20 17:45:15 +00:00
|
|
|
|
2023-10-20 13:53:41 +00:00
|
|
|
func (d *driver) putContent(ctx context.Context, obj *storage.ObjectHandle, content []byte, contentType string, metadata map[string]string) error {
|
|
|
|
wc := obj.NewWriter(ctx)
|
|
|
|
wc.Metadata = metadata
|
|
|
|
wc.ContentType = contentType
|
|
|
|
wc.ChunkSize = d.chunkSize
|
|
|
|
|
|
|
|
if _, err := bytes.NewReader(content).WriteTo(wc); err != nil {
|
2016-02-12 13:30:57 +00:00
|
|
|
return err
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
2016-02-12 13:30:57 +00:00
|
|
|
return wc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit flushes all content written to this FileWriter and makes it
|
|
|
|
// available for future calls to StorageDriver.GetContent and
|
|
|
|
// StorageDriver.Reader.
|
2023-10-18 09:34:10 +00:00
|
|
|
func (w *writer) Commit(ctx context.Context) error {
|
2023-10-20 13:53:41 +00:00
|
|
|
if w.closed {
|
|
|
|
return fmt.Errorf("already closed")
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
2016-02-12 13:30:57 +00:00
|
|
|
w.closed = true
|
2015-07-20 17:45:15 +00:00
|
|
|
|
2016-02-12 13:30:57 +00:00
|
|
|
// no session started yet just perform a simple upload
|
|
|
|
if w.sessionURI == "" {
|
|
|
|
err := retry(func() error {
|
2023-10-20 13:53:41 +00:00
|
|
|
err := w.driver.putContent(ctx, w.object, w.buffer[0:w.buffSize], blobContentType, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.committed = true
|
|
|
|
w.size = w.offset + int64(w.buffSize)
|
|
|
|
w.buffSize = 0
|
|
|
|
return nil
|
2016-02-12 13:30:57 +00:00
|
|
|
})
|
2023-10-20 13:53:41 +00:00
|
|
|
return err
|
2016-02-12 13:30:57 +00:00
|
|
|
}
|
|
|
|
size := w.offset + int64(w.buffSize)
|
2023-10-20 13:53:41 +00:00
|
|
|
var written int
|
2016-02-12 13:30:57 +00:00
|
|
|
// loop must be performed at least once to ensure the file is committed even when
|
|
|
|
// the buffer is empty
|
|
|
|
for {
|
2023-10-20 13:53:41 +00:00
|
|
|
n, err := w.putChunk(ctx, w.sessionURI, w.buffer[written:w.buffSize], w.offset, size)
|
|
|
|
written += int(n)
|
2016-02-12 13:30:57 +00:00
|
|
|
w.offset += n
|
|
|
|
w.size = w.offset
|
|
|
|
if err != nil {
|
2023-10-20 13:53:41 +00:00
|
|
|
w.buffSize = copy(w.buffer, w.buffer[written:w.buffSize])
|
2016-02-12 13:30:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
if written == w.buffSize {
|
2016-02-12 13:30:57 +00:00
|
|
|
break
|
|
|
|
}
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
w.committed = true
|
2016-02-12 13:30:57 +00:00
|
|
|
w.buffSize = 0
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-20 17:45:15 +00:00
|
|
|
|
2023-10-20 13:53:41 +00:00
|
|
|
func (w *writer) writeChunk(ctx context.Context) error {
|
2016-02-12 13:30:57 +00:00
|
|
|
var err error
|
|
|
|
// chunks can be uploaded only in multiples of minChunkSize
|
|
|
|
// chunkSize is a multiple of minChunkSize less than or equal to buffSize
|
|
|
|
chunkSize := w.buffSize - (w.buffSize % minChunkSize)
|
|
|
|
if chunkSize == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// if their is no sessionURI yet, obtain one by starting the session
|
|
|
|
if w.sessionURI == "" {
|
2023-10-20 13:53:41 +00:00
|
|
|
w.sessionURI, err = w.newSession()
|
2016-02-12 13:30:57 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
n, err := w.putChunk(ctx, w.sessionURI, w.buffer[0:chunkSize], w.offset, -1)
|
|
|
|
w.offset += n
|
2016-02-12 13:30:57 +00:00
|
|
|
if w.offset > w.size {
|
|
|
|
w.size = w.offset
|
|
|
|
}
|
|
|
|
// shift the remaining bytes to the start of the buffer
|
2023-10-20 13:53:41 +00:00
|
|
|
w.buffSize = copy(w.buffer, w.buffer[int(n):w.buffSize])
|
2016-02-12 13:30:57 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) Write(p []byte) (int, error) {
|
2023-10-20 13:53:41 +00:00
|
|
|
if w.closed {
|
|
|
|
return 0, fmt.Errorf("already closed")
|
|
|
|
} else if w.cancelled {
|
|
|
|
return 0, fmt.Errorf("already cancelled")
|
2016-02-12 13:30:57 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 13:53:41 +00:00
|
|
|
var (
|
|
|
|
written int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
for written < len(p) {
|
|
|
|
n := copy(w.buffer[w.buffSize:], p[written:])
|
2016-02-12 13:30:57 +00:00
|
|
|
w.buffSize += n
|
|
|
|
if w.buffSize == cap(w.buffer) {
|
2023-10-20 13:53:41 +00:00
|
|
|
err = w.writeChunk(w.ctx)
|
2016-02-12 13:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
written += n
|
2016-02-12 13:30:57 +00:00
|
|
|
}
|
2023-05-23 08:42:39 +00:00
|
|
|
w.size = w.offset + int64(w.buffSize)
|
2023-10-20 13:53:41 +00:00
|
|
|
return written, err
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 13:30:57 +00:00
|
|
|
// Size returns the number of bytes written to this FileWriter.
|
|
|
|
func (w *writer) Size() int64 {
|
|
|
|
return w.size
|
|
|
|
}
|
|
|
|
|
2023-10-20 13:53:41 +00:00
|
|
|
func (w *writer) init(ctx context.Context) error {
|
|
|
|
attrs, err := w.object.Attrs(ctx)
|
2016-02-12 13:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
|
|
|
|
// NOTE(milosgajdos): when PUSH abruptly finishes by
|
|
|
|
// calling a single commit and then closes the stream
|
|
|
|
// attrs.ContentType ends up being set to application/octet-stream
|
|
|
|
// We must handle this case so the upload can resume.
|
|
|
|
if attrs.ContentType != uploadSessionContentType &&
|
|
|
|
attrs.ContentType != blobContentType {
|
|
|
|
return storagedriver.PathNotFoundError{Path: w.object.ObjectName()}
|
|
|
|
}
|
|
|
|
|
|
|
|
offset := int64(0)
|
|
|
|
// NOTE(milosgajdos): if a client creates an empty blob, then
|
|
|
|
// closes the stream and then attempts to append to it, the offset
|
|
|
|
// will be empty, in which case strconv.ParseInt will return error
|
|
|
|
// See: https://pkg.go.dev/strconv#ParseInt
|
|
|
|
if attrs.Metadata["Offset"] != "" {
|
|
|
|
offset, err = strconv.ParseInt(attrs.Metadata["Offset"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-12 13:30:57 +00:00
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
|
|
|
|
r, err := w.object.NewReader(ctx)
|
2016-02-12 13:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
for err == nil && w.buffSize < len(w.buffer) {
|
|
|
|
var n int
|
|
|
|
n, err = r.Read(w.buffer[w.buffSize:])
|
|
|
|
w.buffSize += n
|
|
|
|
}
|
|
|
|
if err != nil && err != io.EOF {
|
2016-02-12 13:30:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
|
|
|
|
// NOTE(milosgajdos): if a client closes an existing session and then attempts
|
|
|
|
// to append to an existing blob, the session will be empty; recreate it
|
|
|
|
if w.sessionURI = attrs.Metadata["Session-URI"]; w.sessionURI == "" {
|
|
|
|
w.sessionURI, err = w.newSession()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 13:30:57 +00:00
|
|
|
w.offset = offset
|
|
|
|
w.size = offset + int64(w.buffSize)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:45:15 +00:00
|
|
|
type request func() error
|
|
|
|
|
2016-02-12 13:30:57 +00:00
|
|
|
func retry(req request) error {
|
2015-07-20 17:45:15 +00:00
|
|
|
backoff := time.Second
|
|
|
|
var err error
|
|
|
|
for i := 0; i < maxTries; i++ {
|
2016-01-06 11:47:28 +00:00
|
|
|
err = req()
|
2015-07-20 17:45:15 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-06 11:47:28 +00:00
|
|
|
status, ok := err.(*googleapi.Error)
|
2023-10-20 13:53:41 +00:00
|
|
|
if !ok || (status.Code != http.StatusTooManyRequests && status.Code < http.StatusInternalServerError) {
|
2015-07-20 17:45:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(backoff - time.Second + (time.Duration(rand.Int31n(1000)) * time.Millisecond))
|
|
|
|
if i <= 4 {
|
|
|
|
backoff = backoff * 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat retrieves the FileInfo for the given path, including the current
|
|
|
|
// size in bytes and the creation time.
|
2023-05-23 08:42:39 +00:00
|
|
|
func (d *driver) Stat(ctx context.Context, path string) (storagedriver.FileInfo, error) {
|
2015-07-20 17:45:15 +00:00
|
|
|
var fi storagedriver.FileInfoFields
|
2022-11-02 21:05:45 +00:00
|
|
|
// try to get as file
|
2023-10-20 13:53:41 +00:00
|
|
|
obj, err := d.bucket.Object(d.pathToKey(path)).Attrs(ctx)
|
2015-07-20 17:45:15 +00:00
|
|
|
if err == nil {
|
2016-02-12 13:30:57 +00:00
|
|
|
if obj.ContentType == uploadSessionContentType {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
2015-07-20 17:45:15 +00:00
|
|
|
fi = storagedriver.FileInfoFields{
|
|
|
|
Path: path,
|
|
|
|
Size: obj.Size,
|
|
|
|
ModTime: obj.Updated,
|
|
|
|
IsDir: false,
|
|
|
|
}
|
|
|
|
return storagedriver.FileInfoInternal{FileInfoFields: fi}, nil
|
|
|
|
}
|
2022-11-02 21:05:45 +00:00
|
|
|
// try to get as folder
|
2015-07-20 17:45:15 +00:00
|
|
|
dirpath := d.pathToDirKey(path)
|
|
|
|
|
2023-06-28 10:41:22 +00:00
|
|
|
query := &storage.Query{
|
|
|
|
Prefix: dirpath,
|
|
|
|
}
|
2015-07-20 17:45:15 +00:00
|
|
|
|
2023-10-20 13:53:41 +00:00
|
|
|
obj, err = d.bucket.Objects(ctx, query).Next()
|
2015-07-20 17:45:15 +00:00
|
|
|
if err != nil {
|
2023-10-20 13:53:41 +00:00
|
|
|
if err == iterator.Done {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
2015-07-20 17:45:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
|
2015-07-20 17:45:15 +00:00
|
|
|
fi = storagedriver.FileInfoFields{
|
|
|
|
Path: path,
|
|
|
|
IsDir: true,
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
|
2015-07-20 17:45:15 +00:00
|
|
|
if obj.Name == dirpath {
|
|
|
|
fi.Size = obj.Size
|
|
|
|
fi.ModTime = obj.Updated
|
|
|
|
}
|
|
|
|
return storagedriver.FileInfoInternal{FileInfoFields: fi}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// List returns a list of the objects that are direct descendants of the
|
2022-11-02 21:05:45 +00:00
|
|
|
// given path.
|
2023-05-23 08:42:39 +00:00
|
|
|
func (d *driver) List(ctx context.Context, path string) ([]string, error) {
|
2023-06-28 10:41:22 +00:00
|
|
|
query := &storage.Query{
|
|
|
|
Delimiter: "/",
|
|
|
|
Prefix: d.pathToDirKey(path),
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
objects := d.bucket.Objects(ctx, query)
|
|
|
|
|
2015-07-20 17:45:15 +00:00
|
|
|
list := make([]string, 0, 64)
|
2023-10-20 13:53:41 +00:00
|
|
|
for {
|
|
|
|
object, err := objects.Next()
|
|
|
|
if err != nil {
|
|
|
|
if err == iterator.Done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-23 08:42:39 +00:00
|
|
|
// GCS does not guarantee strong consistency between
|
|
|
|
// DELETE and LIST operations. Check that the object is not deleted,
|
|
|
|
// and filter out any objects with a non-zero time-deleted
|
|
|
|
if object.Deleted.IsZero() && object.ContentType != uploadSessionContentType && object.Name != "" {
|
|
|
|
list = append(list, d.keyToPath(object.Name))
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
2023-05-23 08:42:39 +00:00
|
|
|
|
|
|
|
if object.Name == "" && object.Prefix != "" {
|
|
|
|
subpath := d.keyToPath(object.Prefix)
|
2015-07-20 17:45:15 +00:00
|
|
|
list = append(list, subpath)
|
|
|
|
}
|
|
|
|
}
|
2023-05-23 08:42:39 +00:00
|
|
|
|
2016-01-06 18:17:17 +00:00
|
|
|
if path != "/" && len(list) == 0 {
|
|
|
|
// Treat empty response as missing directory, since we don't actually
|
|
|
|
// have directories in Google Cloud Storage.
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
2015-07-20 17:45:15 +00:00
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move moves an object stored at sourcePath to destPath, removing the
|
|
|
|
// original object.
|
2023-05-23 08:42:39 +00:00
|
|
|
func (d *driver) Move(ctx context.Context, sourcePath string, destPath string) error {
|
2023-10-20 13:53:41 +00:00
|
|
|
srcKey, dstKey := d.pathToKey(sourcePath), d.pathToKey(destPath)
|
|
|
|
src := d.bucket.Object(srcKey)
|
|
|
|
_, err := d.bucket.Object(dstKey).CopierFrom(src).Run(ctx)
|
2015-07-20 17:45:15 +00:00
|
|
|
if err != nil {
|
2023-10-20 13:53:41 +00:00
|
|
|
var status *googleapi.Error
|
|
|
|
if errors.As(err, &status) {
|
2015-07-20 17:45:15 +00:00
|
|
|
if status.Code == http.StatusNotFound {
|
2023-10-20 13:53:41 +00:00
|
|
|
return storagedriver.PathNotFoundError{Path: srcKey}
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
return fmt.Errorf("move %q to %q: %v", srcKey, dstKey, err)
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
err = src.Delete(ctx)
|
2016-03-08 23:13:24 +00:00
|
|
|
// if deleting the file fails, log the error, but do not fail; the file was successfully copied,
|
2016-01-22 14:40:21 +00:00
|
|
|
// and the original should eventually be cleaned when purging the uploads folder.
|
|
|
|
if err != nil {
|
2023-10-20 13:53:41 +00:00
|
|
|
logrus.Infof("error deleting %v: %v", sourcePath, err)
|
2016-01-22 14:40:21 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// listAll recursively lists all names of objects stored at "prefix" and its subpaths.
|
2023-05-23 08:42:39 +00:00
|
|
|
func (d *driver) listAll(ctx context.Context, prefix string) ([]string, error) {
|
2023-10-20 13:53:41 +00:00
|
|
|
objects := d.bucket.Objects(ctx, &storage.Query{
|
|
|
|
Prefix: prefix,
|
|
|
|
Versions: false,
|
|
|
|
})
|
|
|
|
|
2015-07-20 17:45:15 +00:00
|
|
|
list := make([]string, 0, 64)
|
2023-10-20 13:53:41 +00:00
|
|
|
for {
|
|
|
|
object, err := objects.Next()
|
|
|
|
if err != nil {
|
|
|
|
if err == iterator.Done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-23 08:42:39 +00:00
|
|
|
// GCS does not guarantee strong consistency between
|
|
|
|
// DELETE and LIST operations. Check that the object is not deleted,
|
|
|
|
// and filter out any objects with a non-zero time-deleted
|
2023-10-20 13:53:41 +00:00
|
|
|
if object.Deleted.IsZero() {
|
|
|
|
list = append(list, object.Name)
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
|
2015-07-20 17:45:15 +00:00
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete recursively deletes all objects stored at "path" and its subpaths.
|
2023-05-23 08:42:39 +00:00
|
|
|
func (d *driver) Delete(ctx context.Context, path string) error {
|
2015-07-20 17:45:15 +00:00
|
|
|
prefix := d.pathToDirKey(path)
|
2023-05-23 08:42:39 +00:00
|
|
|
keys, err := d.listAll(ctx, prefix)
|
2015-07-20 17:45:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(keys) > 0 {
|
2023-10-20 13:53:41 +00:00
|
|
|
// NOTE(milosgajdos): d.listAll calls (BucketHandle).Objects
|
|
|
|
// See: https://pkg.go.dev/cloud.google.com/go/storage#BucketHandle.Objects
|
|
|
|
// docs: Objects will be iterated over lexicographically by name.
|
|
|
|
// This means we don't have to reverse order the slice; we can
|
|
|
|
// range over the keys slice in reverse order
|
|
|
|
for i := len(keys) - 1; i >= 0; i-- {
|
|
|
|
key := keys[i]
|
|
|
|
err := d.bucket.Object(key).Delete(ctx)
|
2016-01-19 14:40:00 +00:00
|
|
|
// GCS only guarantees eventual consistency, so listAll might return
|
2016-01-19 14:09:32 +00:00
|
|
|
// paths that no longer exist. If this happens, just ignore any not
|
|
|
|
// found error
|
|
|
|
if status, ok := err.(*googleapi.Error); ok {
|
|
|
|
if status.Code == http.StatusNotFound {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
2015-07-20 17:45:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
err = d.bucket.Object(d.pathToKey(path)).Delete(ctx)
|
2023-05-23 08:42:39 +00:00
|
|
|
if err == storage.ErrObjectNotExist {
|
|
|
|
return storagedriver.PathNotFoundError{Path: path}
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-24 19:49:47 +00:00
|
|
|
// RedirectURL returns a URL which may be used to retrieve the content stored at
|
2015-07-20 17:45:15 +00:00
|
|
|
// the given path, possibly using the given options.
|
2023-10-24 19:49:47 +00:00
|
|
|
func (d *driver) RedirectURL(r *http.Request, path string) (string, error) {
|
2015-07-20 17:45:15 +00:00
|
|
|
if d.privateKey == nil {
|
2023-10-24 19:49:47 +00:00
|
|
|
return "", nil
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 19:49:47 +00:00
|
|
|
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
|
|
|
return "", nil
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
opts := &storage.SignedURLOptions{
|
|
|
|
GoogleAccessID: d.email,
|
|
|
|
PrivateKey: d.privateKey,
|
2023-10-24 19:49:47 +00:00
|
|
|
Method: r.Method,
|
|
|
|
Expires: time.Now().Add(20 * time.Minute),
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
return d.bucket.SignedURL(d.pathToKey(path), opts)
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 19:17:39 +00:00
|
|
|
// Walk traverses a filesystem defined within driver, starting
|
|
|
|
// from the given path, calling f on each file
|
2022-07-10 02:04:50 +00:00
|
|
|
func (d *driver) Walk(ctx context.Context, path string, f storagedriver.WalkFn, options ...func(*storagedriver.WalkOptions)) error {
|
|
|
|
return storagedriver.WalkFallback(ctx, d, path, f, options...)
|
2017-11-29 19:17:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 13:53:41 +00:00
|
|
|
func (w *writer) newSession() (uri string, err error) {
|
2016-02-12 13:30:57 +00:00
|
|
|
u := &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "www.googleapis.com",
|
2023-10-20 13:53:41 +00:00
|
|
|
Path: fmt.Sprintf("/upload/storage/v1/b/%v/o", w.object.BucketName()),
|
|
|
|
RawQuery: fmt.Sprintf("uploadType=resumable&name=%v", w.object.ObjectName()),
|
2016-02-12 13:30:57 +00:00
|
|
|
}
|
2023-10-20 13:53:41 +00:00
|
|
|
req, err := http.NewRequestWithContext(w.ctx, http.MethodPost, u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
req.Header.Set("X-Upload-Content-Type", blobContentType)
|
|
|
|
req.Header.Set("Content-Length", "0")
|
|
|
|
|
2016-02-12 13:30:57 +00:00
|
|
|
err = retry(func() error {
|
2023-10-20 13:53:41 +00:00
|
|
|
resp, err := w.driver.client.Do(req)
|
2016-02-12 13:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
err = googleapi.CheckMediaResponse(resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
uri = resp.Header.Get("Location")
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return uri, err
|
|
|
|
}
|
|
|
|
|
2023-10-20 13:53:41 +00:00
|
|
|
func (w *writer) putChunk(ctx context.Context, sessionURI string, chunk []byte, from int64, totalSize int64) (int64, error) {
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, sessionURI, bytes.NewReader(chunk))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
length := int64(len(chunk))
|
|
|
|
to := from + length - 1
|
|
|
|
size := "*"
|
|
|
|
if totalSize >= 0 {
|
|
|
|
size = strconv.FormatInt(totalSize, 10)
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", blobContentType)
|
|
|
|
if from == to+1 {
|
|
|
|
req.Header.Set("Content-Range", fmt.Sprintf("bytes */%s", size))
|
|
|
|
} else {
|
|
|
|
req.Header.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%s", from, to, size))
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Length", strconv.FormatInt(length, 10))
|
2016-02-12 13:30:57 +00:00
|
|
|
|
2023-10-20 13:53:41 +00:00
|
|
|
bytesPut := int64(0)
|
|
|
|
err = retry(func() error {
|
|
|
|
resp, err := w.driver.client.Do(req)
|
2016-02-12 13:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2023-10-20 13:53:41 +00:00
|
|
|
if totalSize < 0 && resp.StatusCode == http.StatusPermanentRedirect {
|
2016-02-12 13:30:57 +00:00
|
|
|
groups := rangeHeader.FindStringSubmatch(resp.Header.Get("Range"))
|
|
|
|
end, err := strconv.ParseInt(groups[2], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bytesPut = end - from + 1
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = googleapi.CheckMediaResponse(resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bytesPut = to - from + 1
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return bytesPut, err
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:45:15 +00:00
|
|
|
func (d *driver) pathToKey(path string) string {
|
2016-12-06 23:50:39 +00:00
|
|
|
return strings.TrimSpace(strings.TrimRight(d.rootDirectory+strings.TrimLeft(path, "/"), "/"))
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) pathToDirKey(path string) string {
|
|
|
|
return d.pathToKey(path) + "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) keyToPath(key string) string {
|
|
|
|
return "/" + strings.Trim(strings.TrimPrefix(key, d.rootDirectory), "/")
|
|
|
|
}
|