From e07d5b7de567108ab4406b39d661e48124974f62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wenkai=20Yin=28=E5=B0=B9=E6=96=87=E5=BC=80=29?= Date: Fri, 15 Sep 2023 11:36:35 +0800 Subject: [PATCH] Add a new function to create the Azure backend storage with the provided client MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a new function to create the Azure backend storage with the provided client so that we can wrap and reuse the storage implementation on Velero side Signed-off-by: Wenkai Yin(尹文开) --- repo/blob/azure/patch.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 repo/blob/azure/patch.go diff --git a/repo/blob/azure/patch.go b/repo/blob/azure/patch.go new file mode 100644 index 00000000000..07c5408affb --- /dev/null +++ b/repo/blob/azure/patch.go @@ -0,0 +1,34 @@ +package azure + +import ( + "context" + "fmt" + + "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" + "github.com/kopia/kopia/internal/clock" + "github.com/kopia/kopia/repo/blob" + "github.com/kopia/kopia/repo/blob/retrying" + "github.com/pkg/errors" +) + +// NewWithClient creates new Azure backend storage with the specified client +func NewWithClient(ctx context.Context, opt *Options, client *azblob.Client) (blob.Storage, error) { + raw := &azStorage{ + Options: *opt, + container: opt.Container, + service: client, + } + + az := retrying.NewWrapper(raw) + + // verify Azure connection is functional by listing blobs in a bucket, which will fail if the container + // does not exist. We list with a prefix that will not exist, to avoid iterating through any objects. + nonExistentPrefix := fmt.Sprintf("kopia-azure-storage-initializing-%v", clock.Now().UnixNano()) + if err := raw.ListBlobs(ctx, blob.ID(nonExistentPrefix), func(md blob.Metadata) error { + return nil + }); err != nil { + return nil, errors.Wrap(err, "unable to list from the bucket") + } + + return az, nil +}