2021-04-29 22:01:43 -04:00
|
|
|
/*
|
|
|
|
* MinIO Object Storage (c) 2021 MinIO, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2017-05-01 13:59:54 -04:00
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
package gcs
|
2017-05-01 13:59:54 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-05-12 01:38:22 -04:00
|
|
|
"encoding/base64"
|
2019-05-16 15:00:12 -04:00
|
|
|
"encoding/hex"
|
2017-06-17 19:00:41 -04:00
|
|
|
"encoding/json"
|
2018-09-04 16:11:33 -04:00
|
|
|
"errors"
|
2017-05-01 13:59:54 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-10-20 16:59:12 -04:00
|
|
|
"io/ioutil"
|
2017-06-28 01:27:05 -04:00
|
|
|
"math"
|
2018-08-14 14:53:39 -04:00
|
|
|
"net/http"
|
2019-10-11 21:50:54 -04:00
|
|
|
"os"
|
2018-09-04 16:11:33 -04:00
|
|
|
"path"
|
2017-05-24 21:39:45 -04:00
|
|
|
"regexp"
|
2021-03-04 17:36:23 -05:00
|
|
|
"strconv"
|
2017-05-01 13:59:54 -04:00
|
|
|
"strings"
|
2017-07-19 22:33:10 -04:00
|
|
|
"time"
|
2017-05-01 13:59:54 -04:00
|
|
|
|
|
|
|
"cloud.google.com/go/storage"
|
2017-12-05 20:58:09 -05:00
|
|
|
humanize "github.com/dustin/go-humanize"
|
2017-10-27 18:07:46 -04:00
|
|
|
"github.com/minio/cli"
|
2021-05-06 11:52:02 -04:00
|
|
|
"github.com/minio/madmin-go"
|
2020-07-14 12:38:05 -04:00
|
|
|
miniogopolicy "github.com/minio/minio-go/v7/pkg/policy"
|
2021-03-04 17:36:23 -05:00
|
|
|
minio "github.com/minio/minio/cmd"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/logger"
|
2021-05-30 00:16:42 -04:00
|
|
|
"github.com/minio/pkg/bucket/policy"
|
|
|
|
"github.com/minio/pkg/bucket/policy/condition"
|
2021-05-28 18:17:01 -04:00
|
|
|
"github.com/minio/pkg/env"
|
2017-10-27 18:07:46 -04:00
|
|
|
"google.golang.org/api/googleapi"
|
|
|
|
"google.golang.org/api/iterator"
|
2017-11-13 22:06:51 -05:00
|
|
|
"google.golang.org/api/option"
|
2017-05-01 13:59:54 -04:00
|
|
|
)
|
|
|
|
|
2017-10-13 06:56:16 -04:00
|
|
|
var (
|
|
|
|
// Project ID format is not valid.
|
2017-12-05 20:58:09 -05:00
|
|
|
errGCSInvalidProjectID = fmt.Errorf("GCS project id is either empty or invalid")
|
2017-10-13 06:56:16 -04:00
|
|
|
|
|
|
|
// Project ID not found
|
2017-12-05 20:58:09 -05:00
|
|
|
errGCSProjectIDNotFound = fmt.Errorf("Unknown project id")
|
|
|
|
|
|
|
|
// Invalid format.
|
|
|
|
errGCSFormat = fmt.Errorf("Unknown format")
|
2017-10-13 06:56:16 -04:00
|
|
|
)
|
|
|
|
|
2017-05-05 21:49:26 -04:00
|
|
|
const (
|
2017-06-17 19:00:41 -04:00
|
|
|
// Path where multipart objects are saved.
|
2017-06-21 13:27:44 -04:00
|
|
|
// If we change the backend format we will use a different url path like /multipart/v2
|
|
|
|
// but we will not migrate old data.
|
2017-12-05 20:58:09 -05:00
|
|
|
gcsMinioMultipartPathV1 = minio.GatewayMinioSysTmp + "multipart/v1"
|
2017-07-19 22:33:10 -04:00
|
|
|
|
2017-06-17 19:00:41 -04:00
|
|
|
// Multipart meta file.
|
|
|
|
gcsMinioMultipartMeta = "gcs.json"
|
2017-07-19 22:33:10 -04:00
|
|
|
|
2017-06-17 19:00:41 -04:00
|
|
|
// gcs.json version number
|
2017-07-11 12:25:19 -04:00
|
|
|
gcsMinioMultipartMetaCurrentVersion = "1"
|
|
|
|
|
2017-06-09 22:48:20 -04:00
|
|
|
// token prefixed with GCS returned marker to differentiate
|
|
|
|
// from user supplied marker.
|
2017-07-19 22:33:10 -04:00
|
|
|
gcsTokenPrefix = "{minio}"
|
2017-06-28 01:27:05 -04:00
|
|
|
|
2017-07-19 22:33:10 -04:00
|
|
|
// Maximum component object count to create a composite object.
|
2017-06-28 01:27:05 -04:00
|
|
|
// Refer https://cloud.google.com/storage/docs/composite-objects
|
2017-07-19 22:33:10 -04:00
|
|
|
gcsMaxComponents = 32
|
|
|
|
|
|
|
|
// Every 24 hours we scan minio.sys.tmp to delete expired multiparts in minio.sys.tmp
|
|
|
|
gcsCleanupInterval = time.Hour * 24
|
|
|
|
|
|
|
|
// The cleanup routine deletes files older than 2 weeks in minio.sys.tmp
|
|
|
|
gcsMultipartExpiry = time.Hour * 24 * 14
|
2017-10-20 16:59:12 -04:00
|
|
|
|
|
|
|
// Project ID key in credentials.json
|
|
|
|
gcsProjectIDKey = "project_id"
|
2017-05-05 21:49:26 -04:00
|
|
|
)
|
|
|
|
|
2017-10-27 18:07:46 -04:00
|
|
|
func init() {
|
|
|
|
const gcsGatewayTemplate = `NAME:
|
|
|
|
{{.HelpName}} - {{.Usage}}
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
{{.HelpName}} {{if .VisibleFlags}}[FLAGS]{{end}} [PROJECTID]
|
|
|
|
{{if .VisibleFlags}}
|
|
|
|
FLAGS:
|
|
|
|
{{range .VisibleFlags}}{{.}}
|
|
|
|
{{end}}{{end}}
|
|
|
|
PROJECTID:
|
2019-12-16 23:30:57 -05:00
|
|
|
optional GCS project-id expected GOOGLE_APPLICATION_CREDENTIALS env is not set
|
2017-10-27 18:07:46 -04:00
|
|
|
|
2019-12-16 23:30:57 -05:00
|
|
|
GOOGLE_APPLICATION_CREDENTIALS:
|
|
|
|
path to credentials.json, generated it from here https://developers.google.com/identity/protocols/application-default-credentials
|
2017-10-27 18:07:46 -04:00
|
|
|
|
|
|
|
EXAMPLES:
|
2019-12-16 23:30:57 -05:00
|
|
|
1. Start minio gateway server for GCS backend
|
2019-05-15 04:32:44 -04:00
|
|
|
{{.Prompt}} {{.EnvVarSetCommand}} GOOGLE_APPLICATION_CREDENTIALS{{.AssignmentOperator}}/path/to/credentials.json
|
2021-01-05 13:22:57 -05:00
|
|
|
{{.Prompt}} {{.EnvVarSetCommand}} MINIO_ROOT_USER{{.AssignmentOperator}}accesskey
|
|
|
|
{{.Prompt}} {{.EnvVarSetCommand}} MINIO_ROOT_PASSWORD{{.AssignmentOperator}}secretkey
|
2019-05-15 04:32:44 -04:00
|
|
|
{{.Prompt}} {{.HelpName}} mygcsprojectid
|
2018-03-28 17:14:06 -04:00
|
|
|
|
2019-12-16 23:30:57 -05:00
|
|
|
2. Start minio gateway server for GCS backend with edge caching enabled
|
2019-05-15 04:32:44 -04:00
|
|
|
{{.Prompt}} {{.EnvVarSetCommand}} GOOGLE_APPLICATION_CREDENTIALS{{.AssignmentOperator}}/path/to/credentials.json
|
2021-01-05 13:22:57 -05:00
|
|
|
{{.Prompt}} {{.EnvVarSetCommand}} MINIO_ROOT_USER{{.AssignmentOperator}}accesskey
|
|
|
|
{{.Prompt}} {{.EnvVarSetCommand}} MINIO_ROOT_PASSWORD{{.AssignmentOperator}}secretkey
|
2019-11-20 18:10:24 -05:00
|
|
|
{{.Prompt}} {{.EnvVarSetCommand}} MINIO_CACHE_DRIVES{{.AssignmentOperator}}"/mnt/drive1,/mnt/drive2,/mnt/drive3,/mnt/drive4"
|
2019-05-15 04:32:44 -04:00
|
|
|
{{.Prompt}} {{.EnvVarSetCommand}} MINIO_CACHE_EXCLUDE{{.AssignmentOperator}}"bucket1/*;*.png"
|
2020-02-23 08:33:39 -05:00
|
|
|
{{.Prompt}} {{.EnvVarSetCommand}} MINIO_CACHE_AFTER{{.AssignmentOperator}}3
|
|
|
|
{{.Prompt}} {{.EnvVarSetCommand}} MINIO_CACHE_WATERMARK_LOW{{.AssignmentOperator}}75
|
|
|
|
{{.Prompt}} {{.EnvVarSetCommand}} MINIO_CACHE_WATERMARK_HIGH{{.AssignmentOperator}}85
|
|
|
|
{{.Prompt}} {{.EnvVarSetCommand}} MINIO_CACHE_QUOTA{{.AssignmentOperator}}90
|
2019-05-15 04:32:44 -04:00
|
|
|
{{.Prompt}} {{.HelpName}} mygcsprojectid
|
2017-10-27 18:07:46 -04:00
|
|
|
`
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
minio.RegisterGatewayCommand(cli.Command{
|
2020-08-26 11:52:46 -04:00
|
|
|
Name: minio.GCSBackendGateway,
|
2018-11-20 20:35:33 -05:00
|
|
|
Usage: "Google Cloud Storage",
|
2017-10-27 18:07:46 -04:00
|
|
|
Action: gcsGatewayMain,
|
|
|
|
CustomHelpTemplate: gcsGatewayTemplate,
|
|
|
|
HideHelpCommand: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler for 'minio gateway gcs' command line.
|
|
|
|
func gcsGatewayMain(ctx *cli.Context) {
|
|
|
|
projectID := ctx.Args().First()
|
2019-10-11 21:50:54 -04:00
|
|
|
if projectID == "" && os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") == "" {
|
2020-04-09 12:30:02 -04:00
|
|
|
logger.LogIf(minio.GlobalContext, errGCSProjectIDNotFound, logger.Application)
|
2020-08-26 11:52:46 -04:00
|
|
|
cli.ShowCommandHelpAndExit(ctx, minio.GCSBackendGateway, 1)
|
2017-10-27 18:07:46 -04:00
|
|
|
}
|
|
|
|
if projectID != "" && !isValidGCSProjectIDFormat(projectID) {
|
2018-04-05 18:04:40 -04:00
|
|
|
reqInfo := (&logger.ReqInfo{}).AppendTags("projectID", ctx.Args().First())
|
2020-04-09 12:30:02 -04:00
|
|
|
contxt := logger.SetReqInfo(minio.GlobalContext, reqInfo)
|
2019-10-11 21:50:54 -04:00
|
|
|
logger.LogIf(contxt, errGCSInvalidProjectID, logger.Application)
|
2020-08-26 11:52:46 -04:00
|
|
|
cli.ShowCommandHelpAndExit(ctx, minio.GCSBackendGateway, 1)
|
2017-10-27 18:07:46 -04:00
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
minio.StartGateway(ctx, &GCS{projectID})
|
2017-10-27 18:07:46 -04:00
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
// GCS implements Azure.
|
|
|
|
type GCS struct {
|
2017-10-27 18:07:46 -04:00
|
|
|
projectID string
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:19:30 -05:00
|
|
|
// Name returns the name of gcs ObjectLayer.
|
2017-12-05 20:58:09 -05:00
|
|
|
func (g *GCS) Name() string {
|
2020-08-26 11:52:46 -04:00
|
|
|
return minio.GCSBackendGateway
|
2017-10-27 18:07:46 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 18:19:30 -05:00
|
|
|
// NewGatewayLayer returns gcs ObjectLayer.
|
2021-06-14 15:53:49 -04:00
|
|
|
func (g *GCS) NewGatewayLayer(creds madmin.Credentials) (minio.ObjectLayer, error) {
|
2020-04-09 12:30:02 -04:00
|
|
|
ctx := minio.GlobalContext
|
2017-12-05 20:58:09 -05:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if g.projectID == "" {
|
|
|
|
// If project ID is not provided on command line, we figure it out
|
|
|
|
// from the credentials.json file.
|
2019-10-04 13:35:33 -04:00
|
|
|
g.projectID, err = gcsParseProjectID(env.Get("GOOGLE_APPLICATION_CREDENTIALS", ""))
|
2017-12-05 20:58:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 10:38:01 -05:00
|
|
|
metrics := minio.NewMetrics()
|
|
|
|
|
|
|
|
t := &minio.MetricsTransport{
|
2020-03-22 01:10:13 -04:00
|
|
|
Transport: minio.NewGatewayHTTPTransport(),
|
2020-02-11 10:38:01 -05:00
|
|
|
Metrics: metrics,
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
// Initialize a GCS client.
|
|
|
|
// Send user-agent in this format for Google to obtain usage insights while participating in the
|
|
|
|
// Google Cloud Technology Partners (https://cloud.google.com/partners/)
|
2019-04-09 14:39:42 -04:00
|
|
|
client, err := storage.NewClient(ctx, option.WithUserAgent(fmt.Sprintf("MinIO/%s (GPN:MinIO;)", minio.Version)))
|
2017-12-05 20:58:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
gcs := &gcsGateway{
|
2018-02-09 18:19:30 -05:00
|
|
|
client: client,
|
|
|
|
projectID: g.projectID,
|
2020-02-11 10:38:01 -05:00
|
|
|
metrics: metrics,
|
2019-11-01 19:58:11 -04:00
|
|
|
httpClient: &http.Client{
|
2020-02-11 10:38:01 -05:00
|
|
|
Transport: t,
|
2019-11-01 19:58:11 -04:00
|
|
|
},
|
2017-12-05 20:58:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start background process to cleanup old files in minio.sys.tmp
|
2018-11-28 03:19:51 -05:00
|
|
|
go gcs.CleanupGCSMinioSysTmp(ctx)
|
2017-12-05 20:58:09 -05:00
|
|
|
return gcs, nil
|
|
|
|
}
|
|
|
|
|
2017-06-17 19:00:41 -04:00
|
|
|
// Stored in gcs.json - Contents of this file is not used anywhere. It can be
|
|
|
|
// used for debugging purposes.
|
|
|
|
type gcsMultipartMetaV1 struct {
|
|
|
|
Version string `json:"version"` // Version number
|
|
|
|
Bucket string `json:"bucket"` // Bucket name
|
|
|
|
Object string `json:"object"` // Object name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns name of the multipart meta object.
|
|
|
|
func gcsMultipartMetaName(uploadID string) string {
|
2017-06-21 13:27:44 -04:00
|
|
|
return fmt.Sprintf("%s/%s/%s", gcsMinioMultipartPathV1, uploadID, gcsMinioMultipartMeta)
|
2017-06-17 19:00:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns name of the part object.
|
2017-07-14 02:20:16 -04:00
|
|
|
func gcsMultipartDataName(uploadID string, partNumber int, etag string) string {
|
|
|
|
return fmt.Sprintf("%s/%s/%05d.%s", gcsMinioMultipartPathV1, uploadID, partNumber, etag)
|
2017-06-06 02:13:53 -04:00
|
|
|
}
|
|
|
|
|
2019-04-09 14:39:42 -04:00
|
|
|
// Convert MinIO errors to minio object layer errors.
|
2017-05-01 13:59:54 -04:00
|
|
|
func gcsToObjectError(err error, params ...string) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := ""
|
|
|
|
object := ""
|
2017-11-06 13:09:21 -05:00
|
|
|
uploadID := ""
|
2017-05-01 13:59:54 -04:00
|
|
|
if len(params) >= 1 {
|
|
|
|
bucket = params[0]
|
|
|
|
}
|
|
|
|
if len(params) == 2 {
|
|
|
|
object = params[1]
|
|
|
|
}
|
2017-11-06 13:09:21 -05:00
|
|
|
if len(params) == 3 {
|
|
|
|
uploadID = params[2]
|
|
|
|
}
|
2017-05-01 13:59:54 -04:00
|
|
|
|
|
|
|
// in some cases just a plain error is being returned
|
|
|
|
switch err.Error() {
|
|
|
|
case "storage: bucket doesn't exist":
|
2017-12-05 20:58:09 -05:00
|
|
|
err = minio.BucketNotFound{
|
2017-05-01 13:59:54 -04:00
|
|
|
Bucket: bucket,
|
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
return err
|
2017-05-01 13:59:54 -04:00
|
|
|
case "storage: object doesn't exist":
|
2017-11-06 13:09:21 -05:00
|
|
|
if uploadID != "" {
|
2017-12-05 20:58:09 -05:00
|
|
|
err = minio.InvalidUploadID{
|
2017-11-06 13:09:21 -05:00
|
|
|
UploadID: uploadID,
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-05 20:58:09 -05:00
|
|
|
err = minio.ObjectNotFound{
|
2017-11-06 13:09:21 -05:00
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
}
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
return err
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-05-05 21:49:26 -04:00
|
|
|
googleAPIErr, ok := err.(*googleapi.Error)
|
2017-05-01 13:59:54 -04:00
|
|
|
if !ok {
|
2019-04-09 14:39:42 -04:00
|
|
|
// We don't interpret non MinIO errors. As minio errors will
|
2017-05-01 13:59:54 -04:00
|
|
|
// have StatusCode to help to convert to object errors.
|
2018-04-05 18:04:40 -04:00
|
|
|
return err
|
2017-06-17 19:00:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(googleAPIErr.Errors) == 0 {
|
2018-04-05 18:04:40 -04:00
|
|
|
return err
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-05-05 21:49:26 -04:00
|
|
|
reason := googleAPIErr.Errors[0].Reason
|
|
|
|
message := googleAPIErr.Errors[0].Message
|
2017-05-01 13:59:54 -04:00
|
|
|
|
|
|
|
switch reason {
|
2017-05-08 17:07:51 -04:00
|
|
|
case "required":
|
|
|
|
// Anonymous users does not have storage.xyz access to project 123.
|
|
|
|
fallthrough
|
|
|
|
case "keyInvalid":
|
|
|
|
fallthrough
|
2017-05-01 13:59:54 -04:00
|
|
|
case "forbidden":
|
2017-12-05 20:58:09 -05:00
|
|
|
err = minio.PrefixAccessDenied{
|
2017-05-01 13:59:54 -04:00
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
}
|
|
|
|
case "invalid":
|
2017-12-05 20:58:09 -05:00
|
|
|
err = minio.BucketNameInvalid{
|
2017-05-01 13:59:54 -04:00
|
|
|
Bucket: bucket,
|
|
|
|
}
|
|
|
|
case "notFound":
|
|
|
|
if object != "" {
|
2017-12-05 20:58:09 -05:00
|
|
|
err = minio.ObjectNotFound{
|
2017-05-01 13:59:54 -04:00
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
}
|
2017-06-23 20:57:25 -04:00
|
|
|
break
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
2017-12-05 20:58:09 -05:00
|
|
|
err = minio.BucketNotFound{Bucket: bucket}
|
2017-05-01 13:59:54 -04:00
|
|
|
case "conflict":
|
|
|
|
if message == "You already own this bucket. Please select another name." {
|
2017-12-05 20:58:09 -05:00
|
|
|
err = minio.BucketAlreadyOwnedByYou{Bucket: bucket}
|
2017-06-23 20:57:25 -04:00
|
|
|
break
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
2017-06-23 20:57:25 -04:00
|
|
|
if message == "Sorry, that name is not available. Please try a different one." {
|
2017-12-05 20:58:09 -05:00
|
|
|
err = minio.BucketAlreadyExists{Bucket: bucket}
|
2017-06-23 20:57:25 -04:00
|
|
|
break
|
|
|
|
}
|
2017-12-05 20:58:09 -05:00
|
|
|
err = minio.BucketNotEmpty{Bucket: bucket}
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
return err
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-05-24 21:39:45 -04:00
|
|
|
// gcsProjectIDRegex defines a valid gcs project id format
|
|
|
|
var gcsProjectIDRegex = regexp.MustCompile("^[a-z][a-z0-9-]{5,29}$")
|
|
|
|
|
2017-06-24 01:10:29 -04:00
|
|
|
// isValidGCSProjectIDFormat - checks if a given project id format is valid or not.
|
|
|
|
// Project IDs must start with a lowercase letter and can have lowercase ASCII letters,
|
|
|
|
// digits or hyphens. Project IDs must be between 6 and 30 characters.
|
2017-05-24 21:39:45 -04:00
|
|
|
// Ref: https://cloud.google.com/resource-manager/reference/rest/v1/projects#Project (projectId section)
|
2017-06-24 01:10:29 -04:00
|
|
|
func isValidGCSProjectIDFormat(projectID string) bool {
|
|
|
|
// Checking projectID format
|
2017-05-24 21:39:45 -04:00
|
|
|
return gcsProjectIDRegex.MatchString(projectID)
|
|
|
|
}
|
|
|
|
|
2019-04-09 14:39:42 -04:00
|
|
|
// gcsGateway - Implements gateway for MinIO and GCS compatible object storage servers.
|
2017-05-01 13:59:54 -04:00
|
|
|
type gcsGateway struct {
|
2017-12-05 20:58:09 -05:00
|
|
|
minio.GatewayUnsupported
|
2019-11-01 19:58:11 -04:00
|
|
|
client *storage.Client
|
|
|
|
httpClient *http.Client
|
2021-01-18 23:35:38 -05:00
|
|
|
metrics *minio.BackendMetrics
|
2019-11-01 19:58:11 -04:00
|
|
|
projectID string
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-10-20 16:59:12 -04:00
|
|
|
// Returns projectID from the GOOGLE_APPLICATION_CREDENTIALS file.
|
|
|
|
func gcsParseProjectID(credsFile string) (projectID string, err error) {
|
|
|
|
contents, err := ioutil.ReadFile(credsFile)
|
|
|
|
if err != nil {
|
|
|
|
return projectID, err
|
|
|
|
}
|
|
|
|
googleCreds := make(map[string]string)
|
|
|
|
if err = json.Unmarshal(contents, &googleCreds); err != nil {
|
|
|
|
return projectID, err
|
|
|
|
}
|
|
|
|
return googleCreds[gcsProjectIDKey], err
|
|
|
|
}
|
|
|
|
|
2020-02-11 10:38:01 -05:00
|
|
|
// GetMetrics returns this gateway's metrics
|
2021-01-18 23:35:38 -05:00
|
|
|
func (l *gcsGateway) GetMetrics(ctx context.Context) (*minio.BackendMetrics, error) {
|
2020-02-11 10:38:01 -05:00
|
|
|
return l.metrics, nil
|
|
|
|
}
|
|
|
|
|
2017-07-19 22:33:10 -04:00
|
|
|
// Cleanup old files in minio.sys.tmp of the given bucket.
|
2018-11-28 03:19:51 -05:00
|
|
|
func (l *gcsGateway) CleanupGCSMinioSysTmpBucket(ctx context.Context, bucket string) {
|
|
|
|
it := l.client.Bucket(bucket).Objects(ctx, &storage.Query{Prefix: minio.GatewayMinioSysTmp, Versions: false})
|
2017-07-19 22:33:10 -04:00
|
|
|
for {
|
|
|
|
attrs, err := it.Next()
|
|
|
|
if err != nil {
|
|
|
|
if err != iterator.Done {
|
2018-04-05 18:04:40 -04:00
|
|
|
reqInfo := &logger.ReqInfo{BucketName: bucket}
|
2020-04-09 12:30:02 -04:00
|
|
|
ctx := logger.SetReqInfo(minio.GlobalContext, reqInfo)
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2017-07-19 22:33:10 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if time.Since(attrs.Updated) > gcsMultipartExpiry {
|
|
|
|
// Delete files older than 2 weeks.
|
2018-11-28 03:19:51 -05:00
|
|
|
err := l.client.Bucket(bucket).Object(attrs.Name).Delete(ctx)
|
2017-07-19 22:33:10 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
reqInfo := &logger.ReqInfo{BucketName: bucket, ObjectName: attrs.Name}
|
2020-04-09 12:30:02 -04:00
|
|
|
ctx := logger.SetReqInfo(minio.GlobalContext, reqInfo)
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2017-07-19 22:33:10 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup old files in minio.sys.tmp of all buckets.
|
2018-11-28 03:19:51 -05:00
|
|
|
func (l *gcsGateway) CleanupGCSMinioSysTmp(ctx context.Context) {
|
2017-07-19 22:33:10 -04:00
|
|
|
for {
|
2018-11-28 03:19:51 -05:00
|
|
|
it := l.client.Buckets(ctx, l.projectID)
|
2017-07-19 22:33:10 -04:00
|
|
|
for {
|
|
|
|
attrs, err := it.Next()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2018-11-28 03:19:51 -05:00
|
|
|
l.CleanupGCSMinioSysTmpBucket(ctx, attrs.Name)
|
2017-07-19 22:33:10 -04:00
|
|
|
}
|
|
|
|
// Run the cleanup loop every 1 day.
|
|
|
|
time.Sleep(gcsCleanupInterval)
|
|
|
|
}
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown - save any gateway metadata to disk
|
|
|
|
// if necessary and reload upon next restart.
|
2018-03-14 20:31:14 -04:00
|
|
|
func (l *gcsGateway) Shutdown(ctx context.Context) error {
|
2017-05-01 13:59:54 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageInfo - Not relevant to GCS backend.
|
2021-01-04 12:42:09 -05:00
|
|
|
func (l *gcsGateway) StorageInfo(ctx context.Context) (si minio.StorageInfo, _ []error) {
|
2021-03-04 17:36:23 -05:00
|
|
|
si.Backend.Type = madmin.Gateway
|
2020-11-19 13:38:02 -05:00
|
|
|
si.Backend.GatewayOnline = minio.IsBackendOnline(ctx, "storage.googleapis.com:443")
|
2020-05-28 16:03:04 -04:00
|
|
|
return si, nil
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-06-17 19:00:41 -04:00
|
|
|
// MakeBucketWithLocation - Create a new container on GCS backend.
|
2020-06-12 23:04:01 -04:00
|
|
|
func (l *gcsGateway) MakeBucketWithLocation(ctx context.Context, bucket string, opts minio.BucketOptions) error {
|
|
|
|
if opts.LockEnabled || opts.VersioningEnabled {
|
2020-05-08 16:44:44 -04:00
|
|
|
return minio.NotImplemented{}
|
|
|
|
}
|
|
|
|
|
2017-05-01 13:59:54 -04:00
|
|
|
bkt := l.client.Bucket(bucket)
|
|
|
|
|
2017-06-17 19:00:41 -04:00
|
|
|
// we'll default to the us multi-region in case of us-east-1
|
2020-06-12 23:04:01 -04:00
|
|
|
location := opts.Location
|
2017-06-17 19:00:41 -04:00
|
|
|
if location == "us-east-1" {
|
|
|
|
location = "us"
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2018-11-28 03:19:51 -05:00
|
|
|
err := bkt.Create(ctx, l.projectID, &storage.BucketAttrs{
|
2017-06-17 19:00:41 -04:00
|
|
|
Location: location,
|
|
|
|
})
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return gcsToObjectError(err, bucket)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketInfo - Get bucket metadata..
|
2018-03-14 20:31:14 -04:00
|
|
|
func (l *gcsGateway) GetBucketInfo(ctx context.Context, bucket string) (minio.BucketInfo, error) {
|
2018-11-28 03:19:51 -05:00
|
|
|
attrs, err := l.client.Bucket(bucket).Attrs(ctx)
|
2017-05-01 13:59:54 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.BucketInfo{}, gcsToObjectError(err, bucket)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.BucketInfo{
|
2017-05-01 13:59:54 -04:00
|
|
|
Name: attrs.Name,
|
|
|
|
Created: attrs.Created,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-07-11 12:25:19 -04:00
|
|
|
// ListBuckets lists all buckets under your project-id on GCS.
|
2018-03-14 20:31:14 -04:00
|
|
|
func (l *gcsGateway) ListBuckets(ctx context.Context) (buckets []minio.BucketInfo, err error) {
|
2018-11-28 03:19:51 -05:00
|
|
|
it := l.client.Buckets(ctx, l.projectID)
|
2017-05-01 13:59:54 -04:00
|
|
|
|
2017-07-11 12:25:19 -04:00
|
|
|
// Iterate and capture all the buckets.
|
2017-05-01 13:59:54 -04:00
|
|
|
for {
|
2017-07-11 12:25:19 -04:00
|
|
|
attrs, ierr := it.Next()
|
|
|
|
if ierr == iterator.Done {
|
2017-05-01 13:59:54 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-07-11 12:25:19 -04:00
|
|
|
if ierr != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
return buckets, gcsToObjectError(ierr)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
buckets = append(buckets, minio.BucketInfo{
|
2017-05-01 13:59:54 -04:00
|
|
|
Name: attrs.Name,
|
|
|
|
Created: attrs.Created,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-11 12:25:19 -04:00
|
|
|
return buckets, nil
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-06-23 20:57:25 -04:00
|
|
|
// DeleteBucket delete a bucket on GCS.
|
2021-10-06 13:24:40 -04:00
|
|
|
func (l *gcsGateway) DeleteBucket(ctx context.Context, bucket string, opts minio.DeleteBucketOptions) error {
|
2018-11-28 03:19:51 -05:00
|
|
|
itObject := l.client.Bucket(bucket).Objects(ctx, &storage.Query{
|
2019-08-06 15:08:58 -04:00
|
|
|
Delimiter: minio.SlashSeparator,
|
2017-12-05 20:58:09 -05:00
|
|
|
Versions: false,
|
|
|
|
})
|
2017-06-23 20:57:25 -04:00
|
|
|
// We list the bucket and if we find any objects we return BucketNotEmpty error. If we
|
2017-07-19 22:33:10 -04:00
|
|
|
// find only "minio.sys.tmp/" then we remove it before deleting the bucket.
|
2017-06-23 20:57:25 -04:00
|
|
|
gcsMinioPathFound := false
|
|
|
|
nonGCSMinioPathFound := false
|
|
|
|
for {
|
|
|
|
objAttrs, err := itObject.Next()
|
|
|
|
if err == iterator.Done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return gcsToObjectError(err)
|
2017-06-23 20:57:25 -04:00
|
|
|
}
|
2017-12-05 20:58:09 -05:00
|
|
|
if objAttrs.Prefix == minio.GatewayMinioSysTmp {
|
2017-06-23 20:57:25 -04:00
|
|
|
gcsMinioPathFound = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nonGCSMinioPathFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if nonGCSMinioPathFound {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, minio.BucketNotEmpty{})
|
|
|
|
return gcsToObjectError(minio.BucketNotEmpty{})
|
2017-06-23 20:57:25 -04:00
|
|
|
}
|
|
|
|
if gcsMinioPathFound {
|
2017-07-19 22:33:10 -04:00
|
|
|
// Remove minio.sys.tmp before deleting the bucket.
|
2018-11-28 03:19:51 -05:00
|
|
|
itObject = l.client.Bucket(bucket).Objects(ctx, &storage.Query{Versions: false, Prefix: minio.GatewayMinioSysTmp})
|
2017-06-23 20:57:25 -04:00
|
|
|
for {
|
|
|
|
objAttrs, err := itObject.Next()
|
|
|
|
if err == iterator.Done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return gcsToObjectError(err)
|
2017-06-23 20:57:25 -04:00
|
|
|
}
|
2018-11-28 03:19:51 -05:00
|
|
|
err = l.client.Bucket(bucket).Object(objAttrs.Name).Delete(ctx)
|
2017-06-23 20:57:25 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return gcsToObjectError(err)
|
2017-06-23 20:57:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-28 03:19:51 -05:00
|
|
|
err := l.client.Bucket(bucket).Delete(ctx)
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return gcsToObjectError(err, bucket)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-05-10 18:36:49 -04:00
|
|
|
func toGCSPageToken(name string) string {
|
|
|
|
length := uint16(len(name))
|
|
|
|
|
|
|
|
b := []byte{
|
|
|
|
0xa,
|
|
|
|
byte(length & 0xFF),
|
|
|
|
}
|
|
|
|
|
2021-11-16 12:28:29 -05:00
|
|
|
length >>= 7
|
2017-05-10 18:36:49 -04:00
|
|
|
if length > 0 {
|
|
|
|
b = append(b, byte(length&0xFF))
|
|
|
|
}
|
|
|
|
|
|
|
|
b = append(b, []byte(name)...)
|
|
|
|
|
|
|
|
return base64.StdEncoding.EncodeToString(b)
|
|
|
|
}
|
|
|
|
|
2017-06-09 22:48:20 -04:00
|
|
|
// Returns true if marker was returned by GCS, i.e prefixed with
|
2017-12-05 20:58:09 -05:00
|
|
|
// ##minio by minio gcs minio.
|
2017-06-09 22:48:20 -04:00
|
|
|
func isGCSMarker(marker string) bool {
|
|
|
|
return strings.HasPrefix(marker, gcsTokenPrefix)
|
|
|
|
}
|
|
|
|
|
2017-05-01 13:59:54 -04:00
|
|
|
// ListObjects - lists all blobs in GCS bucket filtered by prefix
|
2018-03-14 20:31:14 -04:00
|
|
|
func (l *gcsGateway) ListObjects(ctx context.Context, bucket string, prefix string, marker string, delimiter string, maxKeys int) (minio.ListObjectsInfo, error) {
|
2018-07-18 12:19:16 -04:00
|
|
|
if maxKeys == 0 {
|
|
|
|
return minio.ListObjectsInfo{}, nil
|
|
|
|
}
|
|
|
|
|
2018-11-28 03:19:51 -05:00
|
|
|
it := l.client.Bucket(bucket).Objects(ctx, &storage.Query{
|
2017-07-11 12:25:19 -04:00
|
|
|
Delimiter: delimiter,
|
|
|
|
Prefix: prefix,
|
|
|
|
Versions: false,
|
|
|
|
})
|
2017-05-01 13:59:54 -04:00
|
|
|
|
2017-06-09 22:48:20 -04:00
|
|
|
// To accommodate S3-compatible applications using
|
|
|
|
// ListObjectsV1 to use object keys as markers to control the
|
|
|
|
// listing of objects, we use the following encoding scheme to
|
|
|
|
// distinguish between GCS continuation tokens and application
|
|
|
|
// supplied markers.
|
|
|
|
//
|
|
|
|
// - NextMarker in ListObjectsV1 response is constructed by
|
2018-04-27 19:08:46 -04:00
|
|
|
// prefixing "{minio}" to the GCS continuation token,
|
|
|
|
// e.g, "{minio}CgRvYmoz"
|
2017-06-09 22:48:20 -04:00
|
|
|
//
|
2018-07-18 12:19:16 -04:00
|
|
|
// - Application supplied markers are transformed to a
|
|
|
|
// GCS continuation token.
|
2017-06-09 22:48:20 -04:00
|
|
|
|
|
|
|
// If application is using GCS continuation token we should
|
|
|
|
// strip the gcsTokenPrefix we added.
|
2018-07-18 12:19:16 -04:00
|
|
|
token := ""
|
|
|
|
if marker != "" {
|
|
|
|
if isGCSMarker(marker) {
|
|
|
|
token = strings.TrimPrefix(marker, gcsTokenPrefix)
|
|
|
|
} else {
|
|
|
|
token = toGCSPageToken(marker)
|
|
|
|
}
|
2017-06-09 22:48:20 -04:00
|
|
|
}
|
2018-07-18 12:19:16 -04:00
|
|
|
nextMarker := ""
|
2017-06-09 22:48:20 -04:00
|
|
|
|
2018-07-18 12:19:16 -04:00
|
|
|
var prefixes []string
|
|
|
|
var objects []minio.ObjectInfo
|
|
|
|
var nextPageToken string
|
|
|
|
var err error
|
2017-05-10 18:36:49 -04:00
|
|
|
|
2018-07-18 12:19:16 -04:00
|
|
|
pager := iterator.NewPager(it, maxKeys, token)
|
2017-05-01 13:59:54 -04:00
|
|
|
for {
|
2018-07-18 12:19:16 -04:00
|
|
|
gcsObjects := make([]*storage.ObjectAttrs, 0)
|
|
|
|
nextPageToken, err = pager.NextPage(&gcsObjects)
|
2017-06-23 20:57:25 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.ListObjectsInfo{}, gcsToObjectError(err, bucket, prefix)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2018-07-18 12:19:16 -04:00
|
|
|
for _, attrs := range gcsObjects {
|
2017-05-10 18:36:49 -04:00
|
|
|
|
2018-07-18 12:19:16 -04:00
|
|
|
// Due to minio.GatewayMinioSysTmp keys being skipped, the number of objects + prefixes
|
|
|
|
// returned may not total maxKeys. This behavior is compatible with the S3 spec which
|
|
|
|
// allows the response to include less keys than maxKeys.
|
|
|
|
if attrs.Prefix == minio.GatewayMinioSysTmp {
|
|
|
|
// We don't return our metadata prefix.
|
2017-06-23 20:57:25 -04:00
|
|
|
continue
|
|
|
|
}
|
2018-07-18 12:19:16 -04:00
|
|
|
if !strings.HasPrefix(prefix, minio.GatewayMinioSysTmp) {
|
|
|
|
// If client lists outside gcsMinioPath then we filter out gcsMinioPath/* entries.
|
|
|
|
// But if the client lists inside gcsMinioPath then we return the entries in gcsMinioPath/
|
|
|
|
// which will be helpful to observe the "directory structure" for debugging purposes.
|
|
|
|
if strings.HasPrefix(attrs.Prefix, minio.GatewayMinioSysTmp) ||
|
|
|
|
strings.HasPrefix(attrs.Name, minio.GatewayMinioSysTmp) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if attrs.Prefix != "" {
|
|
|
|
prefixes = append(prefixes, attrs.Prefix)
|
|
|
|
} else {
|
|
|
|
objects = append(objects, fromGCSAttrsToObjectInfo(attrs))
|
|
|
|
}
|
|
|
|
|
|
|
|
// The NextMarker property should only be set in the response if a delimiter is used
|
|
|
|
if delimiter != "" {
|
|
|
|
if attrs.Prefix > nextMarker {
|
|
|
|
nextMarker = attrs.Prefix
|
|
|
|
} else if attrs.Name > nextMarker {
|
|
|
|
nextMarker = attrs.Name
|
|
|
|
}
|
|
|
|
}
|
2017-06-23 20:57:25 -04:00
|
|
|
}
|
2018-07-18 12:19:16 -04:00
|
|
|
|
|
|
|
// Exit the loop if at least one item can be returned from
|
|
|
|
// the current page or there are no more pages available
|
|
|
|
if nextPageToken == "" || len(prefixes)+len(objects) > 0 {
|
|
|
|
break
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
2018-07-18 12:19:16 -04:00
|
|
|
}
|
2017-05-01 13:59:54 -04:00
|
|
|
|
2018-07-18 12:19:16 -04:00
|
|
|
if nextPageToken == "" {
|
|
|
|
nextMarker = ""
|
|
|
|
} else if nextMarker != "" {
|
|
|
|
nextMarker = gcsTokenPrefix + toGCSPageToken(nextMarker)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.ListObjectsInfo{
|
2018-07-18 12:19:16 -04:00
|
|
|
IsTruncated: nextPageToken != "",
|
|
|
|
NextMarker: nextMarker,
|
2017-05-01 13:59:54 -04:00
|
|
|
Prefixes: prefixes,
|
|
|
|
Objects: objects,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjectsV2 - lists all blobs in GCS bucket filtered by prefix
|
2018-03-14 20:31:14 -04:00
|
|
|
func (l *gcsGateway) ListObjectsV2(ctx context.Context, bucket, prefix, continuationToken, delimiter string, maxKeys int, fetchOwner bool, startAfter string) (minio.ListObjectsV2Info, error) {
|
2018-07-18 12:19:16 -04:00
|
|
|
if maxKeys == 0 {
|
|
|
|
return minio.ListObjectsV2Info{ContinuationToken: continuationToken}, nil
|
2018-07-01 00:22:45 -04:00
|
|
|
}
|
|
|
|
|
2018-11-28 03:19:51 -05:00
|
|
|
it := l.client.Bucket(bucket).Objects(ctx, &storage.Query{
|
2017-07-11 12:25:19 -04:00
|
|
|
Delimiter: delimiter,
|
|
|
|
Prefix: prefix,
|
|
|
|
Versions: false,
|
|
|
|
})
|
2017-05-01 13:59:54 -04:00
|
|
|
|
2018-07-18 12:19:16 -04:00
|
|
|
token := continuationToken
|
|
|
|
if token == "" && startAfter != "" {
|
|
|
|
token = toGCSPageToken(startAfter)
|
2017-06-29 14:19:55 -04:00
|
|
|
}
|
2017-05-01 13:59:54 -04:00
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
var prefixes []string
|
|
|
|
var objects []minio.ObjectInfo
|
2018-07-18 12:19:16 -04:00
|
|
|
var nextPageToken string
|
|
|
|
var err error
|
2017-06-29 14:19:55 -04:00
|
|
|
|
2018-07-18 12:19:16 -04:00
|
|
|
pager := iterator.NewPager(it, maxKeys, token)
|
|
|
|
for {
|
|
|
|
gcsObjects := make([]*storage.ObjectAttrs, 0)
|
|
|
|
nextPageToken, err = pager.NextPage(&gcsObjects)
|
2017-05-01 13:59:54 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.ListObjectsV2Info{}, gcsToObjectError(err, bucket, prefix)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2018-07-18 12:19:16 -04:00
|
|
|
for _, attrs := range gcsObjects {
|
|
|
|
|
|
|
|
// Due to minio.GatewayMinioSysTmp keys being skipped, the number of objects + prefixes
|
|
|
|
// returned may not total maxKeys. This behavior is compatible with the S3 spec which
|
|
|
|
// allows the response to include less keys than maxKeys.
|
|
|
|
if attrs.Prefix == minio.GatewayMinioSysTmp {
|
|
|
|
// We don't return our metadata prefix.
|
2017-06-29 14:19:55 -04:00
|
|
|
continue
|
|
|
|
}
|
2018-07-18 12:19:16 -04:00
|
|
|
if !strings.HasPrefix(prefix, minio.GatewayMinioSysTmp) {
|
|
|
|
// If client lists outside gcsMinioPath then we filter out gcsMinioPath/* entries.
|
|
|
|
// But if the client lists inside gcsMinioPath then we return the entries in gcsMinioPath/
|
|
|
|
// which will be helpful to observe the "directory structure" for debugging purposes.
|
|
|
|
if strings.HasPrefix(attrs.Prefix, minio.GatewayMinioSysTmp) ||
|
|
|
|
strings.HasPrefix(attrs.Name, minio.GatewayMinioSysTmp) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 14:19:55 -04:00
|
|
|
|
2018-07-18 12:19:16 -04:00
|
|
|
if attrs.Prefix != "" {
|
|
|
|
prefixes = append(prefixes, attrs.Prefix)
|
|
|
|
} else {
|
|
|
|
objects = append(objects, fromGCSAttrsToObjectInfo(attrs))
|
|
|
|
}
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2018-07-18 12:19:16 -04:00
|
|
|
// Exit the loop if at least one item can be returned from
|
|
|
|
// the current page or there are no more pages available
|
|
|
|
if nextPageToken == "" || len(prefixes)+len(objects) > 0 {
|
|
|
|
break
|
|
|
|
}
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.ListObjectsV2Info{
|
2018-07-18 12:19:16 -04:00
|
|
|
IsTruncated: nextPageToken != "",
|
2017-05-01 13:59:54 -04:00
|
|
|
ContinuationToken: continuationToken,
|
2018-07-18 12:19:16 -04:00
|
|
|
NextContinuationToken: nextPageToken,
|
2017-05-01 13:59:54 -04:00
|
|
|
Prefixes: prefixes,
|
|
|
|
Objects: objects,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-09-20 22:22:09 -04:00
|
|
|
// GetObjectNInfo - returns object info and locked object ReadCloser
|
2018-09-27 06:06:45 -04:00
|
|
|
func (l *gcsGateway) GetObjectNInfo(ctx context.Context, bucket, object string, rs *minio.HTTPRangeSpec, h http.Header, lockType minio.LockType, opts minio.ObjectOptions) (gr *minio.GetObjectReader, err error) {
|
2018-09-20 22:22:09 -04:00
|
|
|
var objInfo minio.ObjectInfo
|
2018-09-27 06:06:45 -04:00
|
|
|
objInfo, err = l.GetObjectInfo(ctx, bucket, object, opts)
|
2018-09-20 22:22:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var startOffset, length int64
|
|
|
|
startOffset, length, err = rs.GetOffsetLength(objInfo.Size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go func() {
|
2021-02-26 12:52:02 -05:00
|
|
|
err := l.getObject(ctx, bucket, object, startOffset, length, pw, objInfo.ETag, opts)
|
2018-09-20 22:22:09 -04:00
|
|
|
pw.CloseWithError(err)
|
|
|
|
}()
|
2018-09-21 14:42:06 -04:00
|
|
|
// Setup cleanup function to cause the above go-routine to
|
|
|
|
// exit in case of partial read
|
|
|
|
pipeCloser := func() { pr.Close() }
|
2020-04-21 01:01:59 -04:00
|
|
|
return minio.NewGetObjectReaderFromReader(pr, objInfo, opts, pipeCloser)
|
2018-09-20 22:22:09 -04:00
|
|
|
}
|
|
|
|
|
2017-05-01 13:59:54 -04:00
|
|
|
// GetObject - reads an object from GCS. Supports additional
|
|
|
|
// parameters like offset and length which are synonymous with
|
|
|
|
// HTTP Range requests.
|
|
|
|
//
|
|
|
|
// startOffset indicates the starting read location of the object.
|
|
|
|
// length indicates the total length of the object.
|
2021-02-26 12:52:02 -05:00
|
|
|
func (l *gcsGateway) getObject(ctx context.Context, bucket string, key string, startOffset int64, length int64, writer io.Writer, etag string, opts minio.ObjectOptions) error {
|
2017-05-01 13:59:54 -04:00
|
|
|
// if we want to mimic S3 behavior exactly, we need to verify if bucket exists first,
|
|
|
|
// otherwise gcs will just return object not exist in case of non-existing bucket
|
2018-11-28 03:19:51 -05:00
|
|
|
if _, err := l.client.Bucket(bucket).Attrs(ctx); err != nil {
|
2019-10-11 21:50:54 -04:00
|
|
|
logger.LogIf(ctx, err, logger.Application)
|
2018-04-05 18:04:40 -04:00
|
|
|
return gcsToObjectError(err, bucket)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2018-09-21 14:43:10 -04:00
|
|
|
// GCS storage decompresses a gzipped object by default and returns the data.
|
|
|
|
// Refer to https://cloud.google.com/storage/docs/transcoding#decompressive_transcoding
|
|
|
|
// Need to set `Accept-Encoding` header to `gzip` when issuing a GetObject call, to be able
|
|
|
|
// to download the object in compressed state.
|
|
|
|
// Calling ReadCompressed with true accomplishes that.
|
|
|
|
object := l.client.Bucket(bucket).Object(key).ReadCompressed(true)
|
|
|
|
|
2018-11-28 03:19:51 -05:00
|
|
|
r, err := object.NewRangeReader(ctx, startOffset, length)
|
2017-05-01 13:59:54 -04:00
|
|
|
if err != nil {
|
2019-10-11 21:50:54 -04:00
|
|
|
logger.LogIf(ctx, err, logger.Application)
|
2018-04-05 18:04:40 -04:00
|
|
|
return gcsToObjectError(err, bucket, key)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(writer, r); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return gcsToObjectError(err, bucket, key)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-31 18:40:33 -04:00
|
|
|
// fromGCSAttrsToObjectInfo converts GCS BucketAttrs to gateway ObjectInfo
|
2017-12-05 20:58:09 -05:00
|
|
|
func fromGCSAttrsToObjectInfo(attrs *storage.ObjectAttrs) minio.ObjectInfo {
|
2017-05-31 18:40:33 -04:00
|
|
|
// All google cloud storage objects have a CRC32c hash, whereas composite objects may not have a MD5 hash
|
|
|
|
// Refer https://cloud.google.com/storage/docs/hashes-etags. Use CRC32C for ETag
|
2018-04-18 12:10:48 -04:00
|
|
|
metadata := make(map[string]string)
|
2019-02-28 14:01:25 -05:00
|
|
|
var (
|
|
|
|
expiry time.Time
|
|
|
|
e error
|
|
|
|
)
|
2018-04-18 12:10:48 -04:00
|
|
|
for k, v := range attrs.Metadata {
|
2018-08-14 14:53:39 -04:00
|
|
|
k = http.CanonicalHeaderKey(k)
|
|
|
|
// Translate the GCS custom metadata prefix
|
|
|
|
if strings.HasPrefix(k, "X-Goog-Meta-") {
|
|
|
|
k = strings.Replace(k, "X-Goog-Meta-", "X-Amz-Meta-", 1)
|
|
|
|
}
|
2019-02-28 14:01:25 -05:00
|
|
|
if k == "Expires" {
|
|
|
|
if expiry, e = time.Parse(http.TimeFormat, v); e == nil {
|
|
|
|
expiry = expiry.UTC()
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2018-04-18 12:10:48 -04:00
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
if attrs.ContentType != "" {
|
2018-08-14 14:53:39 -04:00
|
|
|
metadata["Content-Type"] = attrs.ContentType
|
2018-04-18 12:10:48 -04:00
|
|
|
}
|
|
|
|
if attrs.ContentEncoding != "" {
|
2018-08-14 14:53:39 -04:00
|
|
|
metadata["Content-Encoding"] = attrs.ContentEncoding
|
2018-04-18 12:10:48 -04:00
|
|
|
}
|
|
|
|
if attrs.CacheControl != "" {
|
2018-08-14 14:53:39 -04:00
|
|
|
metadata["Cache-Control"] = attrs.CacheControl
|
2018-04-18 12:10:48 -04:00
|
|
|
}
|
|
|
|
if attrs.ContentDisposition != "" {
|
2018-08-14 14:53:39 -04:00
|
|
|
metadata["Content-Disposition"] = attrs.ContentDisposition
|
2018-04-18 12:10:48 -04:00
|
|
|
}
|
|
|
|
if attrs.ContentLanguage != "" {
|
2018-08-14 14:53:39 -04:00
|
|
|
metadata["Content-Language"] = attrs.ContentLanguage
|
2018-04-18 12:10:48 -04:00
|
|
|
}
|
2019-05-16 15:00:12 -04:00
|
|
|
|
|
|
|
etag := hex.EncodeToString(attrs.MD5)
|
|
|
|
if etag == "" {
|
|
|
|
etag = minio.ToS3ETag(fmt.Sprintf("%d", attrs.CRC32C))
|
|
|
|
}
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.ObjectInfo{
|
2017-05-01 13:59:54 -04:00
|
|
|
Name: attrs.Name,
|
|
|
|
Bucket: attrs.Bucket,
|
|
|
|
ModTime: attrs.Updated,
|
|
|
|
Size: attrs.Size,
|
2019-05-16 15:00:12 -04:00
|
|
|
ETag: etag,
|
2018-04-18 12:10:48 -04:00
|
|
|
UserDefined: metadata,
|
2017-05-01 13:59:54 -04:00
|
|
|
ContentType: attrs.ContentType,
|
|
|
|
ContentEncoding: attrs.ContentEncoding,
|
2019-02-28 14:01:25 -05:00
|
|
|
Expires: expiry,
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 12:10:48 -04:00
|
|
|
// applyMetadataToGCSAttrs applies metadata to a GCS ObjectAttrs instance
|
|
|
|
func applyMetadataToGCSAttrs(metadata map[string]string, attrs *storage.ObjectAttrs) {
|
|
|
|
attrs.Metadata = make(map[string]string)
|
|
|
|
for k, v := range metadata {
|
2018-08-14 14:53:39 -04:00
|
|
|
k = http.CanonicalHeaderKey(k)
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(k, "X-Amz-Meta-"):
|
|
|
|
// Translate the S3 user-defined metadata prefix
|
|
|
|
k = strings.Replace(k, "X-Amz-Meta-", "x-goog-meta-", 1)
|
|
|
|
attrs.Metadata[k] = v
|
|
|
|
case k == "Content-Type":
|
|
|
|
attrs.ContentType = v
|
|
|
|
case k == "Content-Encoding":
|
|
|
|
attrs.ContentEncoding = v
|
|
|
|
case k == "Cache-Control":
|
|
|
|
attrs.CacheControl = v
|
|
|
|
case k == "Content-Disposition":
|
|
|
|
attrs.ContentDisposition = v
|
|
|
|
case k == "Content-Language":
|
|
|
|
attrs.ContentLanguage = v
|
|
|
|
}
|
2018-04-18 12:10:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 13:59:54 -04:00
|
|
|
// GetObjectInfo - reads object info and replies back ObjectInfo
|
2018-09-10 12:42:43 -04:00
|
|
|
func (l *gcsGateway) GetObjectInfo(ctx context.Context, bucket string, object string, opts minio.ObjectOptions) (minio.ObjectInfo, error) {
|
2017-05-01 13:59:54 -04:00
|
|
|
// if we want to mimic S3 behavior exactly, we need to verify if bucket exists first,
|
|
|
|
// otherwise gcs will just return object not exist in case of non-existing bucket
|
2018-11-28 03:19:51 -05:00
|
|
|
if _, err := l.client.Bucket(bucket).Attrs(ctx); err != nil {
|
2019-10-11 21:50:54 -04:00
|
|
|
logger.LogIf(ctx, err, logger.Application)
|
2018-04-05 18:04:40 -04:00
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(err, bucket)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2018-11-28 03:19:51 -05:00
|
|
|
attrs, err := l.client.Bucket(bucket).Object(object).Attrs(ctx)
|
2017-05-01 13:59:54 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(err, bucket, object)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-07-11 12:25:19 -04:00
|
|
|
return fromGCSAttrsToObjectInfo(attrs), nil
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutObject - Create a new object with the incoming data,
|
2019-02-09 00:31:06 -05:00
|
|
|
func (l *gcsGateway) PutObject(ctx context.Context, bucket string, key string, r *minio.PutObjReader, opts minio.ObjectOptions) (minio.ObjectInfo, error) {
|
2018-11-14 20:36:41 -05:00
|
|
|
data := r.Reader
|
|
|
|
|
2020-05-18 12:59:45 -04:00
|
|
|
nctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
2017-05-01 13:59:54 -04:00
|
|
|
// if we want to mimic S3 behavior exactly, we need to verify if bucket exists first,
|
|
|
|
// otherwise gcs will just return object not exist in case of non-existing bucket
|
2020-05-18 12:59:45 -04:00
|
|
|
if _, err := l.client.Bucket(bucket).Attrs(nctx); err != nil {
|
2019-10-11 21:50:54 -04:00
|
|
|
logger.LogIf(ctx, err, logger.Application)
|
2018-04-05 18:04:40 -04:00
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(err, bucket)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
object := l.client.Bucket(bucket).Object(key)
|
|
|
|
|
2020-05-18 12:59:45 -04:00
|
|
|
w := object.NewWriter(nctx)
|
2019-02-13 07:59:36 -05:00
|
|
|
|
2018-04-19 19:15:14 -04:00
|
|
|
// Disable "chunked" uploading in GCS client if the size of the data to be uploaded is below
|
|
|
|
// the current chunk-size of the writer. This avoids an unnecessary memory allocation.
|
|
|
|
if data.Size() < int64(w.ChunkSize) {
|
|
|
|
w.ChunkSize = 0
|
|
|
|
}
|
2019-02-09 00:31:06 -05:00
|
|
|
applyMetadataToGCSAttrs(opts.UserDefined, &w.ObjectAttrs)
|
2017-05-31 18:40:33 -04:00
|
|
|
|
2017-09-19 15:40:27 -04:00
|
|
|
if _, err := io.Copy(w, data); err != nil {
|
2017-07-11 12:25:19 -04:00
|
|
|
// Close the object writer upon error.
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(err, bucket, key)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
2017-10-22 01:30:34 -04:00
|
|
|
|
2017-07-11 12:25:19 -04:00
|
|
|
// Close the object writer upon success.
|
2019-05-02 11:27:10 -04:00
|
|
|
if err := w.Close(); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(err, bucket, key)
|
2017-07-11 12:25:19 -04:00
|
|
|
}
|
|
|
|
|
2019-05-02 11:27:10 -04:00
|
|
|
return fromGCSAttrsToObjectInfo(w.Attrs()), nil
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CopyObject - Copies a blob from source container to destination container.
|
2018-03-14 20:31:14 -04:00
|
|
|
func (l *gcsGateway) CopyObject(ctx context.Context, srcBucket string, srcObject string, destBucket string, destObject string,
|
2018-09-10 12:42:43 -04:00
|
|
|
srcInfo minio.ObjectInfo, srcOpts, dstOpts minio.ObjectOptions) (minio.ObjectInfo, error) {
|
2020-07-17 16:01:22 -04:00
|
|
|
if srcOpts.CheckPrecondFn != nil && srcOpts.CheckPrecondFn(srcInfo) {
|
2019-03-06 15:38:41 -05:00
|
|
|
return minio.ObjectInfo{}, minio.PreConditionFailed{}
|
|
|
|
}
|
2017-05-01 13:59:54 -04:00
|
|
|
src := l.client.Bucket(srcBucket).Object(srcObject)
|
|
|
|
dst := l.client.Bucket(destBucket).Object(destObject)
|
|
|
|
|
2017-09-26 14:04:42 -04:00
|
|
|
copier := dst.CopierFrom(src)
|
2018-04-18 12:10:48 -04:00
|
|
|
applyMetadataToGCSAttrs(srcInfo.UserDefined, &copier.ObjectAttrs)
|
2017-09-26 14:04:42 -04:00
|
|
|
|
2018-11-28 03:19:51 -05:00
|
|
|
attrs, err := copier.Run(ctx)
|
2017-05-01 13:59:54 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(err, destBucket, destObject)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-05-31 18:40:33 -04:00
|
|
|
return fromGCSAttrsToObjectInfo(attrs), nil
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteObject - Deletes a blob in bucket
|
2020-06-12 23:04:01 -04:00
|
|
|
func (l *gcsGateway) DeleteObject(ctx context.Context, bucket string, object string, opts minio.ObjectOptions) (minio.ObjectInfo, error) {
|
2018-11-28 03:19:51 -05:00
|
|
|
err := l.client.Bucket(bucket).Object(object).Delete(ctx)
|
2017-05-01 13:59:54 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2020-06-12 23:04:01 -04:00
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(err, bucket, object)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
return minio.ObjectInfo{
|
|
|
|
Bucket: bucket,
|
|
|
|
Name: object,
|
|
|
|
}, nil
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
func (l *gcsGateway) DeleteObjects(ctx context.Context, bucket string, objects []minio.ObjectToDelete, opts minio.ObjectOptions) ([]minio.DeletedObject, []error) {
|
2019-05-13 15:25:49 -04:00
|
|
|
errs := make([]error, len(objects))
|
2020-06-12 23:04:01 -04:00
|
|
|
dobjects := make([]minio.DeletedObject, len(objects))
|
2019-05-13 15:25:49 -04:00
|
|
|
for idx, object := range objects {
|
2020-06-12 23:04:01 -04:00
|
|
|
_, errs[idx] = l.DeleteObject(ctx, bucket, object.ObjectName, opts)
|
|
|
|
if errs[idx] == nil {
|
|
|
|
dobjects[idx] = minio.DeletedObject{
|
|
|
|
ObjectName: object.ObjectName,
|
|
|
|
}
|
|
|
|
}
|
2019-05-13 15:25:49 -04:00
|
|
|
}
|
2020-06-12 23:04:01 -04:00
|
|
|
return dobjects, errs
|
2019-05-13 15:25:49 -04:00
|
|
|
}
|
|
|
|
|
2017-05-01 13:59:54 -04:00
|
|
|
// NewMultipartUpload - upload object in multiple parts
|
2019-02-09 00:31:06 -05:00
|
|
|
func (l *gcsGateway) NewMultipartUpload(ctx context.Context, bucket string, key string, o minio.ObjectOptions) (uploadID string, err error) {
|
2017-05-01 13:59:54 -04:00
|
|
|
// generate new uploadid
|
2017-12-05 20:58:09 -05:00
|
|
|
uploadID = minio.MustGetUUID()
|
2017-05-01 13:59:54 -04:00
|
|
|
|
|
|
|
// generate name for part zero
|
2017-06-17 19:00:41 -04:00
|
|
|
meta := gcsMultipartMetaName(uploadID)
|
2017-05-01 13:59:54 -04:00
|
|
|
|
2018-11-28 03:19:51 -05:00
|
|
|
w := l.client.Bucket(bucket).Object(meta).NewWriter(ctx)
|
2017-07-11 12:25:19 -04:00
|
|
|
defer w.Close()
|
|
|
|
|
2019-02-09 00:31:06 -05:00
|
|
|
applyMetadataToGCSAttrs(o.UserDefined, &w.ObjectAttrs)
|
2017-06-17 19:00:41 -04:00
|
|
|
|
2017-07-11 12:25:19 -04:00
|
|
|
if err = json.NewEncoder(w).Encode(gcsMultipartMetaV1{
|
|
|
|
gcsMinioMultipartMetaCurrentVersion,
|
|
|
|
bucket,
|
|
|
|
key,
|
|
|
|
}); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return "", gcsToObjectError(err, bucket, key)
|
2017-06-17 19:00:41 -04:00
|
|
|
}
|
2017-05-01 13:59:54 -04:00
|
|
|
return uploadID, nil
|
|
|
|
}
|
|
|
|
|
2018-09-04 16:11:33 -04:00
|
|
|
// ListMultipartUploads - lists the (first) multipart upload for an object
|
|
|
|
// matched _exactly_ by the prefix
|
2018-03-14 20:31:14 -04:00
|
|
|
func (l *gcsGateway) ListMultipartUploads(ctx context.Context, bucket string, prefix string, keyMarker string, uploadIDMarker string, delimiter string, maxUploads int) (minio.ListMultipartsInfo, error) {
|
2018-09-04 16:11:33 -04:00
|
|
|
// List objects under <bucket>/gcsMinioMultipartPathV1
|
|
|
|
it := l.client.Bucket(bucket).Objects(ctx, &storage.Query{
|
|
|
|
Prefix: gcsMinioMultipartPathV1,
|
|
|
|
})
|
|
|
|
|
|
|
|
var uploads []minio.MultipartInfo
|
|
|
|
|
|
|
|
for {
|
|
|
|
attrs, err := it.Next()
|
|
|
|
if err == iterator.Done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.ListMultipartsInfo{
|
|
|
|
KeyMarker: keyMarker,
|
|
|
|
UploadIDMarker: uploadIDMarker,
|
|
|
|
MaxUploads: maxUploads,
|
|
|
|
Prefix: prefix,
|
|
|
|
Delimiter: delimiter,
|
|
|
|
}, gcsToObjectError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip entries other than gcs.json
|
|
|
|
if !strings.HasSuffix(attrs.Name, gcsMinioMultipartMeta) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract multipart upload information from gcs.json
|
|
|
|
obj := l.client.Bucket(bucket).Object(attrs.Name)
|
|
|
|
objReader, rErr := obj.NewReader(ctx)
|
|
|
|
if rErr != nil {
|
|
|
|
logger.LogIf(ctx, rErr)
|
|
|
|
return minio.ListMultipartsInfo{}, rErr
|
|
|
|
}
|
|
|
|
defer objReader.Close()
|
|
|
|
|
|
|
|
var mpMeta gcsMultipartMetaV1
|
|
|
|
dec := json.NewDecoder(objReader)
|
|
|
|
decErr := dec.Decode(&mpMeta)
|
|
|
|
if decErr != nil {
|
|
|
|
logger.LogIf(ctx, decErr)
|
|
|
|
return minio.ListMultipartsInfo{}, decErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if prefix == mpMeta.Object {
|
|
|
|
// Extract uploadId
|
|
|
|
// E.g minio.sys.tmp/multipart/v1/d063ad89-fdc4-4ea3-a99e-22dba98151f5/gcs.json
|
2019-08-06 15:08:58 -04:00
|
|
|
components := strings.SplitN(attrs.Name, minio.SlashSeparator, 5)
|
2018-09-04 16:11:33 -04:00
|
|
|
if len(components) != 5 {
|
|
|
|
compErr := errors.New("Invalid multipart upload format")
|
|
|
|
logger.LogIf(ctx, compErr)
|
|
|
|
return minio.ListMultipartsInfo{}, compErr
|
|
|
|
}
|
|
|
|
upload := minio.MultipartInfo{
|
|
|
|
Object: mpMeta.Object,
|
|
|
|
UploadID: components[3],
|
|
|
|
Initiated: attrs.Created,
|
|
|
|
}
|
2019-01-08 14:03:28 -05:00
|
|
|
uploads = append(uploads, upload)
|
2018-09-04 16:11:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.ListMultipartsInfo{
|
2018-09-04 16:11:33 -04:00
|
|
|
KeyMarker: keyMarker,
|
|
|
|
UploadIDMarker: uploadIDMarker,
|
|
|
|
MaxUploads: maxUploads,
|
|
|
|
Prefix: prefix,
|
|
|
|
Delimiter: delimiter,
|
|
|
|
Uploads: uploads,
|
|
|
|
NextKeyMarker: "",
|
|
|
|
NextUploadIDMarker: "",
|
|
|
|
IsTruncated: false,
|
2017-06-17 19:00:41 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-07-19 22:33:10 -04:00
|
|
|
// Checks if minio.sys.tmp/multipart/v1/<upload-id>/gcs.json exists, returns
|
2017-07-11 12:25:19 -04:00
|
|
|
// an object layer compatible error upon any error.
|
2018-03-14 20:31:14 -04:00
|
|
|
func (l *gcsGateway) checkUploadIDExists(ctx context.Context, bucket string, key string, uploadID string) error {
|
2018-11-28 03:19:51 -05:00
|
|
|
_, err := l.client.Bucket(bucket).Object(gcsMultipartMetaName(uploadID)).Attrs(ctx)
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return gcsToObjectError(err, bucket, key, uploadID)
|
2017-07-11 12:25:19 -04:00
|
|
|
}
|
|
|
|
|
2017-05-01 13:59:54 -04:00
|
|
|
// PutObjectPart puts a part of object in bucket
|
2018-11-14 20:36:41 -05:00
|
|
|
func (l *gcsGateway) PutObjectPart(ctx context.Context, bucket string, key string, uploadID string, partNumber int, r *minio.PutObjReader, opts minio.ObjectOptions) (minio.PartInfo, error) {
|
|
|
|
data := r.Reader
|
2018-03-14 20:31:14 -04:00
|
|
|
if err := l.checkUploadIDExists(ctx, bucket, key, uploadID); err != nil {
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.PartInfo{}, err
|
2017-06-17 19:00:41 -04:00
|
|
|
}
|
2017-10-22 01:30:34 -04:00
|
|
|
etag := data.MD5HexString()
|
2017-09-19 15:40:27 -04:00
|
|
|
if etag == "" {
|
2017-07-11 12:25:19 -04:00
|
|
|
// Generate random ETag.
|
2017-12-05 20:58:09 -05:00
|
|
|
etag = minio.GenETag()
|
2017-07-11 12:25:19 -04:00
|
|
|
}
|
2017-07-14 02:20:16 -04:00
|
|
|
object := l.client.Bucket(bucket).Object(gcsMultipartDataName(uploadID, partNumber, etag))
|
2018-11-28 03:19:51 -05:00
|
|
|
w := object.NewWriter(ctx)
|
2017-06-17 19:00:41 -04:00
|
|
|
// Disable "chunked" uploading in GCS client. If enabled, it can cause a corner case
|
|
|
|
// where it tries to upload 0 bytes in the last chunk and get error from server.
|
|
|
|
w.ChunkSize = 0
|
2017-09-19 15:40:27 -04:00
|
|
|
if _, err := io.Copy(w, data); err != nil {
|
2017-07-11 12:25:19 -04:00
|
|
|
// Make sure to close object writer upon error.
|
|
|
|
w.Close()
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.PartInfo{}, gcsToObjectError(err, bucket, key)
|
2017-06-17 19:00:41 -04:00
|
|
|
}
|
2017-07-11 12:25:19 -04:00
|
|
|
// Make sure to close the object writer upon success.
|
2019-05-02 11:27:10 -04:00
|
|
|
if err := w.Close(); err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.PartInfo{}, gcsToObjectError(err, bucket, key)
|
|
|
|
}
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.PartInfo{
|
2017-07-14 02:20:16 -04:00
|
|
|
PartNumber: partNumber,
|
2017-06-17 19:00:41 -04:00
|
|
|
ETag: etag,
|
2017-12-05 20:58:09 -05:00
|
|
|
LastModified: minio.UTCNow(),
|
2017-09-19 15:40:27 -04:00
|
|
|
Size: data.Size(),
|
2017-06-17 19:00:41 -04:00
|
|
|
}, nil
|
|
|
|
}
|
2017-05-01 13:59:54 -04:00
|
|
|
|
2018-09-04 16:11:33 -04:00
|
|
|
// gcsGetPartInfo returns PartInfo of a given object part
|
|
|
|
func gcsGetPartInfo(ctx context.Context, attrs *storage.ObjectAttrs) (minio.PartInfo, error) {
|
2019-08-06 15:08:58 -04:00
|
|
|
components := strings.SplitN(attrs.Name, minio.SlashSeparator, 5)
|
2018-09-04 16:11:33 -04:00
|
|
|
if len(components) != 5 {
|
|
|
|
logger.LogIf(ctx, errors.New("Invalid multipart upload format"))
|
|
|
|
return minio.PartInfo{}, errors.New("Invalid multipart upload format")
|
|
|
|
}
|
|
|
|
|
|
|
|
partComps := strings.SplitN(components[4], ".", 2)
|
|
|
|
if len(partComps) != 2 {
|
|
|
|
logger.LogIf(ctx, errors.New("Invalid multipart part format"))
|
|
|
|
return minio.PartInfo{}, errors.New("Invalid multipart part format")
|
|
|
|
}
|
|
|
|
|
|
|
|
partNum, pErr := strconv.Atoi(partComps[0])
|
|
|
|
if pErr != nil {
|
|
|
|
logger.LogIf(ctx, pErr)
|
|
|
|
return minio.PartInfo{}, errors.New("Invalid part number")
|
|
|
|
}
|
|
|
|
|
|
|
|
return minio.PartInfo{
|
|
|
|
PartNumber: partNum,
|
|
|
|
LastModified: attrs.Updated,
|
|
|
|
Size: attrs.Size,
|
|
|
|
ETag: partComps[1],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:36:20 -04:00
|
|
|
// GetMultipartInfo returns multipart info of the uploadId of the object
|
|
|
|
func (l *gcsGateway) GetMultipartInfo(ctx context.Context, bucket, object, uploadID string, opts minio.ObjectOptions) (result minio.MultipartInfo, err error) {
|
|
|
|
result.Bucket = bucket
|
|
|
|
result.Object = object
|
|
|
|
result.UploadID = uploadID
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2018-09-04 16:11:33 -04:00
|
|
|
// ListObjectParts returns all object parts for specified object in specified bucket
|
2019-01-05 17:16:43 -05:00
|
|
|
func (l *gcsGateway) ListObjectParts(ctx context.Context, bucket string, key string, uploadID string, partNumberMarker int, maxParts int, opts minio.ObjectOptions) (minio.ListPartsInfo, error) {
|
2018-09-04 16:11:33 -04:00
|
|
|
it := l.client.Bucket(bucket).Objects(ctx, &storage.Query{
|
|
|
|
Prefix: path.Join(gcsMinioMultipartPathV1, uploadID),
|
|
|
|
})
|
|
|
|
|
|
|
|
var (
|
|
|
|
count int
|
|
|
|
partInfos []minio.PartInfo
|
|
|
|
)
|
|
|
|
|
|
|
|
isTruncated := true
|
|
|
|
for count < maxParts {
|
|
|
|
attrs, err := it.Next()
|
|
|
|
if err == iterator.Done {
|
|
|
|
isTruncated = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.ListPartsInfo{}, gcsToObjectError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(attrs.Name, gcsMinioMultipartMeta) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
partInfo, pErr := gcsGetPartInfo(ctx, attrs)
|
|
|
|
if pErr != nil {
|
|
|
|
logger.LogIf(ctx, pErr)
|
|
|
|
return minio.ListPartsInfo{}, pErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if partInfo.PartNumber <= partNumberMarker {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
partInfos = append(partInfos, partInfo)
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
|
|
|
nextPartNumberMarker := 0
|
|
|
|
if isTruncated {
|
|
|
|
nextPartNumberMarker = partInfos[maxParts-1].PartNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
return minio.ListPartsInfo{
|
|
|
|
Bucket: bucket,
|
|
|
|
Object: key,
|
|
|
|
UploadID: uploadID,
|
|
|
|
PartNumberMarker: partNumberMarker,
|
|
|
|
NextPartNumberMarker: nextPartNumberMarker,
|
|
|
|
MaxParts: maxParts,
|
|
|
|
Parts: partInfos,
|
|
|
|
IsTruncated: isTruncated,
|
|
|
|
}, nil
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-06-17 19:00:41 -04:00
|
|
|
// Called by AbortMultipartUpload and CompleteMultipartUpload for cleaning up.
|
2018-04-05 18:04:40 -04:00
|
|
|
func (l *gcsGateway) cleanupMultipartUpload(ctx context.Context, bucket, key, uploadID string) error {
|
2017-06-21 13:27:44 -04:00
|
|
|
prefix := fmt.Sprintf("%s/%s/", gcsMinioMultipartPathV1, uploadID)
|
2017-06-17 19:00:41 -04:00
|
|
|
|
|
|
|
// iterate through all parts and delete them
|
2018-11-28 03:19:51 -05:00
|
|
|
it := l.client.Bucket(bucket).Objects(ctx, &storage.Query{Prefix: prefix, Versions: false})
|
2017-05-11 17:21:56 -04:00
|
|
|
|
2017-05-01 13:59:54 -04:00
|
|
|
for {
|
|
|
|
attrs, err := it.Next()
|
|
|
|
if err == iterator.Done {
|
|
|
|
break
|
2017-06-17 19:00:41 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return gcsToObjectError(err, bucket, key)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-06-17 19:00:41 -04:00
|
|
|
object := l.client.Bucket(bucket).Object(attrs.Name)
|
|
|
|
// Ignore the error as parallel AbortMultipartUpload might have deleted it.
|
2018-11-28 03:19:51 -05:00
|
|
|
object.Delete(ctx)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-17 19:00:41 -04:00
|
|
|
// AbortMultipartUpload aborts a ongoing multipart upload
|
2020-09-14 18:57:13 -04:00
|
|
|
func (l *gcsGateway) AbortMultipartUpload(ctx context.Context, bucket string, key string, uploadID string, opts minio.ObjectOptions) error {
|
2018-03-14 20:31:14 -04:00
|
|
|
if err := l.checkUploadIDExists(ctx, bucket, key, uploadID); err != nil {
|
2017-07-11 12:25:19 -04:00
|
|
|
return err
|
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
return l.cleanupMultipartUpload(ctx, bucket, key, uploadID)
|
2017-06-17 19:00:41 -04:00
|
|
|
}
|
2017-05-01 13:59:54 -04:00
|
|
|
|
2017-06-17 19:00:41 -04:00
|
|
|
// CompleteMultipartUpload completes ongoing multipart upload and finalizes object
|
2017-07-11 12:25:19 -04:00
|
|
|
// Note that there is a limit (currently 32) to the number of components that can
|
2018-07-06 20:07:11 -04:00
|
|
|
// be composed in a single operation. There is a per-project rate limit (currently 200)
|
|
|
|
// to the number of source objects you can compose per second.
|
2018-11-14 20:36:41 -05:00
|
|
|
func (l *gcsGateway) CompleteMultipartUpload(ctx context.Context, bucket string, key string, uploadID string, uploadedParts []minio.CompletePart, opts minio.ObjectOptions) (minio.ObjectInfo, error) {
|
2017-06-17 19:00:41 -04:00
|
|
|
meta := gcsMultipartMetaName(uploadID)
|
|
|
|
object := l.client.Bucket(bucket).Object(meta)
|
2017-05-01 13:59:54 -04:00
|
|
|
|
2018-11-28 03:19:51 -05:00
|
|
|
partZeroAttrs, err := object.Attrs(ctx)
|
2017-06-17 19:00:41 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(err, bucket, key, uploadID)
|
2017-06-17 19:00:41 -04:00
|
|
|
}
|
2017-07-11 12:25:19 -04:00
|
|
|
|
2018-11-28 03:19:51 -05:00
|
|
|
r, err := object.NewReader(ctx)
|
2017-05-01 13:59:54 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(err, bucket, key)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
2017-07-11 12:25:19 -04:00
|
|
|
defer r.Close()
|
2017-05-01 13:59:54 -04:00
|
|
|
|
2017-06-17 19:00:41 -04:00
|
|
|
// Check version compatibility of the meta file before compose()
|
|
|
|
multipartMeta := gcsMultipartMetaV1{}
|
2017-07-11 12:25:19 -04:00
|
|
|
if err = json.NewDecoder(r).Decode(&multipartMeta); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(err, bucket, key)
|
2017-06-17 19:00:41 -04:00
|
|
|
}
|
2017-07-11 12:25:19 -04:00
|
|
|
|
|
|
|
if multipartMeta.Version != gcsMinioMultipartMetaCurrentVersion {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errGCSFormat)
|
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(errGCSFormat, bucket, key)
|
2017-06-17 19:00:41 -04:00
|
|
|
}
|
|
|
|
|
2017-07-11 12:25:19 -04:00
|
|
|
// Validate if the gcs.json stores valid entries for the bucket and key.
|
|
|
|
if multipartMeta.Bucket != bucket || multipartMeta.Object != key {
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(minio.InvalidUploadID{
|
2017-07-11 12:25:19 -04:00
|
|
|
UploadID: uploadID,
|
|
|
|
}, bucket, key)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-07-11 12:25:19 -04:00
|
|
|
var parts []*storage.ObjectHandle
|
2017-11-06 13:09:21 -05:00
|
|
|
partSizes := make([]int64, len(uploadedParts))
|
|
|
|
for i, uploadedPart := range uploadedParts {
|
2017-07-14 02:20:16 -04:00
|
|
|
parts = append(parts, l.client.Bucket(bucket).Object(gcsMultipartDataName(uploadID,
|
|
|
|
uploadedPart.PartNumber, uploadedPart.ETag)))
|
2018-11-28 03:19:51 -05:00
|
|
|
partAttr, pErr := l.client.Bucket(bucket).Object(gcsMultipartDataName(uploadID, uploadedPart.PartNumber, uploadedPart.ETag)).Attrs(ctx)
|
2017-11-06 13:09:21 -05:00
|
|
|
if pErr != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, pErr)
|
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(pErr, bucket, key, uploadID)
|
2017-11-06 13:09:21 -05:00
|
|
|
}
|
|
|
|
partSizes[i] = partAttr.Size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error out if parts except last part sizing < 5MiB.
|
|
|
|
for i, size := range partSizes[:len(partSizes)-1] {
|
2017-12-05 20:58:09 -05:00
|
|
|
if size < 5*humanize.MiByte {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, minio.PartTooSmall{
|
2017-11-06 13:09:21 -05:00
|
|
|
PartNumber: uploadedParts[i].PartNumber,
|
|
|
|
PartSize: size,
|
|
|
|
PartETag: uploadedParts[i].ETag,
|
|
|
|
})
|
2018-04-05 18:04:40 -04:00
|
|
|
return minio.ObjectInfo{}, minio.PartTooSmall{
|
|
|
|
PartNumber: uploadedParts[i].PartNumber,
|
|
|
|
PartSize: size,
|
|
|
|
PartETag: uploadedParts[i].ETag,
|
|
|
|
}
|
2017-11-06 13:09:21 -05:00
|
|
|
}
|
2017-07-14 02:20:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns name of the composed object.
|
|
|
|
gcsMultipartComposeName := func(uploadID string, composeNumber int) string {
|
2017-12-05 20:58:09 -05:00
|
|
|
return fmt.Sprintf("%s/tmp/%s/composed-object-%05d", minio.GatewayMinioSysTmp, uploadID, composeNumber)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-07-19 22:33:10 -04:00
|
|
|
composeCount := int(math.Ceil(float64(len(parts)) / float64(gcsMaxComponents)))
|
2017-06-28 01:27:05 -04:00
|
|
|
if composeCount > 1 {
|
|
|
|
// Create composes of every 32 parts.
|
|
|
|
composeParts := make([]*storage.ObjectHandle, composeCount)
|
|
|
|
for i := 0; i < composeCount; i++ {
|
|
|
|
// Create 'composed-object-N' using next 32 parts.
|
2017-07-14 02:20:16 -04:00
|
|
|
composeParts[i] = l.client.Bucket(bucket).Object(gcsMultipartComposeName(uploadID, i))
|
2017-07-19 22:33:10 -04:00
|
|
|
start := i * gcsMaxComponents
|
|
|
|
end := start + gcsMaxComponents
|
2017-06-28 01:27:05 -04:00
|
|
|
if end > len(parts) {
|
|
|
|
end = len(parts)
|
|
|
|
}
|
|
|
|
|
|
|
|
composer := composeParts[i].ComposerFrom(parts[start:end]...)
|
|
|
|
composer.ContentType = partZeroAttrs.ContentType
|
|
|
|
composer.Metadata = partZeroAttrs.Metadata
|
|
|
|
|
2018-11-28 03:19:51 -05:00
|
|
|
if _, err = composer.Run(ctx); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(err, bucket, key)
|
2017-06-28 01:27:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// As composes are successfully created, final object needs to be created using composes.
|
|
|
|
parts = composeParts
|
|
|
|
}
|
|
|
|
|
2017-07-11 12:25:19 -04:00
|
|
|
composer := l.client.Bucket(bucket).Object(key).ComposerFrom(parts...)
|
2017-05-01 13:59:54 -04:00
|
|
|
composer.ContentType = partZeroAttrs.ContentType
|
2018-04-18 12:10:48 -04:00
|
|
|
composer.ContentEncoding = partZeroAttrs.ContentEncoding
|
|
|
|
composer.CacheControl = partZeroAttrs.CacheControl
|
|
|
|
composer.ContentDisposition = partZeroAttrs.ContentDisposition
|
|
|
|
composer.ContentLanguage = partZeroAttrs.ContentLanguage
|
2017-05-01 13:59:54 -04:00
|
|
|
composer.Metadata = partZeroAttrs.Metadata
|
2018-11-28 03:19:51 -05:00
|
|
|
attrs, err := composer.Run(ctx)
|
2017-06-17 19:00:41 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(err, bucket, key)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
if err = l.cleanupMultipartUpload(ctx, bucket, key, uploadID); err != nil {
|
|
|
|
return minio.ObjectInfo{}, gcsToObjectError(err, bucket, key)
|
2017-06-17 19:00:41 -04:00
|
|
|
}
|
|
|
|
return fromGCSAttrsToObjectInfo(attrs), nil
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 18:19:30 -05:00
|
|
|
// SetBucketPolicy - Set policy on bucket
|
2018-04-24 18:53:30 -04:00
|
|
|
func (l *gcsGateway) SetBucketPolicy(ctx context.Context, bucket string, bucketPolicy *policy.Policy) error {
|
|
|
|
policyInfo, err := minio.PolicyToBucketAccessPolicy(bucketPolicy)
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return gcsToObjectError(err, bucket)
|
|
|
|
}
|
2017-05-08 20:55:39 -04:00
|
|
|
|
2018-04-24 18:53:30 -04:00
|
|
|
var policies []minio.BucketAccessPolicy
|
|
|
|
for prefix, policy := range miniogopolicy.GetPolicies(policyInfo.Statements, bucket, "") {
|
2017-12-05 20:58:09 -05:00
|
|
|
policies = append(policies, minio.BucketAccessPolicy{
|
2017-05-08 20:55:39 -04:00
|
|
|
Prefix: prefix,
|
|
|
|
Policy: policy,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := bucket + "/*" // For all objects inside the bucket.
|
|
|
|
|
|
|
|
if len(policies) != 1 {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, minio.NotImplemented{})
|
|
|
|
return minio.NotImplemented{}
|
2017-06-23 20:57:25 -04:00
|
|
|
}
|
|
|
|
if policies[0].Prefix != prefix {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, minio.NotImplemented{})
|
|
|
|
return minio.NotImplemented{}
|
2017-05-08 20:55:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
acl := l.client.Bucket(bucket).ACL()
|
2018-04-24 18:53:30 -04:00
|
|
|
if policies[0].Policy == miniogopolicy.BucketPolicyNone {
|
2018-11-28 03:19:51 -05:00
|
|
|
if err := acl.Delete(ctx, storage.AllUsers); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return gcsToObjectError(err, bucket)
|
2017-05-08 20:55:39 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-12 19:49:14 -04:00
|
|
|
var role storage.ACLRole
|
2017-05-10 13:02:45 -04:00
|
|
|
switch policies[0].Policy {
|
2018-04-24 18:53:30 -04:00
|
|
|
case miniogopolicy.BucketPolicyReadOnly:
|
2017-05-08 20:55:39 -04:00
|
|
|
role = storage.RoleReader
|
2018-04-24 18:53:30 -04:00
|
|
|
case miniogopolicy.BucketPolicyWriteOnly:
|
2017-05-08 20:55:39 -04:00
|
|
|
role = storage.RoleWriter
|
2017-05-10 13:02:45 -04:00
|
|
|
default:
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, minio.NotImplemented{})
|
|
|
|
return minio.NotImplemented{}
|
2017-05-08 20:55:39 -04:00
|
|
|
}
|
|
|
|
|
2018-11-28 03:19:51 -05:00
|
|
|
if err := acl.Set(ctx, storage.AllUsers, role); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return gcsToObjectError(err, bucket)
|
2017-05-08 20:55:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 18:19:30 -05:00
|
|
|
// GetBucketPolicy - Get policy on bucket
|
2018-04-24 18:53:30 -04:00
|
|
|
func (l *gcsGateway) GetBucketPolicy(ctx context.Context, bucket string) (*policy.Policy, error) {
|
2018-11-28 03:19:51 -05:00
|
|
|
rules, err := l.client.Bucket(bucket).ACL().List(ctx)
|
2017-05-08 20:55:39 -04:00
|
|
|
if err != nil {
|
2018-04-24 18:53:30 -04:00
|
|
|
return nil, gcsToObjectError(err, bucket)
|
2017-05-08 20:55:39 -04:00
|
|
|
}
|
2018-04-24 18:53:30 -04:00
|
|
|
|
|
|
|
var readOnly, writeOnly bool
|
2017-05-08 20:55:39 -04:00
|
|
|
for _, r := range rules {
|
2017-05-12 19:49:14 -04:00
|
|
|
if r.Entity != storage.AllUsers || r.Role == storage.RoleOwner {
|
2017-05-08 20:55:39 -04:00
|
|
|
continue
|
|
|
|
}
|
2018-04-24 18:53:30 -04:00
|
|
|
|
2017-05-08 20:55:39 -04:00
|
|
|
switch r.Role {
|
|
|
|
case storage.RoleReader:
|
2018-04-24 18:53:30 -04:00
|
|
|
readOnly = true
|
2017-05-08 20:55:39 -04:00
|
|
|
case storage.RoleWriter:
|
2018-04-24 18:53:30 -04:00
|
|
|
writeOnly = true
|
2017-05-08 20:55:39 -04:00
|
|
|
}
|
|
|
|
}
|
2018-04-24 18:53:30 -04:00
|
|
|
|
|
|
|
actionSet := policy.NewActionSet()
|
|
|
|
if readOnly {
|
|
|
|
actionSet.Add(policy.GetBucketLocationAction)
|
|
|
|
actionSet.Add(policy.ListBucketAction)
|
|
|
|
actionSet.Add(policy.GetObjectAction)
|
|
|
|
}
|
|
|
|
if writeOnly {
|
|
|
|
actionSet.Add(policy.GetBucketLocationAction)
|
|
|
|
actionSet.Add(policy.ListBucketMultipartUploadsAction)
|
|
|
|
actionSet.Add(policy.AbortMultipartUploadAction)
|
|
|
|
actionSet.Add(policy.DeleteObjectAction)
|
|
|
|
actionSet.Add(policy.ListMultipartUploadPartsAction)
|
|
|
|
actionSet.Add(policy.PutObjectAction)
|
2017-10-26 21:01:00 -04:00
|
|
|
}
|
2018-04-24 18:53:30 -04:00
|
|
|
|
|
|
|
// Return NoSuchBucketPolicy error, when policy is not set
|
|
|
|
if len(actionSet) == 0 {
|
|
|
|
return nil, gcsToObjectError(minio.BucketPolicyNotFound{}, bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &policy.Policy{
|
|
|
|
Version: policy.DefaultVersion,
|
|
|
|
Statements: []policy.Statement{
|
|
|
|
policy.NewStatement(
|
2022-01-10 17:26:26 -05:00
|
|
|
"",
|
2018-04-24 18:53:30 -04:00
|
|
|
policy.Allow,
|
|
|
|
policy.NewPrincipal("*"),
|
|
|
|
actionSet,
|
|
|
|
policy.NewResourceSet(
|
|
|
|
policy.NewResource(bucket, ""),
|
|
|
|
policy.NewResource(bucket, "*"),
|
|
|
|
),
|
|
|
|
condition.NewFunctions(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}, nil
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 18:19:30 -05:00
|
|
|
// DeleteBucketPolicy - Delete all policies on bucket
|
2018-03-14 20:31:14 -04:00
|
|
|
func (l *gcsGateway) DeleteBucketPolicy(ctx context.Context, bucket string) error {
|
2017-05-12 19:49:14 -04:00
|
|
|
// This only removes the storage.AllUsers policies
|
2018-11-28 03:19:51 -05:00
|
|
|
if err := l.client.Bucket(bucket).ACL().Delete(ctx, storage.AllUsers); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
return gcsToObjectError(err, bucket)
|
2017-05-08 20:55:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
2018-09-27 23:36:17 -04:00
|
|
|
|
|
|
|
// IsCompressionSupported returns whether compression is applicable for this layer.
|
|
|
|
func (l *gcsGateway) IsCompressionSupported() bool {
|
|
|
|
return false
|
|
|
|
}
|