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"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/replication/repl_util"
|
2018-10-09 08:35:48 +00:00
|
|
|
"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"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/replication/sink"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/replication/source"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2018-10-09 08:35:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AzureSink struct {
|
2021-03-01 00:19:03 +00:00
|
|
|
containerURL azblob.ContainerURL
|
|
|
|
container string
|
|
|
|
dir string
|
|
|
|
filerSource *source.FilerSource
|
|
|
|
isIncremental bool
|
2018-10-09 08:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
sink.Sinks = append(sink.Sinks, &AzureSink{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *AzureSink) GetName() string {
|
|
|
|
return "azure"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *AzureSink) GetSinkToDirectory() string {
|
|
|
|
return g.dir
|
|
|
|
}
|
|
|
|
|
2021-03-01 00:19:03 +00:00
|
|
|
func (g *AzureSink) IsIncremental() bool {
|
|
|
|
return g.isIncremental
|
|
|
|
}
|
|
|
|
|
2020-01-29 17:09:55 +00:00
|
|
|
func (g *AzureSink) Initialize(configuration util.Configuration, prefix string) error {
|
2021-03-01 04:34:14 +00:00
|
|
|
g.isIncremental = configuration.GetBool(prefix + "is_incremental")
|
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 {
|
2021-08-24 07:32:35 +00:00
|
|
|
glog.Fatalf("failed to create Azure credential with account name:%s: %v", accountName, err)
|
2018-10-23 09:18:46 +00:00
|
|
|
}
|
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-09-09 18:21:23 +00:00
|
|
|
func (g *AzureSink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) 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-09-09 18:21:23 +00:00
|
|
|
func (g *AzureSink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) 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
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
totalSize := filer.FileSize(entry)
|
2022-11-15 14:33:36 +00:00
|
|
|
chunkViews := filer.ViewFromChunks(g.filerSource.LookupFileId, entry.GetChunks(), 0, int64(totalSize))
|
2018-10-09 08:35:48 +00:00
|
|
|
|
|
|
|
// Create a URL that references a to-be-created blob in your
|
|
|
|
// Azure Storage account's container.
|
|
|
|
appendBlobURL := g.containerURL.NewAppendBlobURL(key)
|
|
|
|
|
2022-05-11 07:52:15 +00:00
|
|
|
_, err := appendBlobURL.Create(context.Background(), azblob.BlobHTTPHeaders{}, azblob.Metadata{}, azblob.BlobAccessConditions{}, azblob.BlobTagsMap{}, azblob.ClientProvidedKeyOptions{}, azblob.ImmutabilityPolicyOptions{})
|
2018-10-09 08:35:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-08 05:49:04 +00:00
|
|
|
writeFunc := func(data []byte) error {
|
2021-08-24 07:32:35 +00:00
|
|
|
_, writeErr := appendBlobURL.AppendBlock(context.Background(), bytes.NewReader(data), azblob.AppendBlobAccessConditions{}, nil, azblob.ClientProvidedKeyOptions{})
|
2020-10-08 05:49:04 +00:00
|
|
|
return writeErr
|
|
|
|
}
|
2018-10-09 08:35:48 +00:00
|
|
|
|
2022-08-20 06:00:56 +00:00
|
|
|
if len(entry.Content) > 0 {
|
|
|
|
return writeFunc(entry.Content)
|
|
|
|
}
|
|
|
|
|
2020-10-08 05:49:04 +00:00
|
|
|
if err := repl_util.CopyFromChunkViews(chunkViews, g.filerSource, writeFunc); err != nil {
|
|
|
|
return err
|
2018-10-09 08:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:21:23 +00:00
|
|
|
func (g *AzureSink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool, signatures []int32) (foundExistingEntry bool, err error) {
|
2018-11-04 19:58:59 +00:00
|
|
|
key = cleanKey(key)
|
2021-05-26 21:53:11 +00:00
|
|
|
return true, g.CreateEntry(key, newEntry, signatures)
|
2018-10-09 08:35:48 +00:00
|
|
|
}
|
2018-11-04 19:58:59 +00:00
|
|
|
|
|
|
|
func cleanKey(key string) string {
|
|
|
|
if strings.HasPrefix(key, "/") {
|
|
|
|
key = key[1:]
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|