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 (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-01-16 19:18:42 +01:00
|
|
|
"net/http"
|
2014-11-25 19:40:24 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-01-16 19:18:42 +01:00
|
|
|
"time"
|
2014-11-25 19:40:24 +01:00
|
|
|
|
2014-12-24 01:01:38 +01:00
|
|
|
"github.com/docker/distribution/storagedriver"
|
|
|
|
"github.com/docker/distribution/storagedriver/factory"
|
2014-11-25 19:40:24 +01:00
|
|
|
|
|
|
|
azure "github.com/MSOpenTech/azure-sdk-for-go/clients/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
const driverName = "azure"
|
|
|
|
|
|
|
|
const (
|
|
|
|
paramAccountName = "accountname"
|
|
|
|
paramAccountKey = "accountkey"
|
|
|
|
paramContainer = "container"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Driver is a storagedriver.StorageDriver implementation backed by
|
|
|
|
// Microsoft Azure Blob Storage Service.
|
|
|
|
type Driver struct {
|
|
|
|
client *azure.BlobStorageClient
|
|
|
|
container string
|
|
|
|
}
|
|
|
|
|
|
|
|
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) == "" {
|
2014-11-25 19:40:24 +01:00
|
|
|
return nil, fmt.Errorf("No %s parameter provided", paramAccountName)
|
|
|
|
}
|
|
|
|
|
|
|
|
accountKey, ok := parameters[paramAccountKey]
|
2015-01-16 19:18:42 +01:00
|
|
|
if !ok || fmt.Sprint(accountKey) == "" {
|
2014-11-25 19:40:24 +01:00
|
|
|
return nil, fmt.Errorf("No %s parameter provided", paramAccountKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
container, ok := parameters[paramContainer]
|
2015-01-16 19:18:42 +01:00
|
|
|
if !ok || fmt.Sprint(container) == "" {
|
2014-11-25 19:40:24 +01:00
|
|
|
return nil, fmt.Errorf("No %s parameter provided", paramContainer)
|
|
|
|
}
|
|
|
|
|
2015-01-16 19:18:42 +01:00
|
|
|
return New(fmt.Sprint(accountName), fmt.Sprint(accountKey), fmt.Sprint(container))
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// New constructs a new Driver with the given Azure Storage Account credentials
|
|
|
|
func New(accountName, accountKey, container string) (*Driver, error) {
|
|
|
|
api, err := azure.NewBasicClient(accountName, accountKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blobClient := api.GetBlobService()
|
|
|
|
|
|
|
|
// Create registry container
|
|
|
|
if _, err = blobClient.CreateContainerIfNotExists(container, azure.ContainerAccessTypePrivate); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Driver{
|
|
|
|
client: blobClient,
|
|
|
|
container: container}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement the storagedriver.StorageDriver interface.
|
|
|
|
|
|
|
|
// GetContent retrieves the content stored at "path" as a []byte.
|
|
|
|
func (d *Driver) GetContent(path string) ([]byte, error) {
|
2015-01-16 19:18:42 +01:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return nil, storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
2014-11-25 19:40:24 +01:00
|
|
|
blob, err := d.client.GetBlob(d.container, path)
|
|
|
|
if err != nil {
|
|
|
|
if is404(err) {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.ReadAll(blob)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutContent stores the []byte content at a location designated by "path".
|
|
|
|
func (d *Driver) PutContent(path string, contents []byte) error {
|
2015-01-16 19:18:42 +01:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
2014-11-25 19:40:24 +01:00
|
|
|
return d.client.PutBlockBlob(d.container, path, ioutil.NopCloser(bytes.NewReader(contents)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadStream retrieves an io.ReadCloser for the content stored at "path" with a
|
|
|
|
// given byte offset.
|
2015-01-16 19:18:42 +01:00
|
|
|
func (d *driver) ReadStream(path string, offset int64) (io.ReadCloser, error) {
|
2014-11-25 19:40:24 +01:00
|
|
|
if ok, err := d.client.BlobExists(d.container, path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !ok {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
|
2015-01-16 19:18:42 +01:00
|
|
|
info, err := d.client.GetBlobProperties(d.container, path)
|
2014-11-25 19:40:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-16 19:18:42 +01:00
|
|
|
size := int64(info.ContentLength)
|
|
|
|
if offset >= size {
|
|
|
|
return ioutil.NopCloser(bytes.NewReader(nil)), nil
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bytesRange := fmt.Sprintf("%v-", offset)
|
|
|
|
resp, err := d.client.GetBlobRange(d.container, path, bytesRange)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteStream stores the contents of the provided io.ReadCloser at a location
|
|
|
|
// designated by the given path.
|
2015-01-16 19:18:42 +01:00
|
|
|
func (d *Driver) WriteStream(path string, offset int64, reader io.Reader) (int64, error) {
|
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return 0, storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
2014-11-25 19:40:24 +01:00
|
|
|
var (
|
|
|
|
lastBlockNum int
|
2014-12-03 04:01:00 +01:00
|
|
|
resumableOffset int64
|
2014-11-25 19:40:24 +01:00
|
|
|
blocks []azure.Block
|
|
|
|
)
|
|
|
|
|
|
|
|
if blobExists, err := d.client.BlobExists(d.container, path); err != nil {
|
2015-01-16 19:18:42 +01:00
|
|
|
return 0, err
|
2014-11-25 19:40:24 +01:00
|
|
|
} else if !blobExists { // new blob
|
|
|
|
lastBlockNum = 0
|
|
|
|
resumableOffset = 0
|
|
|
|
} else { // append
|
|
|
|
if parts, err := d.client.GetBlockList(d.container, path, azure.BlockListTypeCommitted); err != nil {
|
2015-01-16 19:18:42 +01:00
|
|
|
return 0, err
|
2014-11-25 19:40:24 +01:00
|
|
|
} else if len(parts.CommittedBlocks) == 0 {
|
|
|
|
lastBlockNum = 0
|
|
|
|
resumableOffset = 0
|
|
|
|
} else {
|
|
|
|
lastBlock := parts.CommittedBlocks[len(parts.CommittedBlocks)-1]
|
2015-01-16 19:18:42 +01:00
|
|
|
if lastBlockNum, err = fromBlockID(lastBlock.Name); err != nil {
|
|
|
|
return 0, fmt.Errorf("Cannot parse block name as number '%s': %s", lastBlock.Name, err.Error())
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
2014-12-03 04:01:00 +01:00
|
|
|
var totalSize int64
|
2014-11-25 19:40:24 +01:00
|
|
|
for _, v := range parts.CommittedBlocks {
|
|
|
|
blocks = append(blocks, azure.Block{
|
|
|
|
Id: v.Name,
|
|
|
|
Status: azure.BlockStatusCommitted})
|
2014-12-03 04:01:00 +01:00
|
|
|
totalSize += int64(v.Size)
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Azure driver currently supports only append mode (resumable
|
|
|
|
// index is exactly where the committed blocks of the blob end).
|
|
|
|
// In order to support writing to offsets other than last index,
|
|
|
|
// adjacent blocks overlapping with the [offset:offset+size] area
|
|
|
|
// must be fetched, splitted and should be overwritten accordingly.
|
|
|
|
// As the current use of this method is append only, that implementation
|
|
|
|
// is omitted.
|
|
|
|
resumableOffset = totalSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 19:18:42 +01:00
|
|
|
if offset < resumableOffset {
|
|
|
|
// only writing at the end or after the end of the file is supported
|
|
|
|
return 0, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
|
|
|
} else if offset > resumableOffset {
|
|
|
|
// zero-fill in between, construct a multi-reader
|
|
|
|
zeroReader := bytes.NewReader(make([]byte, offset-resumableOffset))
|
|
|
|
reader = io.MultiReader(zeroReader, reader)
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put content
|
2015-01-16 19:18:42 +01:00
|
|
|
var nn int64
|
2014-11-25 19:40:24 +01:00
|
|
|
buf := make([]byte, azure.MaxBlobBlockSize)
|
|
|
|
for {
|
|
|
|
// Read chunks of exactly size N except the last chunk to
|
|
|
|
// maximize block size and minimize block count.
|
|
|
|
n, err := io.ReadFull(reader, buf)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2015-01-16 19:18:42 +01:00
|
|
|
nn += int64(n)
|
2014-11-25 19:40:24 +01:00
|
|
|
|
|
|
|
data := buf[:n]
|
2015-01-16 19:18:42 +01:00
|
|
|
lastBlockNum++
|
|
|
|
blockID := toBlockID(lastBlockNum)
|
2014-11-25 19:40:24 +01:00
|
|
|
if err = d.client.PutBlock(d.container, path, blockID, data); err != nil {
|
2015-01-16 19:18:42 +01:00
|
|
|
return 0, err
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
2015-01-16 19:18:42 +01:00
|
|
|
|
2014-11-25 19:40:24 +01:00
|
|
|
blocks = append(blocks, azure.Block{
|
|
|
|
Id: blockID,
|
|
|
|
Status: azure.BlockStatusLatest})
|
2015-01-16 19:18:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there was a zero-fill, adjust nn to exclude zeros
|
|
|
|
if offset > resumableOffset {
|
|
|
|
nn -= offset - resumableOffset
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit block list
|
2015-01-16 19:18:42 +01:00
|
|
|
return nn, d.client.PutBlockList(d.container, path, blocks)
|
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.
|
|
|
|
func (d *Driver) Stat(path string) (storagedriver.FileInfo, error) {
|
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return nil, storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the path is a blob
|
|
|
|
if ok, err := d.client.BlobExists(d.container, path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if ok {
|
|
|
|
blob, err := d.client.GetBlobProperties(d.container, path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mtim, err := time.Parse(http.TimeFormat, blob.LastModified)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return storagedriver.FileInfoInternal{FileInfoFields: storagedriver.FileInfoFields{
|
|
|
|
Path: path,
|
|
|
|
Size: int64(blob.ContentLength),
|
|
|
|
ModTime: mtim,
|
|
|
|
IsDir: false,
|
|
|
|
}}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if path is a virtual container
|
|
|
|
virtContainerPath := path
|
|
|
|
if !strings.HasSuffix(virtContainerPath, "/") {
|
|
|
|
virtContainerPath += "/"
|
|
|
|
}
|
|
|
|
blobs, err := d.client.ListBlobs(d.container, azure.ListBlobsParameters{
|
|
|
|
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.
|
|
|
|
func (d *Driver) List(path string) ([]string, error) {
|
2015-01-16 19:18:42 +01:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) && path != "/" {
|
|
|
|
return nil, storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move moves an object stored at sourcePath to destPath, removing the original
|
|
|
|
// object.
|
|
|
|
func (d *Driver) Move(sourcePath string, destPath string) error {
|
2015-01-16 19:18:42 +01:00
|
|
|
if !storagedriver.PathRegexp.MatchString(sourcePath) {
|
|
|
|
return storagedriver.InvalidPathError{Path: sourcePath}
|
|
|
|
} else if !storagedriver.PathRegexp.MatchString(destPath) {
|
|
|
|
return storagedriver.InvalidPathError{Path: destPath}
|
|
|
|
}
|
|
|
|
|
2014-11-25 19:40:24 +01:00
|
|
|
sourceBlobURL := d.client.GetBlobUrl(d.container, sourcePath)
|
|
|
|
err := d.client.CopyBlob(d.container, destPath, sourceBlobURL)
|
|
|
|
if err != nil {
|
|
|
|
if is404(err) {
|
|
|
|
return storagedriver.PathNotFoundError{Path: sourcePath}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.client.DeleteBlob(d.container, sourcePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete recursively deletes all objects stored at "path" and its subpaths.
|
|
|
|
func (d *Driver) Delete(path string) error {
|
2015-01-16 19:18:42 +01:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
2014-11-25 19:40:24 +01:00
|
|
|
ok, err := d.client.DeleteBlobIfExists(d.container, path)
|
|
|
|
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 {
|
|
|
|
if err = d.client.DeleteBlob(d.container, b); err != nil {
|
|
|
|
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.
|
|
|
|
func (d *driver) URLFor(path string, options map[string]interface{}) (string, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d.client.GetBlobSASURI(d.container, path, expiresTime, "r")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) listBlobs(container, virtPath string) ([]string, error) {
|
|
|
|
if virtPath != "" && !strings.HasSuffix(virtPath, "/") { // containerify the path
|
|
|
|
virtPath += "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
out := []string{}
|
|
|
|
marker := ""
|
|
|
|
for {
|
|
|
|
resp, err := d.client.ListBlobs(d.container, azure.ListBlobsParameters{
|
|
|
|
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 {
|
|
|
|
e, ok := err.(azure.StorageServiceError)
|
|
|
|
return ok && e.StatusCode == 404
|
|
|
|
}
|
|
|
|
|
2015-01-16 19:18:42 +01:00
|
|
|
func fromBlockID(b64Name string) (int, error) {
|
2014-11-25 19:40:24 +01:00
|
|
|
s, err := base64.StdEncoding.DecodeString(b64Name)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return strconv.Atoi(string(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
func toBlockID(i int) string {
|
2015-01-16 19:18:42 +01:00
|
|
|
s := fmt.Sprintf("%010d", i) // add zero padding
|
|
|
|
return base64.StdEncoding.EncodeToString([]byte(s))
|
2014-11-25 19:40:24 +01:00
|
|
|
}
|