2015-02-05 00:37:43 +00:00
|
|
|
package storage
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// Copyright 2017 Microsoft Corporation
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2015-02-05 00:37:43 +00:00
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Blob is an entry in BlobListResponse.
|
|
|
|
type Blob struct {
|
2018-05-21 19:05:11 +00:00
|
|
|
Container *Container
|
2015-02-05 00:37:43 +00:00
|
|
|
Name string `xml:"Name"`
|
2018-05-21 19:05:11 +00:00
|
|
|
Snapshot time.Time `xml:"Snapshot"`
|
2015-02-05 00:37:43 +00:00
|
|
|
Properties BlobProperties `xml:"Properties"`
|
2016-10-28 22:46:05 +00:00
|
|
|
Metadata BlobMetadata `xml:"Metadata"`
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// PutBlobOptions includes the options any put blob operation
|
|
|
|
// (page, block, append)
|
|
|
|
type PutBlobOptions struct {
|
|
|
|
Timeout uint
|
|
|
|
LeaseID string `header:"x-ms-lease-id"`
|
|
|
|
Origin string `header:"Origin"`
|
|
|
|
IfModifiedSince *time.Time `header:"If-Modified-Since"`
|
|
|
|
IfUnmodifiedSince *time.Time `header:"If-Unmodified-Since"`
|
|
|
|
IfMatch string `header:"If-Match"`
|
|
|
|
IfNoneMatch string `header:"If-None-Match"`
|
|
|
|
RequestID string `header:"x-ms-client-request-id"`
|
|
|
|
}
|
|
|
|
|
2016-10-28 22:46:05 +00:00
|
|
|
// BlobMetadata is a set of custom name/value pairs.
|
|
|
|
//
|
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/dd179404.aspx
|
|
|
|
type BlobMetadata map[string]string
|
|
|
|
|
|
|
|
type blobMetadataEntries struct {
|
|
|
|
Entries []blobMetadataEntry `xml:",any"`
|
|
|
|
}
|
|
|
|
type blobMetadataEntry struct {
|
|
|
|
XMLName xml.Name
|
|
|
|
Value string `xml:",chardata"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalXML converts the xml:Metadata into Metadata map
|
|
|
|
func (bm *BlobMetadata) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|
|
|
var entries blobMetadataEntries
|
|
|
|
if err := d.DecodeElement(&entries, &start); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, entry := range entries.Entries {
|
|
|
|
if *bm == nil {
|
|
|
|
*bm = make(BlobMetadata)
|
|
|
|
}
|
|
|
|
(*bm)[strings.ToLower(entry.XMLName.Local)] = entry.Value
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalXML implements the xml.Marshaler interface. It encodes
|
|
|
|
// metadata name/value pairs as they would appear in an Azure
|
|
|
|
// ListBlobs response.
|
|
|
|
func (bm BlobMetadata) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
|
|
|
|
entries := make([]blobMetadataEntry, 0, len(bm))
|
|
|
|
for k, v := range bm {
|
|
|
|
entries = append(entries, blobMetadataEntry{
|
|
|
|
XMLName: xml.Name{Local: http.CanonicalHeaderKey(k)},
|
|
|
|
Value: v,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return enc.EncodeElement(blobMetadataEntries{
|
|
|
|
Entries: entries,
|
|
|
|
}, start)
|
2015-02-05 00:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlobProperties contains various properties of a blob
|
|
|
|
// returned in various endpoints like ListBlobs or GetBlobProperties.
|
|
|
|
type BlobProperties struct {
|
2018-05-21 19:05:11 +00:00
|
|
|
LastModified TimeRFC1123 `xml:"Last-Modified"`
|
|
|
|
Etag string `xml:"Etag"`
|
|
|
|
ContentMD5 string `xml:"Content-MD5" header:"x-ms-blob-content-md5"`
|
|
|
|
ContentLength int64 `xml:"Content-Length"`
|
|
|
|
ContentType string `xml:"Content-Type" header:"x-ms-blob-content-type"`
|
|
|
|
ContentEncoding string `xml:"Content-Encoding" header:"x-ms-blob-content-encoding"`
|
|
|
|
CacheControl string `xml:"Cache-Control" header:"x-ms-blob-cache-control"`
|
|
|
|
ContentLanguage string `xml:"Cache-Language" header:"x-ms-blob-content-language"`
|
|
|
|
ContentDisposition string `xml:"Content-Disposition" header:"x-ms-blob-content-disposition"`
|
|
|
|
BlobType BlobType `xml:"BlobType"`
|
|
|
|
SequenceNumber int64 `xml:"x-ms-blob-sequence-number"`
|
|
|
|
CopyID string `xml:"CopyId"`
|
|
|
|
CopyStatus string `xml:"CopyStatus"`
|
|
|
|
CopySource string `xml:"CopySource"`
|
|
|
|
CopyProgress string `xml:"CopyProgress"`
|
|
|
|
CopyCompletionTime TimeRFC1123 `xml:"CopyCompletionTime"`
|
|
|
|
CopyStatusDescription string `xml:"CopyStatusDescription"`
|
|
|
|
LeaseStatus string `xml:"LeaseStatus"`
|
|
|
|
LeaseState string `xml:"LeaseState"`
|
|
|
|
LeaseDuration string `xml:"LeaseDuration"`
|
|
|
|
ServerEncrypted bool `xml:"ServerEncrypted"`
|
|
|
|
IncrementalCopy bool `xml:"IncrementalCopy"`
|
2015-02-05 00:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlobType defines the type of the Azure Blob.
|
|
|
|
type BlobType string
|
|
|
|
|
2015-06-11 22:30:18 +00:00
|
|
|
// Types of page blobs
|
2015-02-05 00:37:43 +00:00
|
|
|
const (
|
2016-02-08 22:29:21 +00:00
|
|
|
BlobTypeBlock BlobType = "BlockBlob"
|
|
|
|
BlobTypePage BlobType = "PageBlob"
|
|
|
|
BlobTypeAppend BlobType = "AppendBlob"
|
2015-02-05 00:37:43 +00:00
|
|
|
)
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
func (b *Blob) buildPath() string {
|
|
|
|
return b.Container.buildPath() + "/" + b.Name
|
2015-02-05 00:37:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// Exists returns true if a blob with given name exists on the specified
|
2015-06-11 22:30:18 +00:00
|
|
|
// container of the storage account.
|
2018-05-21 19:05:11 +00:00
|
|
|
func (b *Blob) Exists() (bool, error) {
|
|
|
|
uri := b.Container.bsc.client.getEndpoint(blobServiceName, b.buildPath(), nil)
|
|
|
|
headers := b.Container.bsc.client.getStandardHeaders()
|
|
|
|
resp, err := b.Container.bsc.client.exec(http.MethodHead, uri, headers, nil, b.Container.bsc.auth)
|
2015-06-11 22:30:18 +00:00
|
|
|
if resp != nil {
|
2018-05-21 19:05:11 +00:00
|
|
|
defer drainRespBody(resp)
|
|
|
|
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusNotFound {
|
|
|
|
return resp.StatusCode == http.StatusOK, nil
|
2015-06-11 22:30:18 +00:00
|
|
|
}
|
2015-02-05 00:37:43 +00:00
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// GetURL gets the canonical URL to the blob with the specified name in the
|
|
|
|
// specified container.
|
2017-04-14 01:05:38 +00:00
|
|
|
// This method does not create a publicly accessible URL if the blob or container
|
|
|
|
// is private and this method does not check if the blob exists.
|
2018-05-21 19:05:11 +00:00
|
|
|
func (b *Blob) GetURL() string {
|
|
|
|
container := b.Container.Name
|
2015-02-05 00:37:43 +00:00
|
|
|
if container == "" {
|
|
|
|
container = "$root"
|
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
return b.Container.bsc.client.getEndpoint(blobServiceName, pathForResource(container, b.Name), nil)
|
2015-02-05 00:37:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// GetBlobRangeOptions includes the options for a get blob range operation
|
|
|
|
type GetBlobRangeOptions struct {
|
|
|
|
Range *BlobRange
|
|
|
|
GetRangeContentMD5 bool
|
|
|
|
*GetBlobOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlobOptions includes the options for a get blob operation
|
|
|
|
type GetBlobOptions struct {
|
|
|
|
Timeout uint
|
|
|
|
Snapshot *time.Time
|
|
|
|
LeaseID string `header:"x-ms-lease-id"`
|
|
|
|
Origin string `header:"Origin"`
|
|
|
|
IfModifiedSince *time.Time `header:"If-Modified-Since"`
|
|
|
|
IfUnmodifiedSince *time.Time `header:"If-Unmodified-Since"`
|
|
|
|
IfMatch string `header:"If-Match"`
|
|
|
|
IfNoneMatch string `header:"If-None-Match"`
|
|
|
|
RequestID string `header:"x-ms-client-request-id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlobRange represents the bytes range to be get
|
|
|
|
type BlobRange struct {
|
|
|
|
Start uint64
|
|
|
|
End uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br BlobRange) String() string {
|
|
|
|
if br.End == 0 {
|
|
|
|
return fmt.Sprintf("bytes=%d-", br.Start)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("bytes=%d-%d", br.Start, br.End)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns a stream to read the blob. Caller must call both Read and Close()
|
|
|
|
// to correctly close the underlying connection.
|
|
|
|
//
|
|
|
|
// See the GetRange method for use with a Range header.
|
2015-06-11 22:30:18 +00:00
|
|
|
//
|
2018-05-21 19:05:11 +00:00
|
|
|
// See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Get-Blob
|
|
|
|
func (b *Blob) Get(options *GetBlobOptions) (io.ReadCloser, error) {
|
|
|
|
rangeOptions := GetBlobRangeOptions{
|
|
|
|
GetBlobOptions: options,
|
|
|
|
}
|
|
|
|
resp, err := b.getRange(&rangeOptions)
|
2015-02-05 00:37:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
if err := checkRespCode(resp, []int{http.StatusOK}); err != nil {
|
2015-06-11 22:30:18 +00:00
|
|
|
return nil, err
|
2015-02-05 00:37:43 +00:00
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
if err := b.writeProperties(resp.Header, true); err != nil {
|
|
|
|
return resp.Body, err
|
|
|
|
}
|
|
|
|
return resp.Body, nil
|
2015-02-05 00:37:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// GetRange reads the specified range of a blob to a stream. The bytesRange
|
2015-06-11 22:30:18 +00:00
|
|
|
// string must be in a format like "0-", "10-100" as defined in HTTP 1.1 spec.
|
2018-05-21 19:05:11 +00:00
|
|
|
// Caller must call both Read and Close()// to correctly close the underlying
|
|
|
|
// connection.
|
|
|
|
// See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Get-Blob
|
|
|
|
func (b *Blob) GetRange(options *GetBlobRangeOptions) (io.ReadCloser, error) {
|
|
|
|
resp, err := b.getRange(options)
|
2015-02-05 00:37:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
if err := checkRespCode(resp, []int{http.StatusPartialContent}); err != nil {
|
2015-06-11 22:30:18 +00:00
|
|
|
return nil, err
|
2015-02-05 00:37:43 +00:00
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
// Content-Length header should not be updated, as the service returns the range length
|
|
|
|
// (which is not alwys the full blob length)
|
|
|
|
if err := b.writeProperties(resp.Header, false); err != nil {
|
|
|
|
return resp.Body, err
|
|
|
|
}
|
|
|
|
return resp.Body, nil
|
2015-02-05 00:37:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
func (b *Blob) getRange(options *GetBlobRangeOptions) (*http.Response, error) {
|
|
|
|
params := url.Values{}
|
|
|
|
headers := b.Container.bsc.client.getStandardHeaders()
|
2015-02-05 00:37:43 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
if options != nil {
|
|
|
|
if options.Range != nil {
|
|
|
|
headers["Range"] = options.Range.String()
|
|
|
|
if options.GetRangeContentMD5 {
|
|
|
|
headers["x-ms-range-get-content-md5"] = "true"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if options.GetBlobOptions != nil {
|
|
|
|
headers = mergeHeaders(headers, headersFromStruct(*options.GetBlobOptions))
|
|
|
|
params = addTimeout(params, options.Timeout)
|
|
|
|
params = addSnapshot(params, options.Snapshot)
|
|
|
|
}
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
uri := b.Container.bsc.client.getEndpoint(blobServiceName, b.buildPath(), params)
|
2016-10-28 22:46:05 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
resp, err := b.Container.bsc.client.exec(http.MethodGet, uri, headers, nil, b.Container.bsc.auth)
|
2015-02-05 00:37:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// SnapshotOptions includes the options for a snapshot blob operation
|
|
|
|
type SnapshotOptions struct {
|
|
|
|
Timeout uint
|
|
|
|
LeaseID string `header:"x-ms-lease-id"`
|
|
|
|
IfModifiedSince *time.Time `header:"If-Modified-Since"`
|
|
|
|
IfUnmodifiedSince *time.Time `header:"If-Unmodified-Since"`
|
|
|
|
IfMatch string `header:"If-Match"`
|
|
|
|
IfNoneMatch string `header:"If-None-Match"`
|
|
|
|
RequestID string `header:"x-ms-client-request-id"`
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// CreateSnapshot creates a snapshot for a blob
|
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/ee691971.aspx
|
|
|
|
func (b *Blob) CreateSnapshot(options *SnapshotOptions) (snapshotTimestamp *time.Time, err error) {
|
2016-12-13 05:12:39 +00:00
|
|
|
params := url.Values{"comp": {"snapshot"}}
|
2018-05-21 19:05:11 +00:00
|
|
|
headers := b.Container.bsc.client.getStandardHeaders()
|
|
|
|
headers = b.Container.bsc.client.addMetadataToHeaders(headers, b.Metadata)
|
2016-12-13 05:12:39 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
if options != nil {
|
|
|
|
params = addTimeout(params, options.Timeout)
|
|
|
|
headers = mergeHeaders(headers, headersFromStruct(*options))
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
uri := b.Container.bsc.client.getEndpoint(blobServiceName, b.buildPath(), params)
|
2016-12-13 05:12:39 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
resp, err := b.Container.bsc.client.exec(http.MethodPut, uri, headers, nil, b.Container.bsc.auth)
|
2017-04-14 01:05:38 +00:00
|
|
|
if err != nil || resp == nil {
|
2016-12-13 05:12:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
defer drainRespBody(resp)
|
2016-12-13 05:12:39 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
if err := checkRespCode(resp, []int{http.StatusCreated}); err != nil {
|
2016-12-13 05:12:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
snapshotResponse := resp.Header.Get(http.CanonicalHeaderKey("x-ms-snapshot"))
|
2016-12-13 05:12:39 +00:00
|
|
|
if snapshotResponse != "" {
|
|
|
|
snapshotTimestamp, err := time.Parse(time.RFC3339, snapshotResponse)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &snapshotTimestamp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("Snapshot not created")
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// GetBlobPropertiesOptions includes the options for a get blob properties operation
|
|
|
|
type GetBlobPropertiesOptions struct {
|
|
|
|
Timeout uint
|
|
|
|
Snapshot *time.Time
|
|
|
|
LeaseID string `header:"x-ms-lease-id"`
|
|
|
|
IfModifiedSince *time.Time `header:"If-Modified-Since"`
|
|
|
|
IfUnmodifiedSince *time.Time `header:"If-Unmodified-Since"`
|
|
|
|
IfMatch string `header:"If-Match"`
|
|
|
|
IfNoneMatch string `header:"If-None-Match"`
|
|
|
|
RequestID string `header:"x-ms-client-request-id"`
|
|
|
|
}
|
2016-12-13 05:12:39 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// GetProperties provides various information about the specified blob.
|
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/dd179394.aspx
|
|
|
|
func (b *Blob) GetProperties(options *GetBlobPropertiesOptions) error {
|
|
|
|
params := url.Values{}
|
|
|
|
headers := b.Container.bsc.client.getStandardHeaders()
|
2017-04-14 01:05:38 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
if options != nil {
|
|
|
|
params = addTimeout(params, options.Timeout)
|
|
|
|
params = addSnapshot(params, options.Snapshot)
|
|
|
|
headers = mergeHeaders(headers, headersFromStruct(*options))
|
2016-12-13 05:12:39 +00:00
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
uri := b.Container.bsc.client.getEndpoint(blobServiceName, b.buildPath(), params)
|
2016-10-28 22:46:05 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
resp, err := b.Container.bsc.client.exec(http.MethodHead, uri, headers, nil, b.Container.bsc.auth)
|
2016-10-28 22:46:05 +00:00
|
|
|
if err != nil {
|
2018-05-21 19:05:11 +00:00
|
|
|
return err
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
defer drainRespBody(resp)
|
2016-10-28 22:46:05 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
if err = checkRespCode(resp, []int{http.StatusOK}); err != nil {
|
|
|
|
return err
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
return b.writeProperties(resp.Header, true)
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
func (b *Blob) writeProperties(h http.Header, includeContentLen bool) error {
|
|
|
|
var err error
|
2016-10-28 22:46:05 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
contentLength := b.Properties.ContentLength
|
|
|
|
if includeContentLen {
|
|
|
|
contentLengthStr := h.Get("Content-Length")
|
|
|
|
if contentLengthStr != "" {
|
|
|
|
contentLength, err = strconv.ParseInt(contentLengthStr, 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
var sequenceNum int64
|
|
|
|
sequenceNumStr := h.Get("x-ms-blob-sequence-number")
|
|
|
|
if sequenceNumStr != "" {
|
|
|
|
sequenceNum, err = strconv.ParseInt(sequenceNumStr, 0, 64)
|
2016-10-28 22:46:05 +00:00
|
|
|
if err != nil {
|
2018-05-21 19:05:11 +00:00
|
|
|
return err
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
lastModified, err := getTimeFromHeaders(h, "Last-Modified")
|
2016-10-28 22:46:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
copyCompletionTime, err := getTimeFromHeaders(h, "x-ms-copy-completion-time")
|
2016-10-28 22:46:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
b.Properties = BlobProperties{
|
|
|
|
LastModified: TimeRFC1123(*lastModified),
|
|
|
|
Etag: h.Get("Etag"),
|
|
|
|
ContentMD5: h.Get("Content-MD5"),
|
|
|
|
ContentLength: contentLength,
|
|
|
|
ContentEncoding: h.Get("Content-Encoding"),
|
|
|
|
ContentType: h.Get("Content-Type"),
|
|
|
|
ContentDisposition: h.Get("Content-Disposition"),
|
|
|
|
CacheControl: h.Get("Cache-Control"),
|
|
|
|
ContentLanguage: h.Get("Content-Language"),
|
|
|
|
SequenceNumber: sequenceNum,
|
|
|
|
CopyCompletionTime: TimeRFC1123(*copyCompletionTime),
|
|
|
|
CopyStatusDescription: h.Get("x-ms-copy-status-description"),
|
|
|
|
CopyID: h.Get("x-ms-copy-id"),
|
|
|
|
CopyProgress: h.Get("x-ms-copy-progress"),
|
|
|
|
CopySource: h.Get("x-ms-copy-source"),
|
|
|
|
CopyStatus: h.Get("x-ms-copy-status"),
|
|
|
|
BlobType: BlobType(h.Get("x-ms-blob-type")),
|
|
|
|
LeaseStatus: h.Get("x-ms-lease-status"),
|
|
|
|
LeaseState: h.Get("x-ms-lease-state"),
|
|
|
|
}
|
|
|
|
b.writeMetadata(h)
|
2016-10-28 22:46:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// SetBlobPropertiesOptions contains various properties of a blob and is an entry
|
|
|
|
// in SetProperties
|
|
|
|
type SetBlobPropertiesOptions struct {
|
|
|
|
Timeout uint
|
|
|
|
LeaseID string `header:"x-ms-lease-id"`
|
|
|
|
Origin string `header:"Origin"`
|
|
|
|
IfModifiedSince *time.Time `header:"If-Modified-Since"`
|
|
|
|
IfUnmodifiedSince *time.Time `header:"If-Unmodified-Since"`
|
|
|
|
IfMatch string `header:"If-Match"`
|
|
|
|
IfNoneMatch string `header:"If-None-Match"`
|
|
|
|
SequenceNumberAction *SequenceNumberAction
|
|
|
|
RequestID string `header:"x-ms-client-request-id"`
|
|
|
|
}
|
2015-02-05 00:37:43 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// SequenceNumberAction defines how the blob's sequence number should be modified
|
|
|
|
type SequenceNumberAction string
|
2015-02-05 00:37:43 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// Options for sequence number action
|
|
|
|
const (
|
|
|
|
SequenceNumberActionMax SequenceNumberAction = "max"
|
|
|
|
SequenceNumberActionUpdate SequenceNumberAction = "update"
|
|
|
|
SequenceNumberActionIncrement SequenceNumberAction = "increment"
|
|
|
|
)
|
2015-02-05 00:37:43 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// SetProperties replaces the BlobHeaders for the specified blob.
|
2016-10-28 22:46:05 +00:00
|
|
|
//
|
|
|
|
// Some keys may be converted to Camel-Case before sending. All keys
|
|
|
|
// are returned in lower case by GetBlobProperties. HTTP header names
|
|
|
|
// are case-insensitive so case munging should not matter to other
|
|
|
|
// applications either.
|
|
|
|
//
|
2018-05-21 19:05:11 +00:00
|
|
|
// See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Set-Blob-Properties
|
|
|
|
func (b *Blob) SetProperties(options *SetBlobPropertiesOptions) error {
|
2016-10-28 22:46:05 +00:00
|
|
|
params := url.Values{"comp": {"properties"}}
|
2018-05-21 19:05:11 +00:00
|
|
|
headers := b.Container.bsc.client.getStandardHeaders()
|
|
|
|
headers = mergeHeaders(headers, headersFromStruct(b.Properties))
|
|
|
|
|
|
|
|
if options != nil {
|
|
|
|
params = addTimeout(params, options.Timeout)
|
|
|
|
headers = mergeHeaders(headers, headersFromStruct(*options))
|
|
|
|
}
|
|
|
|
uri := b.Container.bsc.client.getEndpoint(blobServiceName, b.buildPath(), params)
|
|
|
|
|
|
|
|
if b.Properties.BlobType == BlobTypePage {
|
|
|
|
headers = addToHeaders(headers, "x-ms-blob-content-length", fmt.Sprintf("%v", b.Properties.ContentLength))
|
|
|
|
if options != nil && options.SequenceNumberAction != nil {
|
|
|
|
headers = addToHeaders(headers, "x-ms-sequence-number-action", string(*options.SequenceNumberAction))
|
|
|
|
if *options.SequenceNumberAction != SequenceNumberActionIncrement {
|
|
|
|
headers = addToHeaders(headers, "x-ms-blob-sequence-number", fmt.Sprintf("%v", b.Properties.SequenceNumber))
|
|
|
|
}
|
|
|
|
}
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
resp, err := b.Container.bsc.client.exec(http.MethodPut, uri, headers, nil, b.Container.bsc.auth)
|
2016-10-28 22:46:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
defer drainRespBody(resp)
|
|
|
|
return checkRespCode(resp, []int{http.StatusOK})
|
|
|
|
}
|
2016-10-28 22:46:05 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// SetBlobMetadataOptions includes the options for a set blob metadata operation
|
|
|
|
type SetBlobMetadataOptions struct {
|
|
|
|
Timeout uint
|
|
|
|
LeaseID string `header:"x-ms-lease-id"`
|
|
|
|
IfModifiedSince *time.Time `header:"If-Modified-Since"`
|
|
|
|
IfUnmodifiedSince *time.Time `header:"If-Unmodified-Since"`
|
|
|
|
IfMatch string `header:"If-Match"`
|
|
|
|
IfNoneMatch string `header:"If-None-Match"`
|
|
|
|
RequestID string `header:"x-ms-client-request-id"`
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// SetMetadata replaces the metadata for the specified blob.
|
2016-02-08 22:29:21 +00:00
|
|
|
//
|
|
|
|
// Some keys may be converted to Camel-Case before sending. All keys
|
|
|
|
// are returned in lower case by GetBlobMetadata. HTTP header names
|
|
|
|
// are case-insensitive so case munging should not matter to other
|
|
|
|
// applications either.
|
|
|
|
//
|
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx
|
2018-05-21 19:05:11 +00:00
|
|
|
func (b *Blob) SetMetadata(options *SetBlobMetadataOptions) error {
|
2016-02-08 22:29:21 +00:00
|
|
|
params := url.Values{"comp": {"metadata"}}
|
2018-05-21 19:05:11 +00:00
|
|
|
headers := b.Container.bsc.client.getStandardHeaders()
|
|
|
|
headers = b.Container.bsc.client.addMetadataToHeaders(headers, b.Metadata)
|
2016-02-08 22:29:21 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
if options != nil {
|
|
|
|
params = addTimeout(params, options.Timeout)
|
|
|
|
headers = mergeHeaders(headers, headersFromStruct(*options))
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
uri := b.Container.bsc.client.getEndpoint(blobServiceName, b.buildPath(), params)
|
2016-10-28 22:46:05 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
resp, err := b.Container.bsc.client.exec(http.MethodPut, uri, headers, nil, b.Container.bsc.auth)
|
2016-02-08 22:29:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
defer drainRespBody(resp)
|
|
|
|
return checkRespCode(resp, []int{http.StatusOK})
|
|
|
|
}
|
2016-02-08 22:29:21 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// GetBlobMetadataOptions includes the options for a get blob metadata operation
|
|
|
|
type GetBlobMetadataOptions struct {
|
|
|
|
Timeout uint
|
|
|
|
Snapshot *time.Time
|
|
|
|
LeaseID string `header:"x-ms-lease-id"`
|
|
|
|
IfModifiedSince *time.Time `header:"If-Modified-Since"`
|
|
|
|
IfUnmodifiedSince *time.Time `header:"If-Unmodified-Since"`
|
|
|
|
IfMatch string `header:"If-Match"`
|
|
|
|
IfNoneMatch string `header:"If-None-Match"`
|
|
|
|
RequestID string `header:"x-ms-client-request-id"`
|
2016-02-08 22:29:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// GetMetadata returns all user-defined metadata for the specified blob.
|
2016-02-08 22:29:21 +00:00
|
|
|
//
|
|
|
|
// All metadata keys will be returned in lower case. (HTTP header
|
|
|
|
// names are case-insensitive.)
|
|
|
|
//
|
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx
|
2018-05-21 19:05:11 +00:00
|
|
|
func (b *Blob) GetMetadata(options *GetBlobMetadataOptions) error {
|
2016-02-08 22:29:21 +00:00
|
|
|
params := url.Values{"comp": {"metadata"}}
|
2018-05-21 19:05:11 +00:00
|
|
|
headers := b.Container.bsc.client.getStandardHeaders()
|
2016-02-08 22:29:21 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
if options != nil {
|
|
|
|
params = addTimeout(params, options.Timeout)
|
|
|
|
params = addSnapshot(params, options.Snapshot)
|
|
|
|
headers = mergeHeaders(headers, headersFromStruct(*options))
|
2016-02-08 22:29:21 +00:00
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
uri := b.Container.bsc.client.getEndpoint(blobServiceName, b.buildPath(), params)
|
2016-02-08 22:29:21 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
resp, err := b.Container.bsc.client.exec(http.MethodGet, uri, headers, nil, b.Container.bsc.auth)
|
2015-02-05 00:37:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
defer drainRespBody(resp)
|
2015-02-05 00:37:43 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
if err := checkRespCode(resp, []int{http.StatusOK}); err != nil {
|
2016-12-13 05:12:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
b.writeMetadata(resp.Header)
|
2016-12-13 05:12:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
func (b *Blob) writeMetadata(h http.Header) {
|
|
|
|
b.Metadata = BlobMetadata(writeMetadata(h))
|
|
|
|
}
|
2015-02-05 00:37:43 +00:00
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// DeleteBlobOptions includes the options for a delete blob operation
|
|
|
|
type DeleteBlobOptions struct {
|
|
|
|
Timeout uint
|
|
|
|
Snapshot *time.Time
|
|
|
|
LeaseID string `header:"x-ms-lease-id"`
|
|
|
|
DeleteSnapshots *bool
|
|
|
|
IfModifiedSince *time.Time `header:"If-Modified-Since"`
|
|
|
|
IfUnmodifiedSince *time.Time `header:"If-Unmodified-Since"`
|
|
|
|
IfMatch string `header:"If-Match"`
|
|
|
|
IfNoneMatch string `header:"If-None-Match"`
|
|
|
|
RequestID string `header:"x-ms-client-request-id"`
|
2015-02-05 00:37:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// Delete deletes the given blob from the specified container.
|
2015-02-05 00:37:43 +00:00
|
|
|
// If the blob does not exists at the time of the Delete Blob operation, it
|
2018-05-21 19:05:11 +00:00
|
|
|
// returns error.
|
|
|
|
// See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Delete-Blob
|
|
|
|
func (b *Blob) Delete(options *DeleteBlobOptions) error {
|
|
|
|
resp, err := b.delete(options)
|
2015-02-05 00:37:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
defer drainRespBody(resp)
|
|
|
|
return checkRespCode(resp, []int{http.StatusAccepted})
|
2015-02-05 00:37:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
// DeleteIfExists deletes the given blob from the specified container If the
|
2015-06-11 22:30:18 +00:00
|
|
|
// blob is deleted with this call, returns true. Otherwise returns false.
|
|
|
|
//
|
2018-05-21 19:05:11 +00:00
|
|
|
// See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Delete-Blob
|
|
|
|
func (b *Blob) DeleteIfExists(options *DeleteBlobOptions) (bool, error) {
|
|
|
|
resp, err := b.delete(options)
|
2016-10-28 22:46:05 +00:00
|
|
|
if resp != nil {
|
2018-05-21 19:05:11 +00:00
|
|
|
defer drainRespBody(resp)
|
|
|
|
if resp.StatusCode == http.StatusAccepted || resp.StatusCode == http.StatusNotFound {
|
|
|
|
return resp.StatusCode == http.StatusAccepted, nil
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
2015-02-05 00:37:43 +00:00
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
func (b *Blob) delete(options *DeleteBlobOptions) (*http.Response, error) {
|
|
|
|
params := url.Values{}
|
|
|
|
headers := b.Container.bsc.client.getStandardHeaders()
|
|
|
|
|
|
|
|
if options != nil {
|
|
|
|
params = addTimeout(params, options.Timeout)
|
|
|
|
params = addSnapshot(params, options.Snapshot)
|
|
|
|
headers = mergeHeaders(headers, headersFromStruct(*options))
|
|
|
|
if options.DeleteSnapshots != nil {
|
|
|
|
if *options.DeleteSnapshots {
|
|
|
|
headers["x-ms-delete-snapshots"] = "include"
|
|
|
|
} else {
|
|
|
|
headers["x-ms-delete-snapshots"] = "only"
|
|
|
|
}
|
|
|
|
}
|
2016-10-28 22:46:05 +00:00
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
uri := b.Container.bsc.client.getEndpoint(blobServiceName, b.buildPath(), params)
|
|
|
|
return b.Container.bsc.client.exec(http.MethodDelete, uri, headers, nil, b.Container.bsc.auth)
|
2015-02-05 00:37:43 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 01:05:38 +00:00
|
|
|
// helper method to construct the path to either a blob or container
|
|
|
|
func pathForResource(container, name string) string {
|
2018-05-21 19:05:11 +00:00
|
|
|
if name != "" {
|
2017-04-14 01:05:38 +00:00
|
|
|
return fmt.Sprintf("/%s/%s", container, name)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("/%s", container)
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:05:11 +00:00
|
|
|
func (b *Blob) respondCreation(resp *http.Response, bt BlobType) error {
|
|
|
|
defer drainRespBody(resp)
|
|
|
|
err := checkRespCode(resp, []int{http.StatusCreated})
|
2016-10-28 22:46:05 +00:00
|
|
|
if err != nil {
|
2018-05-21 19:05:11 +00:00
|
|
|
return err
|
2015-02-05 00:37:43 +00:00
|
|
|
}
|
2018-05-21 19:05:11 +00:00
|
|
|
b.Properties.BlobType = bt
|
|
|
|
return nil
|
2015-02-05 00:37:43 +00:00
|
|
|
}
|