2017-05-01 13:59:54 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2017 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-12-05 20:58:09 -05:00
|
|
|
package gcs
|
2017-05-01 13:59:54 -04:00
|
|
|
|
2017-05-05 21:49:26 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2017-11-25 14:58:29 -05:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/errors"
|
2017-12-05 20:58:09 -05:00
|
|
|
|
|
|
|
minio "github.com/minio/minio/cmd"
|
2017-05-05 21:49:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func toGCSPublicURL(bucket, object string) string {
|
|
|
|
return fmt.Sprintf("https://storage.googleapis.com/%s/%s", bucket, object)
|
|
|
|
}
|
|
|
|
|
2017-05-01 13:59:54 -04:00
|
|
|
// AnonGetObject - Get object anonymously
|
2018-01-12 23:34:52 -05:00
|
|
|
func (l *gcsGateway) AnonGetObject(bucket string, object string, startOffset int64, length int64, writer io.Writer, etag string) error {
|
2017-05-05 21:49:26 -04:00
|
|
|
req, err := http.NewRequest("GET", toGCSPublicURL(bucket, object), nil)
|
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return gcsToObjectError(errors.Trace(err), bucket, object)
|
2017-05-05 21:49:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if length > 0 && startOffset > 0 {
|
|
|
|
req.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", startOffset, startOffset+length-1))
|
|
|
|
} else if startOffset > 0 {
|
|
|
|
req.Header.Add("Range", fmt.Sprintf("bytes=%d-", startOffset))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return gcsToObjectError(errors.Trace(err), bucket, object)
|
2017-05-05 21:49:26 -04:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusPartialContent && resp.StatusCode != http.StatusOK {
|
2017-12-05 20:58:09 -05:00
|
|
|
return gcsToObjectError(errors.Trace(minio.AnonErrToObjectErr(resp.StatusCode, bucket, object)), bucket, object)
|
2017-05-05 21:49:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(writer, resp.Body)
|
2017-11-25 14:58:29 -05:00
|
|
|
return gcsToObjectError(errors.Trace(err), bucket, object)
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AnonGetObjectInfo - Get object info anonymously
|
2017-12-05 20:58:09 -05:00
|
|
|
func (l *gcsGateway) AnonGetObjectInfo(bucket string, object string) (objInfo minio.ObjectInfo, err error) {
|
2017-05-05 21:49:26 -04:00
|
|
|
resp, err := http.Head(toGCSPublicURL(bucket, object))
|
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return objInfo, gcsToObjectError(errors.Trace(err), bucket, object)
|
2017-05-05 21:49:26 -04:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2017-12-05 20:58:09 -05:00
|
|
|
return objInfo, gcsToObjectError(errors.Trace(minio.AnonErrToObjectErr(resp.StatusCode, bucket, object)), bucket, object)
|
2017-05-05 21:49:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var contentLength int64
|
|
|
|
contentLengthStr := resp.Header.Get("Content-Length")
|
|
|
|
if contentLengthStr != "" {
|
|
|
|
contentLength, err = strconv.ParseInt(contentLengthStr, 0, 64)
|
|
|
|
if err != nil {
|
2017-12-05 20:58:09 -05:00
|
|
|
return objInfo, gcsToObjectError(errors.Trace(fmt.Errorf("Unexpected error")), bucket, object)
|
2017-05-05 21:49:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := time.Parse(time.RFC1123, resp.Header.Get("Last-Modified"))
|
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return objInfo, errors.Trace(err)
|
2017-05-05 21:49:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
objInfo.ModTime = t
|
|
|
|
objInfo.Bucket = bucket
|
|
|
|
objInfo.UserDefined = make(map[string]string)
|
|
|
|
if resp.Header.Get("Content-Encoding") != "" {
|
|
|
|
objInfo.UserDefined["Content-Encoding"] = resp.Header.Get("Content-Encoding")
|
|
|
|
}
|
|
|
|
objInfo.UserDefined["Content-Type"] = resp.Header.Get("Content-Type")
|
2017-05-17 08:59:20 -04:00
|
|
|
objInfo.ETag = resp.Header.Get("Etag")
|
2017-05-05 21:49:26 -04:00
|
|
|
objInfo.ModTime = t
|
|
|
|
objInfo.Name = object
|
|
|
|
objInfo.Size = contentLength
|
|
|
|
return
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AnonListObjects - List objects anonymously
|
2017-12-05 20:58:09 -05:00
|
|
|
func (l *gcsGateway) AnonListObjects(bucket string, prefix string, marker string, delimiter string, maxKeys int) (minio.ListObjectsInfo, error) {
|
2017-05-05 21:49:26 -04:00
|
|
|
result, err := l.anonClient.ListObjects(bucket, prefix, marker, delimiter, maxKeys)
|
|
|
|
if err != nil {
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.ListObjectsInfo{}, minio.ErrorRespToObjectError(errors.Trace(err), bucket)
|
2017-05-05 21:49:26 -04:00
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.FromMinioClientListBucketResult(bucket, result), nil
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2017-06-23 20:35:45 -04:00
|
|
|
// AnonListObjectsV2 - List objects in V2 mode, anonymously
|
2017-12-05 20:58:09 -05:00
|
|
|
func (l *gcsGateway) AnonListObjectsV2(bucket, prefix, continuationToken, delimiter string, maxKeys int, fetchOwner bool, startAfter string) (minio.ListObjectsV2Info, error) {
|
2017-06-23 20:35:45 -04:00
|
|
|
// Request V1 List Object to the backend
|
|
|
|
result, err := l.anonClient.ListObjects(bucket, prefix, continuationToken, delimiter, maxKeys)
|
|
|
|
if err != nil {
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.ListObjectsV2Info{}, minio.ErrorRespToObjectError(errors.Trace(err), bucket)
|
2017-06-23 20:35:45 -04:00
|
|
|
}
|
|
|
|
// translate V1 Result to V2Info
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.FromMinioClientListBucketResultToV2Info(bucket, result), nil
|
2017-06-22 15:08:48 -04:00
|
|
|
}
|
|
|
|
|
2017-05-01 13:59:54 -04:00
|
|
|
// AnonGetBucketInfo - Get bucket metadata anonymously.
|
2017-12-05 20:58:09 -05:00
|
|
|
func (l *gcsGateway) AnonGetBucketInfo(bucket string) (bucketInfo minio.BucketInfo, err error) {
|
2017-05-05 21:49:26 -04:00
|
|
|
resp, err := http.Head(toGCSPublicURL(bucket, ""))
|
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return bucketInfo, gcsToObjectError(errors.Trace(err))
|
2017-05-05 21:49:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2017-12-05 20:58:09 -05:00
|
|
|
return bucketInfo, gcsToObjectError(errors.Trace(minio.AnonErrToObjectErr(resp.StatusCode, bucket)), bucket)
|
2017-05-05 21:49:26 -04:00
|
|
|
}
|
|
|
|
|
2017-09-28 22:37:09 -04:00
|
|
|
t, err := time.Parse(time.RFC1123, resp.Header.Get("Last-Modified"))
|
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return bucketInfo, errors.Trace(err)
|
2017-09-28 22:37:09 -04:00
|
|
|
}
|
|
|
|
|
2017-05-05 21:49:26 -04:00
|
|
|
// Last-Modified date being returned by GCS
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.BucketInfo{
|
2017-09-28 22:37:09 -04:00
|
|
|
Name: bucket,
|
|
|
|
Created: t,
|
2017-05-05 21:49:26 -04:00
|
|
|
}, nil
|
2017-05-01 13:59:54 -04:00
|
|
|
}
|