2014-11-25 19:40:24 +01:00
|
|
|
// Package azure provides a storagedriver.StorageDriver implementation to
|
|
|
|
// store blobs in Microsoft Azure Blob Storage Service.
|
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
2016-02-08 23:29:21 +01:00
|
|
|
"bufio"
|
2014-11-25 19:40:24 +01:00
|
|
|
"bytes"
|
2017-08-12 00:31:16 +02:00
|
|
|
"context"
|
2014-11-25 19:40:24 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-01-16 19:18:42 +01:00
|
|
|
"net/http"
|
2014-11-25 19:40:24 +01:00
|
|
|
"strings"
|
2015-01-16 19:18:42 +01:00
|
|
|
"time"
|
2014-11-25 19:40:24 +01:00
|
|
|
|
2020-08-24 13:18:39 +02:00
|
|
|
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver/base"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver/factory"
|
2014-11-25 19:40:24 +01:00
|
|
|
|
2015-06-12 00:30:18 +02:00
|
|
|
azure "github.com/Azure/azure-sdk-for-go/storage"
|
2014-11-25 19:40:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const driverName = "azure"
|
|
|
|
|
|
|
|
const (
|
|
|
|
paramAccountName = "accountname"
|
|
|
|
paramAccountKey = "accountkey"
|
|
|
|
paramContainer = "container"
|
2015-03-24 02:20:06 +01:00
|
|
|
paramRealm = "realm"
|
2016-02-08 23:29:21 +01:00
|
|
|
maxChunkSize = 4 * 1024 * 1024
|
2014-11-25 19:40:24 +01:00
|
|
|
)
|
|
|
|
|
2015-02-04 08:21:39 +01:00
|
|
|
type driver struct {
|
2015-02-04 00:29:00 +01:00
|
|
|
client azure.BlobStorageClient
|
2014-11-25 19:40:24 +01:00
|
|
|
container string
|
|
|
|
}
|
|
|
|
|
2015-02-04 08:21:39 +01:00
|
|
|
type baseEmbed struct{ base.Base }
|
|
|
|
|
|
|
|
// Driver is a storagedriver.StorageDriver implementation backed by
|
|
|
|
// Microsoft Azure Blob Storage Service.
|
|
|
|
type Driver struct{ baseEmbed }
|
|
|
|
|
2014-11-25 19:40:24 +01:00
|
|
|
func init() {
|
|
|
|
factory.Register(driverName, &azureDriverFactory{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type azureDriverFactory struct{}
|
|
|
|
|
2015-01-16 19:18:42 +01:00
|
|
|
func (factory *azureDriverFactory) Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
|
2014-11-25 19:40:24 +01:00
|
|
|
return FromParameters(parameters)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromParameters constructs a new Driver with a given parameters map.
|
2015-01-16 19:18:42 +01:00
|
|
|
func FromParameters(parameters map[string]interface{}) (*Driver, error) {
|
2014-11-25 19:40:24 +01:00
|
|
|
accountName, ok := parameters[paramAccountName]
|
2015-01-16 19:18:42 +01:00
|
|
|
if !ok || fmt.Sprint(accountName) == "" {
|
2019-02-05 01:01:04 +01:00
|
|
|
return nil, fmt.Errorf("no %s parameter provided", paramAccountName)
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
accountKey, ok := parameters[paramAccountKey]
|
2015-01-16 19:18:42 +01:00
|
|
|
if !ok || fmt.Sprint(accountKey) == "" {
|
2019-02-05 01:01:04 +01:00
|
|
|
return nil, fmt.Errorf("no %s parameter provided", paramAccountKey)
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
container, ok := parameters[paramContainer]
|
2015-01-16 19:18:42 +01:00
|
|
|
if !ok || fmt.Sprint(container) == "" {
|
2019-02-05 01:01:04 +01:00
|
|
|
return nil, fmt.Errorf("no %s parameter provided", paramContainer)
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
2015-03-24 02:20:06 +01:00
|
|
|
realm, ok := parameters[paramRealm]
|
|
|
|
if !ok || fmt.Sprint(realm) == "" {
|
2015-06-12 00:30:18 +02:00
|
|
|
realm = azure.DefaultBaseURL
|
2015-03-24 02:20:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return New(fmt.Sprint(accountName), fmt.Sprint(accountKey), fmt.Sprint(container), fmt.Sprint(realm))
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// New constructs a new Driver with the given Azure Storage Account credentials
|
2015-03-24 02:20:06 +01:00
|
|
|
func New(accountName, accountKey, container, realm string) (*Driver, error) {
|
2015-06-12 00:30:18 +02:00
|
|
|
api, err := azure.NewClient(accountName, accountKey, realm, azure.DefaultAPIVersion, true)
|
2014-11-25 19:40:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blobClient := api.GetBlobService()
|
|
|
|
|
|
|
|
// Create registry container
|
2017-04-14 03:05:38 +02:00
|
|
|
containerRef := blobClient.GetContainerReference(container)
|
2018-05-21 21:05:11 +02:00
|
|
|
if _, err = containerRef.CreateIfNotExists(nil); err != nil {
|
2014-11-25 19:40:24 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-02-04 08:21:39 +01:00
|
|
|
d := &driver{
|
2015-06-12 00:30:18 +02:00
|
|
|
client: blobClient,
|
2015-02-04 08:21:39 +01:00
|
|
|
container: container}
|
|
|
|
return &Driver{baseEmbed: baseEmbed{Base: base.Base{StorageDriver: d}}}, nil
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implement the storagedriver.StorageDriver interface.
|
2015-04-23 02:30:01 +02:00
|
|
|
func (d *driver) Name() string {
|
|
|
|
return driverName
|
|
|
|
}
|
2014-11-25 19:40:24 +01:00
|
|
|
|
|
|
|
// GetContent retrieves the content stored at "path" as a []byte.
|
2015-04-28 00:58:58 +02:00
|
|
|
func (d *driver) GetContent(ctx context.Context, path string) ([]byte, error) {
|
2018-05-21 21:05:11 +02:00
|
|
|
blobRef := d.client.GetContainerReference(d.container).GetBlobReference(path)
|
|
|
|
blob, err := blobRef.Get(nil)
|
2014-11-25 19:40:24 +01:00
|
|
|
if err != nil {
|
|
|
|
if is404(err) {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-29 00:13:22 +02:00
|
|
|
defer blob.Close()
|
2014-11-25 19:40:24 +01:00
|
|
|
return ioutil.ReadAll(blob)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutContent stores the []byte content at a location designated by "path".
|
2015-04-28 00:58:58 +02:00
|
|
|
func (d *driver) PutContent(ctx context.Context, path string, contents []byte) error {
|
2018-05-21 21:05:11 +02:00
|
|
|
// max size for block blobs uploaded via single "Put Blob" for version after "2016-05-31"
|
|
|
|
// https://docs.microsoft.com/en-us/rest/api/storageservices/put-blob#remarks
|
|
|
|
const limit = 256 * 1024 * 1024
|
|
|
|
if len(contents) > limit {
|
2016-11-30 21:24:54 +01:00
|
|
|
return fmt.Errorf("uploading %d bytes with PutContent is not supported; limit: %d bytes", len(contents), limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Historically, blobs uploaded via PutContent used to be of type AppendBlob
|
2020-08-24 13:18:39 +02:00
|
|
|
// (https://github.com/distribution/distribution/pull/1438). We can't replace
|
2016-11-30 21:24:54 +01:00
|
|
|
// these blobs atomically via a single "Put Blob" operation without
|
|
|
|
// deleting them first. Once we detect they are BlockBlob type, we can
|
|
|
|
// overwrite them with an atomically "Put Blob" operation.
|
|
|
|
//
|
|
|
|
// While we delete the blob and create a new one, there will be a small
|
|
|
|
// window of inconsistency and if the Put Blob fails, we may end up with
|
|
|
|
// losing the existing data while migrating it to BlockBlob type. However,
|
|
|
|
// expectation is the clients pushing will be retrying when they get an error
|
|
|
|
// response.
|
2018-05-21 21:05:11 +02:00
|
|
|
blobRef := d.client.GetContainerReference(d.container).GetBlobReference(path)
|
|
|
|
err := blobRef.GetProperties(nil)
|
2016-11-30 21:24:54 +01:00
|
|
|
if err != nil && !is404(err) {
|
|
|
|
return fmt.Errorf("failed to get blob properties: %v", err)
|
|
|
|
}
|
2018-05-21 21:05:11 +02:00
|
|
|
if err == nil && blobRef.Properties.BlobType != azure.BlobTypeBlock {
|
|
|
|
if err := blobRef.Delete(nil); err != nil {
|
|
|
|
return fmt.Errorf("failed to delete legacy blob (%s): %v", blobRef.Properties.BlobType, err)
|
2016-11-30 21:24:54 +01:00
|
|
|
}
|
2015-06-12 00:30:18 +02:00
|
|
|
}
|
2016-11-30 21:24:54 +01:00
|
|
|
|
|
|
|
r := bytes.NewReader(contents)
|
2018-05-21 21:05:11 +02:00
|
|
|
// reset properties to empty before doing overwrite
|
|
|
|
blobRef.Properties = azure.BlobProperties{}
|
|
|
|
return blobRef.CreateBlockBlobFromReader(r, nil)
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
2016-02-08 23:29:21 +01:00
|
|
|
// Reader retrieves an io.ReadCloser for the content stored at "path" with a
|
2014-11-25 19:40:24 +01:00
|
|
|
// given byte offset.
|
2016-02-08 23:29:21 +01:00
|
|
|
func (d *driver) Reader(ctx context.Context, path string, offset int64) (io.ReadCloser, error) {
|
2018-05-21 21:05:11 +02:00
|
|
|
blobRef := d.client.GetContainerReference(d.container).GetBlobReference(path)
|
|
|
|
if ok, err := blobRef.Exists(); err != nil {
|
2014-11-25 19:40:24 +01:00
|
|
|
return nil, err
|
|
|
|
} else if !ok {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
|
2018-05-21 21:05:11 +02:00
|
|
|
err := blobRef.GetProperties(nil)
|
2014-11-25 19:40:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-21 21:05:11 +02:00
|
|
|
info := blobRef.Properties
|
2018-08-06 23:34:15 +02:00
|
|
|
size := info.ContentLength
|
2015-01-16 19:18:42 +01:00
|
|
|
if offset >= size {
|
|
|
|
return ioutil.NopCloser(bytes.NewReader(nil)), nil
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
2018-05-21 21:05:11 +02:00
|
|
|
resp, err := blobRef.GetRange(&azure.GetBlobRangeOptions{
|
|
|
|
Range: &azure.BlobRange{
|
|
|
|
Start: uint64(offset),
|
|
|
|
End: 0,
|
|
|
|
},
|
|
|
|
})
|
2014-11-25 19:40:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2016-02-08 23:29:21 +01:00
|
|
|
// Writer returns a FileWriter which will store the content written to it
|
|
|
|
// at the location designated by "path" after the call to Commit.
|
|
|
|
func (d *driver) Writer(ctx context.Context, path string, append bool) (storagedriver.FileWriter, error) {
|
2018-05-21 21:05:11 +02:00
|
|
|
blobRef := d.client.GetContainerReference(d.container).GetBlobReference(path)
|
|
|
|
blobExists, err := blobRef.Exists()
|
2016-02-08 23:29:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var size int64
|
|
|
|
if blobExists {
|
|
|
|
if append {
|
2018-05-21 21:05:11 +02:00
|
|
|
err = blobRef.GetProperties(nil)
|
2016-02-08 23:29:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-21 21:05:11 +02:00
|
|
|
blobProperties := blobRef.Properties
|
2016-02-08 23:29:21 +01:00
|
|
|
size = blobProperties.ContentLength
|
|
|
|
} else {
|
2018-05-21 21:05:11 +02:00
|
|
|
err = blobRef.Delete(nil)
|
2016-02-08 23:29:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if append {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
2018-05-21 21:05:11 +02:00
|
|
|
err = blobRef.PutAppendBlob(nil)
|
2015-02-04 00:29:00 +01:00
|
|
|
if err != nil {
|
2016-02-08 23:29:21 +01:00
|
|
|
return nil, err
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 23:29:21 +01:00
|
|
|
return d.newWriter(path, size), nil
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
2015-01-16 19:18:42 +01:00
|
|
|
// Stat retrieves the FileInfo for the given path, including the current size
|
|
|
|
// in bytes and the creation time.
|
2015-04-28 00:58:58 +02:00
|
|
|
func (d *driver) Stat(ctx context.Context, path string) (storagedriver.FileInfo, error) {
|
2018-05-21 21:05:11 +02:00
|
|
|
blobRef := d.client.GetContainerReference(d.container).GetBlobReference(path)
|
2015-01-16 19:18:42 +01:00
|
|
|
// Check if the path is a blob
|
2018-05-21 21:05:11 +02:00
|
|
|
if ok, err := blobRef.Exists(); err != nil {
|
2015-01-16 19:18:42 +01:00
|
|
|
return nil, err
|
|
|
|
} else if ok {
|
2018-05-21 21:05:11 +02:00
|
|
|
err = blobRef.GetProperties(nil)
|
2015-01-16 19:18:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-21 21:05:11 +02:00
|
|
|
blobProperties := blobRef.Properties
|
2015-01-16 19:18:42 +01:00
|
|
|
|
|
|
|
return storagedriver.FileInfoInternal{FileInfoFields: storagedriver.FileInfoFields{
|
|
|
|
Path: path,
|
2018-08-06 23:34:15 +02:00
|
|
|
Size: blobProperties.ContentLength,
|
2018-05-21 21:05:11 +02:00
|
|
|
ModTime: time.Time(blobProperties.LastModified),
|
2015-01-16 19:18:42 +01:00
|
|
|
IsDir: false,
|
|
|
|
}}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if path is a virtual container
|
|
|
|
virtContainerPath := path
|
|
|
|
if !strings.HasSuffix(virtContainerPath, "/") {
|
|
|
|
virtContainerPath += "/"
|
|
|
|
}
|
2017-04-14 03:05:38 +02:00
|
|
|
|
|
|
|
containerRef := d.client.GetContainerReference(d.container)
|
|
|
|
blobs, err := containerRef.ListBlobs(azure.ListBlobsParameters{
|
2015-01-16 19:18:42 +01:00
|
|
|
Prefix: virtContainerPath,
|
|
|
|
MaxResults: 1,
|
|
|
|
})
|
2014-11-25 19:40:24 +01:00
|
|
|
if err != nil {
|
2015-01-16 19:18:42 +01:00
|
|
|
return nil, err
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
2015-01-16 19:18:42 +01:00
|
|
|
if len(blobs.Blobs) > 0 {
|
|
|
|
// path is a virtual container
|
|
|
|
return storagedriver.FileInfoInternal{FileInfoFields: storagedriver.FileInfoFields{
|
|
|
|
Path: path,
|
|
|
|
IsDir: true,
|
|
|
|
}}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// path is not a blob or virtual container
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// List returns a list of the objects that are direct descendants of the given
|
|
|
|
// path.
|
2015-04-28 00:58:58 +02:00
|
|
|
func (d *driver) List(ctx context.Context, path string) ([]string, error) {
|
2014-11-25 19:40:24 +01:00
|
|
|
if path == "/" {
|
|
|
|
path = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
blobs, err := d.listBlobs(d.container, path)
|
|
|
|
if err != nil {
|
|
|
|
return blobs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
list := directDescendants(blobs, path)
|
2016-02-08 23:29:21 +01:00
|
|
|
if path != "" && len(list) == 0 {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
2014-11-25 19:40:24 +01:00
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move moves an object stored at sourcePath to destPath, removing the original
|
|
|
|
// object.
|
2015-04-28 00:58:58 +02:00
|
|
|
func (d *driver) Move(ctx context.Context, sourcePath string, destPath string) error {
|
2018-05-21 21:05:11 +02:00
|
|
|
srcBlobRef := d.client.GetContainerReference(d.container).GetBlobReference(sourcePath)
|
|
|
|
sourceBlobURL := srcBlobRef.GetURL()
|
|
|
|
destBlobRef := d.client.GetContainerReference(d.container).GetBlobReference(destPath)
|
|
|
|
err := destBlobRef.Copy(sourceBlobURL, nil)
|
2014-11-25 19:40:24 +01:00
|
|
|
if err != nil {
|
|
|
|
if is404(err) {
|
|
|
|
return storagedriver.PathNotFoundError{Path: sourcePath}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-21 21:05:11 +02:00
|
|
|
return srcBlobRef.Delete(nil)
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete recursively deletes all objects stored at "path" and its subpaths.
|
2015-04-28 00:58:58 +02:00
|
|
|
func (d *driver) Delete(ctx context.Context, path string) error {
|
2018-05-21 21:05:11 +02:00
|
|
|
blobRef := d.client.GetContainerReference(d.container).GetBlobReference(path)
|
|
|
|
ok, err := blobRef.DeleteIfExists(nil)
|
2014-11-25 19:40:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
return nil // was a blob and deleted, return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a blob, see if path is a virtual container with blobs
|
|
|
|
blobs, err := d.listBlobs(d.container, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, b := range blobs {
|
2018-05-21 21:05:11 +02:00
|
|
|
blobRef = d.client.GetContainerReference(d.container).GetBlobReference(b)
|
|
|
|
if err = blobRef.Delete(nil); err != nil {
|
2014-11-25 19:40:24 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(blobs) == 0 {
|
|
|
|
return storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-16 19:18:42 +01:00
|
|
|
// URLFor returns a publicly accessible URL for the blob stored at given path
|
|
|
|
// for specified duration by making use of Azure Storage Shared Access Signatures (SAS).
|
|
|
|
// See https://msdn.microsoft.com/en-us/library/azure/ee395415.aspx for more info.
|
2015-04-28 00:58:58 +02:00
|
|
|
func (d *driver) URLFor(ctx context.Context, path string, options map[string]interface{}) (string, error) {
|
2015-01-16 19:18:42 +01:00
|
|
|
expiresTime := time.Now().UTC().Add(20 * time.Minute) // default expiration
|
|
|
|
expires, ok := options["expiry"]
|
|
|
|
if ok {
|
|
|
|
t, ok := expires.(time.Time)
|
|
|
|
if ok {
|
|
|
|
expiresTime = t
|
|
|
|
}
|
|
|
|
}
|
2018-05-21 21:05:11 +02:00
|
|
|
blobRef := d.client.GetContainerReference(d.container).GetBlobReference(path)
|
|
|
|
return blobRef.GetSASURI(azure.BlobSASOptions{
|
|
|
|
BlobServiceSASPermissions: azure.BlobServiceSASPermissions{
|
|
|
|
Read: true,
|
|
|
|
},
|
|
|
|
SASOptions: azure.SASOptions{
|
|
|
|
Expiry: expiresTime,
|
|
|
|
},
|
|
|
|
})
|
2015-01-16 19:18:42 +01:00
|
|
|
}
|
|
|
|
|
2017-11-29 20:17:39 +01:00
|
|
|
// Walk traverses a filesystem defined within driver, starting
|
2021-06-24 20:42:02 +02:00
|
|
|
// from the given path, calling f on each file and directory
|
2017-11-29 20:17:39 +01:00
|
|
|
func (d *driver) Walk(ctx context.Context, path string, f storagedriver.WalkFn) error {
|
|
|
|
return storagedriver.WalkFallback(ctx, d, path, f)
|
|
|
|
}
|
|
|
|
|
2014-11-25 19:40:24 +01:00
|
|
|
// directDescendants will find direct descendants (blobs or virtual containers)
|
|
|
|
// of from list of blob paths and will return their full paths. Elements in blobs
|
|
|
|
// list must be prefixed with a "/" and
|
|
|
|
//
|
|
|
|
// Example: direct descendants of "/" in {"/foo", "/bar/1", "/bar/2"} is
|
|
|
|
// {"/foo", "/bar"} and direct descendants of "bar" is {"/bar/1", "/bar/2"}
|
|
|
|
func directDescendants(blobs []string, prefix string) []string {
|
|
|
|
if !strings.HasPrefix(prefix, "/") { // add trailing '/'
|
|
|
|
prefix = "/" + prefix
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(prefix, "/") { // containerify the path
|
|
|
|
prefix += "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make(map[string]bool)
|
|
|
|
for _, b := range blobs {
|
|
|
|
if strings.HasPrefix(b, prefix) {
|
|
|
|
rel := b[len(prefix):]
|
|
|
|
c := strings.Count(rel, "/")
|
|
|
|
if c == 0 {
|
|
|
|
out[b] = true
|
|
|
|
} else {
|
|
|
|
out[prefix+rel[:strings.Index(rel, "/")]] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var keys []string
|
|
|
|
for k := range out {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
2015-02-04 08:21:39 +01:00
|
|
|
func (d *driver) listBlobs(container, virtPath string) ([]string, error) {
|
2014-11-25 19:40:24 +01:00
|
|
|
if virtPath != "" && !strings.HasSuffix(virtPath, "/") { // containerify the path
|
|
|
|
virtPath += "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
out := []string{}
|
|
|
|
marker := ""
|
2017-04-14 03:05:38 +02:00
|
|
|
containerRef := d.client.GetContainerReference(d.container)
|
2014-11-25 19:40:24 +01:00
|
|
|
for {
|
2017-04-14 03:05:38 +02:00
|
|
|
resp, err := containerRef.ListBlobs(azure.ListBlobsParameters{
|
2014-11-25 19:40:24 +01:00
|
|
|
Marker: marker,
|
|
|
|
Prefix: virtPath,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, b := range resp.Blobs {
|
|
|
|
out = append(out, b.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Blobs) == 0 || resp.NextMarker == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
marker = resp.NextMarker
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func is404(err error) bool {
|
2016-03-14 19:18:27 +01:00
|
|
|
statusCodeErr, ok := err.(azure.AzureStorageServiceError)
|
|
|
|
return ok && statusCodeErr.StatusCode == http.StatusNotFound
|
2016-02-08 23:29:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type writer struct {
|
|
|
|
driver *driver
|
|
|
|
path string
|
|
|
|
size int64
|
|
|
|
bw *bufio.Writer
|
|
|
|
closed bool
|
|
|
|
committed bool
|
|
|
|
cancelled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) newWriter(path string, size int64) storagedriver.FileWriter {
|
|
|
|
return &writer{
|
|
|
|
driver: d,
|
|
|
|
path: path,
|
|
|
|
size: size,
|
|
|
|
bw: bufio.NewWriterSize(&blockWriter{
|
|
|
|
client: d.client,
|
|
|
|
container: d.container,
|
|
|
|
path: path,
|
|
|
|
}, maxChunkSize),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) Write(p []byte) (int, error) {
|
|
|
|
if w.closed {
|
|
|
|
return 0, fmt.Errorf("already closed")
|
|
|
|
} else if w.committed {
|
|
|
|
return 0, fmt.Errorf("already committed")
|
|
|
|
} else if w.cancelled {
|
|
|
|
return 0, fmt.Errorf("already cancelled")
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := w.bw.Write(p)
|
|
|
|
w.size += int64(n)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) Size() int64 {
|
|
|
|
return w.size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) Close() error {
|
|
|
|
if w.closed {
|
|
|
|
return fmt.Errorf("already closed")
|
|
|
|
}
|
|
|
|
w.closed = true
|
|
|
|
return w.bw.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) Cancel() error {
|
|
|
|
if w.closed {
|
|
|
|
return fmt.Errorf("already closed")
|
|
|
|
} else if w.committed {
|
|
|
|
return fmt.Errorf("already committed")
|
|
|
|
}
|
|
|
|
w.cancelled = true
|
2018-05-21 21:05:11 +02:00
|
|
|
blobRef := w.driver.client.GetContainerReference(w.driver.container).GetBlobReference(w.path)
|
|
|
|
return blobRef.Delete(nil)
|
2016-02-08 23:29:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) Commit() error {
|
|
|
|
if w.closed {
|
|
|
|
return fmt.Errorf("already closed")
|
|
|
|
} else if w.committed {
|
|
|
|
return fmt.Errorf("already committed")
|
|
|
|
} else if w.cancelled {
|
|
|
|
return fmt.Errorf("already cancelled")
|
|
|
|
}
|
|
|
|
w.committed = true
|
|
|
|
return w.bw.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
type blockWriter struct {
|
|
|
|
client azure.BlobStorageClient
|
|
|
|
container string
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bw *blockWriter) Write(p []byte) (int, error) {
|
|
|
|
n := 0
|
2018-05-21 21:05:11 +02:00
|
|
|
blobRef := bw.client.GetContainerReference(bw.container).GetBlobReference(bw.path)
|
2016-02-08 23:29:21 +01:00
|
|
|
for offset := 0; offset < len(p); offset += maxChunkSize {
|
|
|
|
chunkSize := maxChunkSize
|
|
|
|
if offset+chunkSize > len(p) {
|
|
|
|
chunkSize = len(p) - offset
|
|
|
|
}
|
2018-05-21 21:05:11 +02:00
|
|
|
err := blobRef.AppendBlock(p[offset:offset+chunkSize], nil)
|
2016-02-08 23:29:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
n += chunkSize
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|