2018-10-09 08:35:48 +00:00
|
|
|
package azuresink
|
|
|
|
|
|
|
|
import (
|
2018-10-11 07:08:13 +00:00
|
|
|
"bytes"
|
2018-10-09 08:35:48 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2018-11-04 19:58:59 +00:00
|
|
|
"strings"
|
2018-10-09 08:35:48 +00:00
|
|
|
|
2018-10-23 09:18:46 +00:00
|
|
|
"github.com/Azure/azure-storage-blob-go/azblob"
|
2018-10-11 07:08:13 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
2018-10-24 06:59:49 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-10-09 08:35:48 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-10-11 07:08:13 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/replication/sink"
|
2018-10-09 08:35:48 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/replication/source"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AzureSink struct {
|
|
|
|
containerURL azblob.ContainerURL
|
|
|
|
container string
|
|
|
|
dir string
|
|
|
|
filerSource *source.FilerSource
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
sink.Sinks = append(sink.Sinks, &AzureSink{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *AzureSink) GetName() string {
|
|
|
|
return "azure"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *AzureSink) GetSinkToDirectory() string {
|
|
|
|
return g.dir
|
|
|
|
}
|
|
|
|
|
2020-01-29 17:09:55 +00:00
|
|
|
func (g *AzureSink) Initialize(configuration util.Configuration, prefix string) error {
|
2018-10-09 08:35:48 +00:00
|
|
|
return g.initialize(
|
2020-01-29 17:09:55 +00:00
|
|
|
configuration.GetString(prefix+"account_name"),
|
|
|
|
configuration.GetString(prefix+"account_key"),
|
|
|
|
configuration.GetString(prefix+"container"),
|
|
|
|
configuration.GetString(prefix+"directory"),
|
2018-10-09 08:35:48 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *AzureSink) SetSourceFiler(s *source.FilerSource) {
|
|
|
|
g.filerSource = s
|
|
|
|
}
|
|
|
|
|
2018-10-11 07:08:13 +00:00
|
|
|
func (g *AzureSink) initialize(accountName, accountKey, container, dir string) error {
|
2018-10-09 08:35:48 +00:00
|
|
|
g.container = container
|
|
|
|
g.dir = dir
|
|
|
|
|
|
|
|
// Use your Storage account's name and key to create a credential object.
|
2018-10-23 09:18:46 +00:00
|
|
|
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("failed to create Azure credential with account name:%s key:%s", accountName, accountKey)
|
|
|
|
}
|
2018-10-09 08:35:48 +00:00
|
|
|
|
|
|
|
// Create a request pipeline that is used to process HTTP(S) requests and responses.
|
|
|
|
p := azblob.NewPipeline(credential, azblob.PipelineOptions{})
|
|
|
|
|
|
|
|
// Create an ServiceURL object that wraps the service URL and a request pipeline.
|
|
|
|
u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", accountName))
|
|
|
|
serviceURL := azblob.NewServiceURL(*u, p)
|
|
|
|
|
|
|
|
g.containerURL = serviceURL.NewContainerURL(g.container)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
func (g *AzureSink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool) error {
|
2018-10-09 08:35:48 +00:00
|
|
|
|
2018-11-04 19:58:59 +00:00
|
|
|
key = cleanKey(key)
|
|
|
|
|
2018-10-09 08:35:48 +00:00
|
|
|
if isDirectory {
|
|
|
|
key = key + "/"
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
if _, err := g.containerURL.NewBlobURL(key).Delete(context.Background(),
|
2018-10-09 08:35:48 +00:00
|
|
|
azblob.DeleteSnapshotsOptionInclude, azblob.BlobAccessConditions{}); err != nil {
|
|
|
|
return fmt.Errorf("azure delete %s/%s: %v", g.container, key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
func (g *AzureSink) CreateEntry(key string, entry *filer_pb.Entry) error {
|
2018-10-09 08:35:48 +00:00
|
|
|
|
2018-11-04 19:58:59 +00:00
|
|
|
key = cleanKey(key)
|
|
|
|
|
2018-10-09 08:35:48 +00:00
|
|
|
if entry.IsDirectory {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
totalSize := filer2.TotalSize(entry.Chunks)
|
|
|
|
chunkViews := filer2.ViewFromChunks(entry.Chunks, 0, int(totalSize))
|
|
|
|
|
|
|
|
// Create a URL that references a to-be-created blob in your
|
|
|
|
// Azure Storage account's container.
|
|
|
|
appendBlobURL := g.containerURL.NewAppendBlobURL(key)
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
_, err := appendBlobURL.Create(context.Background(), azblob.BlobHTTPHeaders{}, azblob.Metadata{}, azblob.BlobAccessConditions{})
|
2018-10-09 08:35:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, chunk := range chunkViews {
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
fileUrl, err := g.filerSource.LookupFileId(chunk.FileId)
|
2018-10-09 08:35:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var writeErr error
|
|
|
|
_, readErr := util.ReadUrlAsStream(fileUrl, chunk.Offset, int(chunk.Size), func(data []byte) {
|
2020-02-26 05:50:12 +00:00
|
|
|
_, writeErr = appendBlobURL.AppendBlock(context.Background(), bytes.NewReader(data), azblob.AppendBlobAccessConditions{}, nil)
|
2018-10-09 08:35:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if readErr != nil {
|
|
|
|
return readErr
|
|
|
|
}
|
|
|
|
if writeErr != nil {
|
|
|
|
return writeErr
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
func (g *AzureSink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (foundExistingEntry bool, err error) {
|
2018-11-04 19:58:59 +00:00
|
|
|
key = cleanKey(key)
|
2018-10-09 08:35:48 +00:00
|
|
|
// TODO improve efficiency
|
|
|
|
return false, nil
|
|
|
|
}
|
2018-11-04 19:58:59 +00:00
|
|
|
|
|
|
|
func cleanKey(key string) string {
|
|
|
|
if strings.HasPrefix(key, "/") {
|
|
|
|
key = key[1:]
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|