2017-03-16 15:21:58 -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 azure
|
2017-03-16 15:21:58 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2017-09-28 18:23:46 -04:00
|
|
|
"strings"
|
2017-03-16 15:21:58 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/storage"
|
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-03-16 15:21:58 -04:00
|
|
|
)
|
|
|
|
|
2017-09-28 18:23:46 -04:00
|
|
|
// Copied from github.com/Azure/azure-sdk-for-go/storage/container.go
|
|
|
|
func azureListBlobsGetParameters(p storage.ListBlobsParameters) url.Values {
|
|
|
|
out := url.Values{}
|
|
|
|
|
|
|
|
if p.Prefix != "" {
|
|
|
|
out.Set("prefix", p.Prefix)
|
|
|
|
}
|
|
|
|
if p.Delimiter != "" {
|
|
|
|
out.Set("delimiter", p.Delimiter)
|
|
|
|
}
|
|
|
|
if p.Marker != "" {
|
|
|
|
out.Set("marker", p.Marker)
|
|
|
|
}
|
|
|
|
if p.Include != nil {
|
|
|
|
addString := func(datasets []string, include bool, text string) []string {
|
|
|
|
if include {
|
|
|
|
datasets = append(datasets, text)
|
|
|
|
}
|
|
|
|
return datasets
|
|
|
|
}
|
|
|
|
|
|
|
|
include := []string{}
|
|
|
|
include = addString(include, p.Include.Snapshots, "snapshots")
|
|
|
|
include = addString(include, p.Include.Metadata, "metadata")
|
|
|
|
include = addString(include, p.Include.UncommittedBlobs, "uncommittedblobs")
|
|
|
|
include = addString(include, p.Include.Copy, "copy")
|
|
|
|
fullInclude := strings.Join(include, ",")
|
|
|
|
out.Set("include", fullInclude)
|
|
|
|
}
|
|
|
|
if p.MaxResults != 0 {
|
|
|
|
out.Set("maxresults", fmt.Sprintf("%v", p.MaxResults))
|
|
|
|
}
|
|
|
|
if p.Timeout != 0 {
|
|
|
|
out.Set("timeout", fmt.Sprintf("%v", p.Timeout))
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2017-06-24 01:07:46 -04:00
|
|
|
// Make anonymous HTTP request to azure endpoint.
|
|
|
|
func azureAnonRequest(verb, urlStr string, header http.Header) (*http.Response, error) {
|
|
|
|
req, err := http.NewRequest(verb, urlStr, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if header != nil {
|
|
|
|
req.Header = header
|
|
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4XX and 5XX are error HTTP codes.
|
|
|
|
if resp.StatusCode >= 400 && resp.StatusCode <= 511 {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(respBody) == 0 {
|
|
|
|
// no error in response body, might happen in HEAD requests
|
|
|
|
return nil, storage.AzureStorageServiceError{
|
|
|
|
StatusCode: resp.StatusCode,
|
|
|
|
Code: resp.Status,
|
|
|
|
Message: "no response body was available for error status code",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Response contains Azure storage service error object.
|
|
|
|
var storageErr storage.AzureStorageServiceError
|
|
|
|
if err := xml.Unmarshal(respBody, &storageErr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
storageErr.StatusCode = resp.StatusCode
|
|
|
|
return nil, storageErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2017-03-16 15:21:58 -04:00
|
|
|
// AnonGetBucketInfo - Get bucket metadata from azure anonymously.
|
2017-12-05 20:58:09 -05:00
|
|
|
func (a *azureObjects) AnonGetBucketInfo(bucket string) (bucketInfo minio.BucketInfo, err error) {
|
2017-09-28 18:23:46 -04:00
|
|
|
blobURL := a.client.GetContainerReference(bucket).GetBlobReference("").GetURL()
|
|
|
|
url, err := url.Parse(blobURL)
|
2017-03-16 15:21:58 -04:00
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return bucketInfo, azureToObjectError(errors.Trace(err))
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|
|
|
|
url.RawQuery = "restype=container"
|
2017-12-05 20:58:09 -05:00
|
|
|
resp, err := azureAnonRequest(http.MethodHead, url.String(), nil)
|
2017-03-16 15:21:58 -04:00
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return bucketInfo, azureToObjectError(errors.Trace(err), bucket)
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|
2017-06-24 01:07:46 -04:00
|
|
|
defer resp.Body.Close()
|
2017-03-16 15:21:58 -04:00
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2017-12-05 20:58:09 -05:00
|
|
|
return bucketInfo, azureToObjectError(errors.Trace(minio.AnonErrToObjectErr(resp.StatusCode, bucket)), bucket)
|
2017-03-16 15:21:58 -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-03-16 15:21:58 -04:00
|
|
|
}
|
2017-09-28 22:37:09 -04:00
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.BucketInfo{
|
2017-03-16 15:21:58 -04:00
|
|
|
Name: bucket,
|
|
|
|
Created: t,
|
2017-12-05 20:58:09 -05:00
|
|
|
}, nil
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AnonGetObject - SendGET request without authentication.
|
|
|
|
// This is needed when clients send GET requests on objects that can be downloaded without auth.
|
2018-01-12 23:34:52 -05:00
|
|
|
func (a *azureObjects) AnonGetObject(bucket, object string, startOffset int64, length int64, writer io.Writer, etag string) (err error) {
|
2017-06-24 01:07:46 -04:00
|
|
|
h := make(http.Header)
|
2017-03-16 15:21:58 -04:00
|
|
|
if length > 0 && startOffset > 0 {
|
2017-06-24 01:07:46 -04:00
|
|
|
h.Add("Range", fmt.Sprintf("bytes=%d-%d", startOffset, startOffset+length-1))
|
2017-03-16 15:21:58 -04:00
|
|
|
} else if startOffset > 0 {
|
2017-06-24 01:07:46 -04:00
|
|
|
h.Add("Range", fmt.Sprintf("bytes=%d-", startOffset))
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|
|
|
|
|
2017-09-28 18:23:46 -04:00
|
|
|
blobURL := a.client.GetContainerReference(bucket).GetBlobReference(object).GetURL()
|
2017-12-05 20:58:09 -05:00
|
|
|
resp, err := azureAnonRequest(http.MethodGet, blobURL, h)
|
2017-03-16 15:21:58 -04:00
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return azureToObjectError(errors.Trace(err), bucket, object)
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusPartialContent && resp.StatusCode != http.StatusOK {
|
2017-12-05 20:58:09 -05:00
|
|
|
return azureToObjectError(errors.Trace(minio.AnonErrToObjectErr(resp.StatusCode, bucket, object)), bucket, object)
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(writer, resp.Body)
|
2017-11-25 14:58:29 -05:00
|
|
|
return errors.Trace(err)
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AnonGetObjectInfo - Send HEAD request without authentication and convert the
|
|
|
|
// result to ObjectInfo.
|
2017-12-05 20:58:09 -05:00
|
|
|
func (a *azureObjects) AnonGetObjectInfo(bucket, object string) (objInfo minio.ObjectInfo, err error) {
|
2017-09-28 18:23:46 -04:00
|
|
|
blobURL := a.client.GetContainerReference(bucket).GetBlobReference(object).GetURL()
|
2017-12-05 20:58:09 -05:00
|
|
|
resp, err := azureAnonRequest(http.MethodHead, blobURL, nil)
|
2017-03-16 15:21:58 -04:00
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return objInfo, azureToObjectError(errors.Trace(err), bucket, object)
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|
2017-06-24 01:07:46 -04:00
|
|
|
defer resp.Body.Close()
|
2017-03-16 15:21:58 -04:00
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2017-12-05 20:58:09 -05:00
|
|
|
return objInfo, azureToObjectError(errors.Trace(minio.AnonErrToObjectErr(resp.StatusCode, bucket, object)), bucket, object)
|
2017-03-16 15:21:58 -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, azureToObjectError(errors.Trace(fmt.Errorf("Unexpected error")), bucket, object)
|
2017-03-16 15:21:58 -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-03-16 15:21:58 -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-14 15:05:51 -04:00
|
|
|
objInfo.ETag = resp.Header.Get("Etag")
|
2017-03-16 15:21:58 -04:00
|
|
|
objInfo.ModTime = t
|
|
|
|
objInfo.Name = object
|
|
|
|
objInfo.Size = contentLength
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// AnonListObjects - Use Azure equivalent ListBlobs.
|
2017-12-05 20:58:09 -05:00
|
|
|
func (a *azureObjects) AnonListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (result minio.ListObjectsInfo, err error) {
|
2017-03-16 15:21:58 -04:00
|
|
|
params := storage.ListBlobsParameters{
|
|
|
|
Prefix: prefix,
|
|
|
|
Marker: marker,
|
|
|
|
Delimiter: delimiter,
|
|
|
|
MaxResults: uint(maxKeys),
|
|
|
|
}
|
|
|
|
|
|
|
|
q := azureListBlobsGetParameters(params)
|
|
|
|
q.Set("restype", "container")
|
|
|
|
q.Set("comp", "list")
|
|
|
|
|
2017-09-28 18:23:46 -04:00
|
|
|
blobURL := a.client.GetContainerReference(bucket).GetBlobReference("").GetURL()
|
|
|
|
url, err := url.Parse(blobURL)
|
2017-03-16 15:21:58 -04:00
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return result, azureToObjectError(errors.Trace(err))
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
resp, err := azureAnonRequest(http.MethodGet, url.String(), nil)
|
2017-03-16 15:21:58 -04:00
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return result, azureToObjectError(errors.Trace(err))
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var listResp storage.BlobListResponse
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return result, azureToObjectError(errors.Trace(err))
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|
|
|
|
err = xml.Unmarshal(data, &listResp)
|
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return result, azureToObjectError(errors.Trace(err))
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
result.IsTruncated = listResp.NextMarker != ""
|
|
|
|
result.NextMarker = listResp.NextMarker
|
|
|
|
for _, object := range listResp.Blobs {
|
2017-12-05 20:58:09 -05:00
|
|
|
result.Objects = append(result.Objects, minio.ObjectInfo{
|
2017-03-16 15:21:58 -04:00
|
|
|
Bucket: bucket,
|
|
|
|
Name: object.Name,
|
2017-09-28 18:23:46 -04:00
|
|
|
ModTime: time.Time(object.Properties.LastModified),
|
2017-03-16 15:21:58 -04:00
|
|
|
Size: object.Properties.ContentLength,
|
2017-05-14 15:05:51 -04:00
|
|
|
ETag: object.Properties.Etag,
|
2017-03-16 15:21:58 -04:00
|
|
|
ContentType: object.Properties.ContentType,
|
|
|
|
ContentEncoding: object.Properties.ContentEncoding,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
result.Prefixes = listResp.BlobPrefixes
|
|
|
|
return result, nil
|
|
|
|
}
|
2017-06-17 01:17:00 -04:00
|
|
|
|
|
|
|
// AnonListObjectsV2 - List objects in V2 mode, anonymously
|
2017-12-05 20:58:09 -05:00
|
|
|
func (a *azureObjects) AnonListObjectsV2(bucket, prefix, continuationToken, delimiter string, maxKeys int, fetchOwner bool, startAfter string) (result minio.ListObjectsV2Info, err error) {
|
2017-06-17 01:17:00 -04:00
|
|
|
params := storage.ListBlobsParameters{
|
|
|
|
Prefix: prefix,
|
|
|
|
Marker: continuationToken,
|
|
|
|
Delimiter: delimiter,
|
|
|
|
MaxResults: uint(maxKeys),
|
|
|
|
}
|
|
|
|
|
|
|
|
q := azureListBlobsGetParameters(params)
|
|
|
|
q.Set("restype", "container")
|
|
|
|
q.Set("comp", "list")
|
|
|
|
|
2017-09-28 18:23:46 -04:00
|
|
|
blobURL := a.client.GetContainerReference(bucket).GetBlobReference("").GetURL()
|
|
|
|
url, err := url.Parse(blobURL)
|
2017-06-17 01:17:00 -04:00
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return result, azureToObjectError(errors.Trace(err))
|
2017-06-17 01:17:00 -04:00
|
|
|
}
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
resp, err := http.Get(url.String())
|
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return result, azureToObjectError(errors.Trace(err))
|
2017-06-17 01:17:00 -04:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var listResp storage.BlobListResponse
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return result, azureToObjectError(errors.Trace(err))
|
2017-06-17 01:17:00 -04:00
|
|
|
}
|
|
|
|
err = xml.Unmarshal(data, &listResp)
|
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return result, azureToObjectError(errors.Trace(err))
|
2017-06-17 01:17:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If NextMarker is not empty, this means response is truncated and NextContinuationToken should be set
|
|
|
|
if listResp.NextMarker != "" {
|
|
|
|
result.IsTruncated = true
|
|
|
|
result.NextContinuationToken = listResp.NextMarker
|
|
|
|
}
|
|
|
|
for _, object := range listResp.Blobs {
|
2017-12-05 20:58:09 -05:00
|
|
|
result.Objects = append(result.Objects, minio.ObjectInfo{
|
2017-06-17 01:17:00 -04:00
|
|
|
Bucket: bucket,
|
|
|
|
Name: object.Name,
|
2017-09-28 18:23:46 -04:00
|
|
|
ModTime: time.Time(object.Properties.LastModified),
|
2017-06-17 01:17:00 -04:00
|
|
|
Size: object.Properties.ContentLength,
|
2017-12-05 20:58:09 -05:00
|
|
|
ETag: minio.CanonicalizeETag(object.Properties.Etag),
|
2017-06-17 01:17:00 -04:00
|
|
|
ContentType: object.Properties.ContentType,
|
|
|
|
ContentEncoding: object.Properties.ContentEncoding,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
result.Prefixes = listResp.BlobPrefixes
|
|
|
|
return result, nil
|
|
|
|
}
|