Incorporate storagedriver base in azure, remove path checks
Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>
This commit is contained in:
parent
e7485c831f
commit
d606948fd7
1 changed files with 19 additions and 38 deletions
|
@ -12,6 +12,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/distribution/storagedriver"
|
"github.com/docker/distribution/storagedriver"
|
||||||
|
"github.com/docker/distribution/storagedriver/base"
|
||||||
"github.com/docker/distribution/storagedriver/factory"
|
"github.com/docker/distribution/storagedriver/factory"
|
||||||
|
|
||||||
azure "github.com/MSOpenTech/azure-sdk-for-go/clients/storage"
|
azure "github.com/MSOpenTech/azure-sdk-for-go/clients/storage"
|
||||||
|
@ -25,13 +26,17 @@ const (
|
||||||
paramContainer = "container"
|
paramContainer = "container"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Driver is a storagedriver.StorageDriver implementation backed by
|
type driver struct {
|
||||||
// Microsoft Azure Blob Storage Service.
|
|
||||||
type Driver struct {
|
|
||||||
client azure.BlobStorageClient
|
client azure.BlobStorageClient
|
||||||
container string
|
container string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type baseEmbed struct{ base.Base }
|
||||||
|
|
||||||
|
// Driver is a storagedriver.StorageDriver implementation backed by
|
||||||
|
// Microsoft Azure Blob Storage Service.
|
||||||
|
type Driver struct{ baseEmbed }
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
factory.Register(driverName, &azureDriverFactory{})
|
factory.Register(driverName, &azureDriverFactory{})
|
||||||
}
|
}
|
||||||
|
@ -76,19 +81,16 @@ func New(accountName, accountKey, container string) (*Driver, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Driver{
|
d := &driver{
|
||||||
client: *blobClient,
|
client: *blobClient,
|
||||||
container: container}, nil
|
container: container}
|
||||||
|
return &Driver{baseEmbed: baseEmbed{Base: base.Base{StorageDriver: d}}}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implement the storagedriver.StorageDriver interface.
|
// Implement the storagedriver.StorageDriver interface.
|
||||||
|
|
||||||
// GetContent retrieves the content stored at "path" as a []byte.
|
// GetContent retrieves the content stored at "path" as a []byte.
|
||||||
func (d *Driver) GetContent(path string) ([]byte, error) {
|
func (d *driver) GetContent(path string) ([]byte, error) {
|
||||||
if !storagedriver.PathRegexp.MatchString(path) {
|
|
||||||
return nil, storagedriver.InvalidPathError{Path: path}
|
|
||||||
}
|
|
||||||
|
|
||||||
blob, err := d.client.GetBlob(d.container, path)
|
blob, err := d.client.GetBlob(d.container, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if is404(err) {
|
if is404(err) {
|
||||||
|
@ -101,10 +103,7 @@ func (d *Driver) GetContent(path string) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// PutContent stores the []byte content at a location designated by "path".
|
// PutContent stores the []byte content at a location designated by "path".
|
||||||
func (d *Driver) PutContent(path string, contents []byte) error {
|
func (d *driver) PutContent(path string, contents []byte) error {
|
||||||
if !storagedriver.PathRegexp.MatchString(path) {
|
|
||||||
return storagedriver.InvalidPathError{Path: path}
|
|
||||||
}
|
|
||||||
return d.client.PutBlockBlob(d.container, path, ioutil.NopCloser(bytes.NewReader(contents)))
|
return d.client.PutBlockBlob(d.container, path, ioutil.NopCloser(bytes.NewReader(contents)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,7 +136,7 @@ func (d *driver) ReadStream(path string, offset int64) (io.ReadCloser, error) {
|
||||||
|
|
||||||
// WriteStream stores the contents of the provided io.ReadCloser at a location
|
// WriteStream stores the contents of the provided io.ReadCloser at a location
|
||||||
// designated by the given path.
|
// designated by the given path.
|
||||||
func (d *Driver) WriteStream(path string, offset int64, reader io.Reader) (int64, error) {
|
func (d *driver) WriteStream(path string, offset int64, reader io.Reader) (int64, error) {
|
||||||
if blobExists, err := d.client.BlobExists(d.container, path); err != nil {
|
if blobExists, err := d.client.BlobExists(d.container, path); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
} else if !blobExists {
|
} else if !blobExists {
|
||||||
|
@ -158,11 +157,7 @@ func (d *Driver) WriteStream(path string, offset int64, reader io.Reader) (int64
|
||||||
|
|
||||||
// Stat retrieves the FileInfo for the given path, including the current size
|
// Stat retrieves the FileInfo for the given path, including the current size
|
||||||
// in bytes and the creation time.
|
// in bytes and the creation time.
|
||||||
func (d *Driver) Stat(path string) (storagedriver.FileInfo, error) {
|
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
|
// Check if the path is a blob
|
||||||
if ok, err := d.client.BlobExists(d.container, path); err != nil {
|
if ok, err := d.client.BlobExists(d.container, path); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -211,11 +206,7 @@ func (d *Driver) Stat(path string) (storagedriver.FileInfo, error) {
|
||||||
|
|
||||||
// List returns a list of the objects that are direct descendants of the given
|
// List returns a list of the objects that are direct descendants of the given
|
||||||
// path.
|
// path.
|
||||||
func (d *Driver) List(path string) ([]string, error) {
|
func (d *driver) List(path string) ([]string, error) {
|
||||||
if !storagedriver.PathRegexp.MatchString(path) && path != "/" {
|
|
||||||
return nil, storagedriver.InvalidPathError{Path: path}
|
|
||||||
}
|
|
||||||
|
|
||||||
if path == "/" {
|
if path == "/" {
|
||||||
path = ""
|
path = ""
|
||||||
}
|
}
|
||||||
|
@ -231,13 +222,7 @@ func (d *Driver) List(path string) ([]string, error) {
|
||||||
|
|
||||||
// Move moves an object stored at sourcePath to destPath, removing the original
|
// Move moves an object stored at sourcePath to destPath, removing the original
|
||||||
// object.
|
// object.
|
||||||
func (d *Driver) Move(sourcePath string, destPath string) error {
|
func (d *driver) Move(sourcePath string, destPath string) error {
|
||||||
if !storagedriver.PathRegexp.MatchString(sourcePath) {
|
|
||||||
return storagedriver.InvalidPathError{Path: sourcePath}
|
|
||||||
} else if !storagedriver.PathRegexp.MatchString(destPath) {
|
|
||||||
return storagedriver.InvalidPathError{Path: destPath}
|
|
||||||
}
|
|
||||||
|
|
||||||
sourceBlobURL := d.client.GetBlobUrl(d.container, sourcePath)
|
sourceBlobURL := d.client.GetBlobUrl(d.container, sourcePath)
|
||||||
err := d.client.CopyBlob(d.container, destPath, sourceBlobURL)
|
err := d.client.CopyBlob(d.container, destPath, sourceBlobURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -251,11 +236,7 @@ func (d *Driver) Move(sourcePath string, destPath string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete recursively deletes all objects stored at "path" and its subpaths.
|
// Delete recursively deletes all objects stored at "path" and its subpaths.
|
||||||
func (d *Driver) Delete(path string) error {
|
func (d *driver) Delete(path string) error {
|
||||||
if !storagedriver.PathRegexp.MatchString(path) {
|
|
||||||
return storagedriver.InvalidPathError{Path: path}
|
|
||||||
}
|
|
||||||
|
|
||||||
ok, err := d.client.DeleteBlobIfExists(d.container, path)
|
ok, err := d.client.DeleteBlobIfExists(d.container, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -331,7 +312,7 @@ func directDescendants(blobs []string, prefix string) []string {
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) listBlobs(container, virtPath string) ([]string, error) {
|
func (d *driver) listBlobs(container, virtPath string) ([]string, error) {
|
||||||
if virtPath != "" && !strings.HasSuffix(virtPath, "/") { // containerify the path
|
if virtPath != "" && !strings.HasSuffix(virtPath, "/") { // containerify the path
|
||||||
virtPath += "/"
|
virtPath += "/"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue