2017-05-01 13:59:54 -04:00
/ *
2018-03-14 10:08:29 -04:00
* Minio Cloud Storage , ( C ) 2017 , 2018 Minio , Inc .
2017-05-01 13:59:54 -04:00
*
* 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-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"
2017-06-17 19:00:41 -04:00
"encoding/json"
2017-05-01 13:59:54 -04:00
"fmt"
"io"
2017-10-20 16:59:12 -04:00
"io/ioutil"
2017-12-15 15:33:42 -05:00
2017-06-28 01:27:05 -04:00
"math"
2017-10-20 16:59:12 -04:00
"os"
2017-05-24 21:39:45 -04:00
"regexp"
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"
2017-05-01 13:59:54 -04:00
"github.com/minio/minio-go/pkg/policy"
2017-12-05 20:58:09 -05:00
"github.com/minio/minio/pkg/auth"
"github.com/minio/minio/pkg/errors"
2017-10-22 01:30:34 -04:00
"github.com/minio/minio/pkg/hash"
2017-12-05 20:58:09 -05:00
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-11-25 14:58:29 -05:00
2017-12-05 20:58:09 -05:00
minio "github.com/minio/minio/cmd"
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-10-27 18:07:46 -04:00
gcsBackend = "gcs"
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 :
GCS project - id should be provided if GOOGLE_APPLICATION_CREDENTIALS environmental variable is not set .
ENVIRONMENT VARIABLES :
ACCESS :
MINIO_ACCESS_KEY : Username or access key of GCS .
MINIO_SECRET_KEY : Password or secret key of GCS .
BROWSER :
MINIO_BROWSER : To disable web browser access , set this value to "off" .
2018-03-28 17:14:06 -04:00
CACHE :
2018-03-29 16:29:21 -04:00
MINIO_CACHE_DRIVES : List of cache drives delimited by ";" .
MINIO_CACHE_EXCLUDE : List of cache exclusion patterns delimited by ";" .
MINIO_CACHE_EXPIRY : Cache expiry duration in days .
2018-03-28 17:14:06 -04:00
2017-12-15 15:33:42 -05:00
UPDATE :
MINIO_UPDATE : To turn off in - place upgrades , set this value to "off" .
2018-03-26 12:11:39 -04:00
DOMAIN :
2018-03-29 16:29:21 -04:00
MINIO_DOMAIN : To enable virtual - host - style requests , set this value to Minio host domain name .
2018-03-26 12:11:39 -04:00
2017-10-27 18:07:46 -04:00
GCS credentials file :
GOOGLE_APPLICATION_CREDENTIALS : Path to credentials . json
EXAMPLES :
1. Start minio gateway server for GCS backend .
$ export GOOGLE_APPLICATION_CREDENTIALS = / path / to / credentials . json
( Instructions to generate credentials : https : //developers.google.com/identity/protocols/application-default-credentials)
$ export MINIO_ACCESS_KEY = accesskey
$ export MINIO_SECRET_KEY = secretkey
$ { { . HelpName } } mygcsprojectid
2018-03-28 17:14:06 -04:00
2. Start minio gateway server for GCS backend with edge caching enabled .
$ export GOOGLE_APPLICATION_CREDENTIALS = / path / to / credentials . json
$ export MINIO_ACCESS_KEY = accesskey
$ export MINIO_SECRET_KEY = secretkey
$ export MINIO_CACHE_DRIVES = "/home/drive1;/home/drive2;/home/drive3;/home/drive4"
$ export MINIO_CACHE_EXCLUDE = "bucket1/*;*.png"
$ export MINIO_CACHE_EXPIRY = 40
$ { { . HelpName } } mygcsprojectid
2017-10-27 18:07:46 -04:00
`
2017-12-05 20:58:09 -05:00
minio . RegisterGatewayCommand ( cli . Command {
2017-10-27 18:07:46 -04:00
Name : gcsBackend ,
Usage : "Google Cloud Storage." ,
Action : gcsGatewayMain ,
CustomHelpTemplate : gcsGatewayTemplate ,
HideHelpCommand : true ,
} )
}
// Handler for 'minio gateway gcs' command line.
func gcsGatewayMain ( ctx * cli . Context ) {
projectID := ctx . Args ( ) . First ( )
if projectID == "" && os . Getenv ( "GOOGLE_APPLICATION_CREDENTIALS" ) == "" {
2017-12-05 20:58:09 -05:00
minio . ErrorIf ( errGCSProjectIDNotFound , "project-id should be provided as argument or GOOGLE_APPLICATION_CREDENTIALS should be set with path to credentials.json" )
2017-10-27 18:07:46 -04:00
cli . ShowCommandHelpAndExit ( ctx , "gcs" , 1 )
}
if projectID != "" && ! isValidGCSProjectIDFormat ( projectID ) {
2017-12-05 20:58:09 -05:00
minio . ErrorIf ( errGCSInvalidProjectID , "Unable to start GCS gateway with %s" , ctx . Args ( ) . First ( ) )
2017-10-27 18:07:46 -04:00
cli . ShowCommandHelpAndExit ( ctx , "gcs" , 1 )
}
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 {
2017-10-27 18:07:46 -04:00
return gcsBackend
}
2018-02-09 18:19:30 -05:00
// NewGatewayLayer returns gcs ObjectLayer.
func ( g * GCS ) NewGatewayLayer ( creds auth . Credentials ) ( minio . ObjectLayer , error ) {
2017-12-05 20:58:09 -05:00
ctx := context . Background ( )
var err error
if g . projectID == "" {
// If project ID is not provided on command line, we figure it out
// from the credentials.json file.
g . projectID , err = gcsParseProjectID ( os . Getenv ( "GOOGLE_APPLICATION_CREDENTIALS" ) )
if err != nil {
return nil , err
}
}
// 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/)
client , err := storage . NewClient ( ctx , option . WithUserAgent ( fmt . Sprintf ( "Minio/%s (GPN:Minio;)" , minio . Version ) ) )
if err != nil {
return nil , err
}
gcs := & gcsGateway {
2018-02-09 18:19:30 -05:00
client : client ,
projectID : g . projectID ,
ctx : ctx ,
2017-12-05 20:58:09 -05:00
}
// Start background process to cleanup old files in minio.sys.tmp
go gcs . CleanupGCSMinioSysTmp ( )
return gcs , nil
}
// Production - FIXME: GCS is not production ready yet.
func ( g * GCS ) Production ( ) bool {
return false
2017-10-27 18:07:46 -04:00
}
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
}
2017-05-01 13:59:54 -04:00
// Convert Minio errors to minio object layer errors.
func gcsToObjectError ( err error , params ... string ) error {
if err == nil {
return nil
}
2017-12-05 20:58:09 -05:00
e , ok := err . ( * errors . Error )
2017-05-01 13:59:54 -04:00
if ! ok {
2017-12-05 20:58:09 -05:00
// Code should be fixed if this function is called without doing errors.Trace()
2017-05-01 13:59:54 -04:00
// Else handling different situations in this function makes this function complicated.
2017-12-05 20:58:09 -05:00
minio . ErrorIf ( err , "Expected type *Error" )
2017-05-01 13:59:54 -04:00
return err
}
2017-11-25 14:58:29 -05:00
err = e . Cause
2017-05-01 13:59:54 -04:00
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 ,
}
2017-11-25 14:58:29 -05:00
e . Cause = err
2017-05-01 13:59:54 -04:00
return e
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
}
2017-11-25 14:58:29 -05:00
e . Cause = err
2017-05-01 13:59:54 -04:00
return e
}
2017-05-05 21:49:26 -04:00
googleAPIErr , ok := err . ( * googleapi . Error )
2017-05-01 13:59:54 -04:00
if ! ok {
// We don't interpret non Minio errors. As minio errors will
// have StatusCode to help to convert to object errors.
2017-11-25 14:58:29 -05:00
e . Cause = err
2017-06-17 19:00:41 -04:00
return e
}
if len ( googleAPIErr . Errors ) == 0 {
2017-11-25 14:58:29 -05:00
e . Cause = err
2017-05-01 13:59:54 -04:00
return e
}
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
default :
err = fmt . Errorf ( "Unsupported error reason: %s" , reason )
}
2017-11-25 14:58:29 -05:00
e . Cause = err
2017-05-01 13:59:54 -04:00
return e
}
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 )
}
2017-05-01 13:59:54 -04:00
// gcsGateway - Implements gateway for Minio and GCS compatible object storage servers.
type gcsGateway struct {
2017-12-05 20:58:09 -05:00
minio . GatewayUnsupported
2018-02-09 18:19:30 -05:00
client * storage . Client
projectID string
ctx context . Context
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
}
2017-07-19 22:33:10 -04:00
// Cleanup old files in minio.sys.tmp of the given bucket.
func ( l * gcsGateway ) CleanupGCSMinioSysTmpBucket ( bucket string ) {
2017-12-05 20:58:09 -05:00
it := l . client . Bucket ( bucket ) . Objects ( l . 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 {
2017-12-05 20:58:09 -05:00
minio . ErrorIf ( err , "Object listing error on bucket %s during purging of old files in minio.sys.tmp" , bucket )
2017-07-19 22:33:10 -04:00
}
return
}
if time . Since ( attrs . Updated ) > gcsMultipartExpiry {
// Delete files older than 2 weeks.
err := l . client . Bucket ( bucket ) . Object ( attrs . Name ) . Delete ( l . ctx )
if err != nil {
2017-12-05 20:58:09 -05:00
minio . ErrorIf ( err , "Unable to delete %s/%s during purging of old files in minio.sys.tmp" , bucket , attrs . Name )
2017-07-19 22:33:10 -04:00
return
}
}
}
}
// Cleanup old files in minio.sys.tmp of all buckets.
func ( l * gcsGateway ) CleanupGCSMinioSysTmp ( ) {
for {
it := l . client . Buckets ( l . ctx , l . projectID )
for {
attrs , err := it . Next ( )
if err != nil {
if err != iterator . Done {
2017-12-05 20:58:09 -05:00
minio . ErrorIf ( err , "Bucket listing error during purging of old files in minio.sys.tmp" )
2017-07-19 22:33:10 -04:00
}
break
}
l . CleanupGCSMinioSysTmpBucket ( attrs . Name )
}
// 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.
2018-03-14 20:31:14 -04:00
func ( l * gcsGateway ) StorageInfo ( ctx context . Context ) minio . StorageInfo {
2017-12-05 20:58:09 -05:00
return minio . StorageInfo { }
2017-05-01 13:59:54 -04:00
}
2017-06-17 19:00:41 -04:00
// MakeBucketWithLocation - Create a new container on GCS backend.
2018-03-14 20:31:14 -04:00
func ( l * gcsGateway ) MakeBucketWithLocation ( ctx context . Context , bucket , location string ) error {
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
if location == "us-east-1" {
location = "us"
2017-05-01 13:59:54 -04:00
}
2017-06-17 19:00:41 -04:00
err := bkt . Create ( l . ctx , l . projectID , & storage . BucketAttrs {
Location : location ,
} )
2017-12-05 20:58:09 -05:00
return gcsToObjectError ( errors . Trace ( 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 ) {
2017-05-01 13:59:54 -04:00
attrs , err := l . client . Bucket ( bucket ) . Attrs ( l . ctx )
if err != nil {
2017-12-05 20:58:09 -05:00
return minio . BucketInfo { } , gcsToObjectError ( errors . Trace ( 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 ) {
2017-05-01 13:59:54 -04:00
it := l . client . Buckets ( l . ctx , l . projectID )
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 {
2017-12-05 20:58:09 -05:00
return buckets , gcsToObjectError ( errors . Trace ( 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.
2018-03-14 20:31:14 -04:00
func ( l * gcsGateway ) DeleteBucket ( ctx context . Context , bucket string ) error {
2017-12-05 20:58:09 -05:00
itObject := l . client . Bucket ( bucket ) . Objects ( l . ctx , & storage . Query {
Delimiter : "/" ,
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 {
2017-12-05 20:58:09 -05:00
return gcsToObjectError ( errors . Trace ( 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 {
2017-12-05 20:58:09 -05:00
return gcsToObjectError ( errors . Trace ( 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.
2017-12-05 20:58:09 -05:00
itObject = l . client . Bucket ( bucket ) . Objects ( l . 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 {
2017-12-05 20:58:09 -05:00
return gcsToObjectError ( errors . Trace ( err ) )
2017-06-23 20:57:25 -04:00
}
err = l . client . Bucket ( bucket ) . Object ( objAttrs . Name ) . Delete ( l . ctx )
if err != nil {
2017-12-05 20:58:09 -05:00
return gcsToObjectError ( errors . Trace ( err ) )
2017-06-23 20:57:25 -04:00
}
}
}
2017-05-01 13:59:54 -04:00
err := l . client . Bucket ( bucket ) . Delete ( l . ctx )
2017-12-05 20:58:09 -05:00
return gcsToObjectError ( errors . Trace ( 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 ) ,
}
length = length >> 7
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 ) {
2017-07-11 12:25:19 -04:00
it := l . client . Bucket ( bucket ) . Objects ( l . ctx , & storage . Query {
Delimiter : delimiter ,
Prefix : prefix ,
Versions : false ,
} )
2017-05-01 13:59:54 -04:00
isTruncated := false
nextMarker := ""
prefixes := [ ] string { }
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
// prefixing "##minio" to the GCS continuation token,
// e.g, "##minioCgRvYmoz"
//
// - Application supplied markers are used as-is to list
// object keys that appear after it in the lexicographical order.
// If application is using GCS continuation token we should
// strip the gcsTokenPrefix we added.
gcsMarker := isGCSMarker ( marker )
if gcsMarker {
it . PageInfo ( ) . Token = strings . TrimPrefix ( marker , gcsTokenPrefix )
}
2017-05-10 19:19:35 -04:00
it . PageInfo ( ) . MaxSize = maxKeys
2017-05-10 18:36:49 -04:00
2017-12-05 20:58:09 -05:00
objects := [ ] minio . ObjectInfo { }
2017-05-01 13:59:54 -04:00
for {
if len ( objects ) >= maxKeys {
// check if there is one next object and
// if that one next object is our hidden
// metadata folder, then just break
// otherwise we've truncated the output
attrs , _ := it . Next ( )
2017-12-05 20:58:09 -05:00
if attrs != nil && attrs . Prefix == minio . GatewayMinioSysTmp {
2017-05-01 13:59:54 -04:00
break
}
isTruncated = true
break
}
attrs , err := it . Next ( )
if err == iterator . Done {
break
2017-06-23 20:57:25 -04:00
}
if err != nil {
2017-12-05 20:58:09 -05:00
return minio . ListObjectsInfo { } , gcsToObjectError ( errors . Trace ( err ) , bucket , prefix )
2017-05-01 13:59:54 -04:00
}
2017-05-10 18:36:49 -04:00
nextMarker = toGCSPageToken ( attrs . Name )
2017-12-05 20:58:09 -05:00
if attrs . Prefix == minio . GatewayMinioSysTmp {
2017-06-23 20:57:25 -04:00
// We don't return our metadata prefix.
2017-05-01 13:59:54 -04:00
continue
2017-06-23 20:57:25 -04:00
}
2017-12-05 20:58:09 -05:00
if ! strings . HasPrefix ( prefix , minio . GatewayMinioSysTmp ) {
2017-06-23 20:57:25 -04:00
// 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.
2017-12-05 20:58:09 -05:00
if strings . HasPrefix ( attrs . Prefix , minio . GatewayMinioSysTmp ) ||
strings . HasPrefix ( attrs . Name , minio . GatewayMinioSysTmp ) {
2017-06-23 20:57:25 -04:00
continue
}
}
if attrs . Prefix != "" {
2017-05-01 13:59:54 -04:00
prefixes = append ( prefixes , attrs . Prefix )
continue
2017-06-23 20:57:25 -04:00
}
if ! gcsMarker && attrs . Name <= marker {
2017-06-09 22:48:20 -04:00
// if user supplied a marker don't append
// objects until we reach marker (and skip it).
continue
2017-05-01 13:59:54 -04:00
}
2017-12-05 20:58:09 -05:00
objects = append ( objects , minio . ObjectInfo {
2017-05-01 13:59:54 -04:00
Name : attrs . Name ,
Bucket : attrs . Bucket ,
ModTime : attrs . Updated ,
Size : attrs . Size ,
2017-12-05 20:58:09 -05:00
ETag : minio . ToS3ETag ( fmt . Sprintf ( "%d" , attrs . CRC32C ) ) ,
2017-05-01 13:59:54 -04:00
UserDefined : attrs . Metadata ,
ContentType : attrs . ContentType ,
ContentEncoding : attrs . ContentEncoding ,
} )
}
2017-12-05 20:58:09 -05:00
return minio . ListObjectsInfo {
2017-05-01 13:59:54 -04:00
IsTruncated : isTruncated ,
2017-06-09 22:48:20 -04:00
NextMarker : gcsTokenPrefix + 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 ) {
2017-07-11 12:25:19 -04:00
it := l . client . Bucket ( bucket ) . Objects ( l . ctx , & storage . Query {
Delimiter : delimiter ,
Prefix : prefix ,
Versions : false ,
} )
2017-05-01 13:59:54 -04:00
isTruncated := false
2017-06-29 14:19:55 -04:00
it . PageInfo ( ) . MaxSize = maxKeys
2017-05-01 13:59:54 -04:00
2017-06-29 14:19:55 -04:00
if continuationToken != "" {
// If client sends continuationToken, set it
it . PageInfo ( ) . Token = continuationToken
} else {
// else set the continuationToken to return
continuationToken = it . PageInfo ( ) . Token
if continuationToken != "" {
// If GCS SDK sets continuationToken, it means there are more than maxKeys in the current page
// and the response will be truncated
2017-05-01 13:59:54 -04:00
isTruncated = true
}
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
2017-06-29 14:19:55 -04:00
for {
2017-05-01 13:59:54 -04:00
attrs , err := it . Next ( )
if err == iterator . Done {
break
}
if err != nil {
2017-12-05 20:58:09 -05:00
return minio . ListObjectsV2Info { } , gcsToObjectError ( errors . Trace ( err ) , bucket , prefix )
2017-05-01 13:59:54 -04:00
}
2017-12-05 20:58:09 -05:00
if attrs . Prefix == minio . GatewayMinioSysTmp {
2017-06-29 14:19:55 -04:00
// We don't return our metadata prefix.
continue
}
2017-12-05 20:58:09 -05:00
if ! strings . HasPrefix ( prefix , minio . GatewayMinioSysTmp ) {
2017-06-29 14:19:55 -04:00
// 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.
2017-12-05 20:58:09 -05:00
if strings . HasPrefix ( attrs . Prefix , minio . GatewayMinioSysTmp ) ||
strings . HasPrefix ( attrs . Name , minio . GatewayMinioSysTmp ) {
2017-06-29 14:19:55 -04:00
continue
}
}
2017-05-01 13:59:54 -04:00
if attrs . Prefix != "" {
prefixes = append ( prefixes , attrs . Prefix )
continue
}
2017-05-31 18:40:33 -04:00
objects = append ( objects , fromGCSAttrsToObjectInfo ( attrs ) )
2017-05-01 13:59:54 -04:00
}
2017-12-05 20:58:09 -05:00
return minio . ListObjectsV2Info {
2017-05-01 13:59:54 -04:00
IsTruncated : isTruncated ,
ContinuationToken : continuationToken ,
2017-06-29 14:19:55 -04:00
NextContinuationToken : continuationToken ,
2017-05-01 13:59:54 -04:00
Prefixes : prefixes ,
Objects : objects ,
} , nil
}
// 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.
2018-03-14 20:31:14 -04:00
func ( l * gcsGateway ) GetObject ( ctx context . Context , bucket string , key string , startOffset int64 , length int64 , writer io . Writer , etag string ) 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
if _ , err := l . client . Bucket ( bucket ) . Attrs ( l . ctx ) ; err != nil {
2017-12-05 20:58:09 -05:00
return gcsToObjectError ( errors . Trace ( err ) , bucket )
2017-05-01 13:59:54 -04:00
}
object := l . client . Bucket ( bucket ) . Object ( key )
r , err := object . NewRangeReader ( l . ctx , startOffset , length )
if err != nil {
2017-12-05 20:58:09 -05:00
return gcsToObjectError ( errors . Trace ( err ) , bucket , key )
2017-05-01 13:59:54 -04:00
}
defer r . Close ( )
if _ , err := io . Copy ( writer , r ) ; err != nil {
2017-12-05 20:58:09 -05:00
return gcsToObjectError ( errors . Trace ( 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
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 ,
2017-12-05 20:58:09 -05:00
ETag : minio . ToS3ETag ( fmt . Sprintf ( "%d" , attrs . CRC32C ) ) ,
2017-05-01 13:59:54 -04:00
UserDefined : attrs . Metadata ,
ContentType : attrs . ContentType ,
ContentEncoding : attrs . ContentEncoding ,
}
}
// GetObjectInfo - reads object info and replies back ObjectInfo
2018-03-14 20:31:14 -04:00
func ( l * gcsGateway ) GetObjectInfo ( ctx context . Context , bucket string , object string ) ( 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
if _ , err := l . client . Bucket ( bucket ) . Attrs ( l . ctx ) ; err != nil {
2017-12-05 20:58:09 -05:00
return minio . ObjectInfo { } , gcsToObjectError ( errors . Trace ( err ) , bucket )
2017-05-01 13:59:54 -04:00
}
attrs , err := l . client . Bucket ( bucket ) . Object ( object ) . Attrs ( l . ctx )
if err != nil {
2017-12-05 20:58:09 -05:00
return minio . ObjectInfo { } , gcsToObjectError ( errors . Trace ( 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,
2018-03-14 20:31:14 -04:00
func ( l * gcsGateway ) PutObject ( ctx context . Context , bucket string , key string , data * hash . Reader , metadata map [ string ] string ) ( 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
if _ , err := l . client . Bucket ( bucket ) . Attrs ( l . ctx ) ; err != nil {
2017-12-05 20:58:09 -05:00
return minio . ObjectInfo { } , gcsToObjectError ( errors . Trace ( err ) , bucket )
2017-05-01 13:59:54 -04:00
}
object := l . client . Bucket ( bucket ) . Object ( key )
w := object . NewWriter ( l . ctx )
w . ContentType = metadata [ "content-type" ]
w . ContentEncoding = metadata [ "content-encoding" ]
2017-07-11 12:25:19 -04:00
w . Metadata = metadata
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.
2017-11-21 17:45:37 -05:00
w . CloseWithError ( err )
2017-12-05 20:58:09 -05:00
return minio . ObjectInfo { } , gcsToObjectError ( errors . Trace ( 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.
w . Close ( )
2017-05-10 19:52:38 -04:00
2017-07-11 12:25:19 -04:00
attrs , err := object . Attrs ( l . ctx )
if err != nil {
2017-12-05 20:58:09 -05:00
return minio . ObjectInfo { } , gcsToObjectError ( errors . Trace ( err ) , bucket , key )
2017-07-11 12:25:19 -04:00
}
2017-05-31 18:40:33 -04:00
return fromGCSAttrsToObjectInfo ( 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-02-21 03:48:47 -05:00
srcInfo minio . ObjectInfo ) ( minio . ObjectInfo , error ) {
2017-07-11 12:25:19 -04:00
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-02-21 03:48:47 -05:00
copier . ObjectAttrs . Metadata = srcInfo . UserDefined
2017-09-26 14:04:42 -04:00
attrs , err := copier . Run ( l . ctx )
2017-05-01 13:59:54 -04:00
if err != nil {
2017-12-05 20:58:09 -05:00
return minio . ObjectInfo { } , gcsToObjectError ( errors . Trace ( 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
2018-03-14 20:31:14 -04:00
func ( l * gcsGateway ) DeleteObject ( ctx context . Context , bucket string , object string ) error {
2017-05-01 13:59:54 -04:00
err := l . client . Bucket ( bucket ) . Object ( object ) . Delete ( l . ctx )
if err != nil {
2017-12-05 20:58:09 -05:00
return gcsToObjectError ( errors . Trace ( err ) , bucket , object )
2017-05-01 13:59:54 -04:00
}
return nil
}
// NewMultipartUpload - upload object in multiple parts
2018-03-14 20:31:14 -04:00
func ( l * gcsGateway ) NewMultipartUpload ( ctx context . Context , bucket string , key string , metadata map [ string ] string ) ( 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
2017-06-17 19:00:41 -04:00
w := l . client . Bucket ( bucket ) . Object ( meta ) . NewWriter ( l . ctx )
2017-07-11 12:25:19 -04:00
defer w . Close ( )
2017-05-01 13:59:54 -04:00
w . ContentType = metadata [ "content-type" ]
w . ContentEncoding = metadata [ "content-encoding" ]
w . Metadata = metadata
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 {
2017-12-05 20:58:09 -05:00
return "" , gcsToObjectError ( errors . Trace ( err ) , bucket , key )
2017-06-17 19:00:41 -04:00
}
2017-05-01 13:59:54 -04:00
return uploadID , nil
}
2017-06-17 19:00:41 -04:00
// ListMultipartUploads - lists all multipart uploads.
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 ) {
2017-12-05 20:58:09 -05:00
return minio . ListMultipartsInfo {
2017-06-17 19:00:41 -04:00
KeyMarker : keyMarker ,
UploadIDMarker : uploadIDMarker ,
MaxUploads : maxUploads ,
Prefix : prefix ,
Delimiter : delimiter ,
} , 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 {
2017-07-11 12:25:19 -04:00
_ , err := l . client . Bucket ( bucket ) . Object ( gcsMultipartMetaName ( uploadID ) ) . Attrs ( l . ctx )
2017-12-05 20:58:09 -05:00
return gcsToObjectError ( errors . Trace ( 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-03-14 20:31:14 -04:00
func ( l * gcsGateway ) PutObjectPart ( ctx context . Context , bucket string , key string , uploadID string , partNumber int , data * hash . Reader ) ( minio . PartInfo , error ) {
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 ) )
2017-06-17 19:00:41 -04:00
w := object . NewWriter ( l . ctx )
// 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 ( )
2017-12-05 20:58:09 -05:00
return minio . PartInfo { } , gcsToObjectError ( errors . Trace ( 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.
w . Close ( )
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-07-11 12:25:19 -04:00
2017-06-17 19:00:41 -04:00
}
2017-05-01 13:59:54 -04:00
2017-06-17 19:00:41 -04:00
// ListObjectParts returns all object parts for specified object in specified bucket
2018-03-14 20:31:14 -04:00
func ( l * gcsGateway ) ListObjectParts ( ctx context . Context , bucket string , key string , uploadID string , partNumberMarker int , maxParts int ) ( minio . ListPartsInfo , error ) {
return minio . ListPartsInfo { } , l . checkUploadIDExists ( ctx , bucket , key , uploadID )
2017-05-01 13:59:54 -04:00
}
2017-06-17 19:00:41 -04:00
// Called by AbortMultipartUpload and CompleteMultipartUpload for cleaning up.
func ( l * gcsGateway ) cleanupMultipartUpload ( 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
it := l . client . Bucket ( bucket ) . Objects ( l . 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 {
2017-12-05 20:58:09 -05:00
return gcsToObjectError ( errors . Trace ( 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.
object . Delete ( l . 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
2018-03-14 20:31:14 -04:00
func ( l * gcsGateway ) AbortMultipartUpload ( ctx context . Context , bucket string , key string , uploadID string ) error {
if err := l . checkUploadIDExists ( ctx , bucket , key , uploadID ) ; err != nil {
2017-07-11 12:25:19 -04:00
return err
}
2017-06-17 19:00:41 -04:00
return l . cleanupMultipartUpload ( bucket , key , uploadID )
}
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
// be composed in a single operation. There is a limit (currently 1024) to the total
// number of components for a given composite object. This means you can append to
// each object at most 1023 times. There is a per-project rate limit (currently 200)
// to the number of components you can compose per second. This rate counts both the
// components being appended to a composite object as well as the components being
// copied when the composite object of which they are a part is copied.
2018-03-14 20:31:14 -04:00
func ( l * gcsGateway ) CompleteMultipartUpload ( ctx context . Context , bucket string , key string , uploadID string , uploadedParts [ ] minio . CompletePart ) ( 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
2017-06-17 19:00:41 -04:00
partZeroAttrs , err := object . Attrs ( l . ctx )
if err != nil {
2017-12-05 20:58:09 -05:00
return minio . ObjectInfo { } , gcsToObjectError ( errors . Trace ( err ) , bucket , key , uploadID )
2017-06-17 19:00:41 -04:00
}
2017-07-11 12:25:19 -04:00
2017-06-17 19:00:41 -04:00
r , err := object . NewReader ( l . ctx )
2017-05-01 13:59:54 -04:00
if err != nil {
2017-12-05 20:58:09 -05:00
return minio . ObjectInfo { } , gcsToObjectError ( errors . Trace ( 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 {
2017-12-05 20:58:09 -05:00
return minio . ObjectInfo { } , gcsToObjectError ( errors . Trace ( err ) , bucket , key )
2017-06-17 19:00:41 -04:00
}
2017-07-11 12:25:19 -04:00
if multipartMeta . Version != gcsMinioMultipartMetaCurrentVersion {
2017-12-05 20:58:09 -05:00
return minio . ObjectInfo { } , gcsToObjectError ( errors . Trace ( 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 ) ) )
2017-11-06 13:09:21 -05:00
partAttr , pErr := l . client . Bucket ( bucket ) . Object ( gcsMultipartDataName ( uploadID , uploadedPart . PartNumber , uploadedPart . ETag ) ) . Attrs ( l . ctx )
if pErr != nil {
2017-12-05 20:58:09 -05:00
return minio . ObjectInfo { } , gcsToObjectError ( errors . Trace ( 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 {
return minio . ObjectInfo { } , errors . Trace ( minio . PartTooSmall {
2017-11-06 13:09:21 -05:00
PartNumber : uploadedParts [ i ] . PartNumber ,
PartSize : size ,
PartETag : uploadedParts [ i ] . ETag ,
} )
}
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
2017-07-11 12:25:19 -04:00
if _ , err = composer . Run ( l . ctx ) ; err != nil {
2017-12-05 20:58:09 -05:00
return minio . ObjectInfo { } , gcsToObjectError ( errors . Trace ( 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
composer . Metadata = partZeroAttrs . Metadata
attrs , err := composer . Run ( l . ctx )
2017-06-17 19:00:41 -04:00
if err != nil {
2017-12-05 20:58:09 -05:00
return minio . ObjectInfo { } , gcsToObjectError ( errors . Trace ( err ) , bucket , key )
2017-05-01 13:59:54 -04:00
}
2017-06-17 19:00:41 -04:00
if err = l . cleanupMultipartUpload ( bucket , key , uploadID ) ; err != nil {
2017-12-05 20:58:09 -05:00
return minio . ObjectInfo { } , gcsToObjectError ( errors . Trace ( 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-03-14 20:31:14 -04:00
func ( l * gcsGateway ) SetBucketPolicy ( ctx context . Context , bucket string , policyInfo policy . BucketAccessPolicy ) error {
2017-12-05 20:58:09 -05:00
var policies [ ] minio . BucketAccessPolicy
2017-05-08 20:55:39 -04:00
2018-03-14 10:08:29 -04:00
for prefix , policy := range policy . 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 {
2017-12-05 20:58:09 -05:00
return errors . Trace ( minio . NotImplemented { } )
2017-06-23 20:57:25 -04:00
}
if policies [ 0 ] . Prefix != prefix {
2017-12-05 20:58:09 -05:00
return errors . Trace ( minio . NotImplemented { } )
2017-05-08 20:55:39 -04:00
}
acl := l . client . Bucket ( bucket ) . ACL ( )
if policies [ 0 ] . Policy == policy . BucketPolicyNone {
if err := acl . Delete ( l . ctx , storage . AllUsers ) ; err != nil {
2017-12-05 20:58:09 -05:00
return gcsToObjectError ( errors . Trace ( 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 {
case policy . BucketPolicyReadOnly :
2017-05-08 20:55:39 -04:00
role = storage . RoleReader
2017-05-10 13:02:45 -04:00
case policy . BucketPolicyWriteOnly :
2017-05-08 20:55:39 -04:00
role = storage . RoleWriter
2017-05-10 13:02:45 -04:00
default :
2017-12-05 20:58:09 -05:00
return errors . Trace ( minio . NotImplemented { } )
2017-05-08 20:55:39 -04:00
}
if err := acl . Set ( l . ctx , storage . AllUsers , role ) ; err != nil {
2017-12-05 20:58:09 -05:00
return gcsToObjectError ( errors . Trace ( 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-03-14 20:31:14 -04:00
func ( l * gcsGateway ) GetBucketPolicy ( ctx context . Context , bucket string ) ( policy . BucketAccessPolicy , error ) {
2017-07-11 12:25:19 -04:00
rules , err := l . client . Bucket ( bucket ) . ACL ( ) . List ( l . ctx )
2017-05-08 20:55:39 -04:00
if err != nil {
2017-12-05 20:58:09 -05:00
return policy . BucketAccessPolicy { } , gcsToObjectError ( errors . Trace ( err ) , bucket )
2017-05-08 20:55:39 -04:00
}
policyInfo := policy . BucketAccessPolicy { Version : "2012-10-17" }
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
}
switch r . Role {
case storage . RoleReader :
policyInfo . Statements = policy . SetPolicy ( policyInfo . Statements , policy . BucketPolicyReadOnly , bucket , "" )
case storage . RoleWriter :
policyInfo . Statements = policy . SetPolicy ( policyInfo . Statements , policy . BucketPolicyWriteOnly , bucket , "" )
}
}
2017-10-26 21:01:00 -04:00
// Return NoSuchBucketPolicy error, when policy is not set
if len ( policyInfo . Statements ) == 0 {
2017-12-05 20:58:09 -05:00
return policy . BucketAccessPolicy { } , gcsToObjectError ( errors . Trace ( minio . PolicyNotFound { } ) , bucket )
2017-10-26 21:01:00 -04:00
}
2017-05-08 20:55:39 -04:00
return policyInfo , 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
2017-07-11 12:25:19 -04:00
if err := l . client . Bucket ( bucket ) . ACL ( ) . Delete ( l . ctx , storage . AllUsers ) ; err != nil {
2017-12-05 20:58:09 -05:00
return gcsToObjectError ( errors . Trace ( err ) , bucket )
2017-05-08 20:55:39 -04:00
}
return nil
2017-05-01 13:59:54 -04:00
}