解密 blob 并将其复制到其他 blob 存储帐户?

Decrypt and copy blob to other blob storage account?

我正在使用 this approach 加密文件并将它们存储在 Azure 块 blob 中。我想将加密的 blob 复制到另一个 blob 存储帐户并在此过程中对其进行解密。我知道可以执行 "copy blob" 操作,该操作完全在 Azure 中异步运行,并且不会在传输过程中通过我的本地计算机下载 blob 内容。我相信这是通过 CloudBlockBlob.StartCopy 方法完成的。但是是否可以使用加密文件并在传输到其他存储帐户的过程中对其进行解密?

按照上面的 link,我的代码如下所示。 blob.OpenRead 有效,但 blob2.StartCopy 无效。

BlobEncryptionPolicy policy = new BlobEncryptionPolicy(null, cloudResolver);
BlobRequestOptions options = new BlobRequestOptions() { EncryptionPolicy = policy };
CloudBlockBlob blob = container.GetBlockBlobReference("MyFile.txt");
//var blobStream = blob.OpenRead(null, options); //this works

CloudBlockBlob blob2 = container2.GetBlockBlobReference("MyFile2.txt");
blob2.StartCopy(blob, null, null, options, null); //this fails with: The remote server returned an error: (404) Not Found.

答案是加密是在存储客户端库中完成的,因此如果您将 blob 复制到新的存储帐户,它仍然会被加密。

您的代码失败的原因是源 blob 在 Private 容器中。要使跨账户复制起作用,源 blob 应该可以公开访问。在同一存储帐户中,您可以从私有容器复制 blob。据我所知,该错误与加密无关。

您可以做的是在源 blob 上创建一个 SAS URL,然后使用以下覆盖 StartCopy 方法:

public string StartCopy(
    Uri source,
    AccessCondition sourceAccessCondition = null,
    AccessCondition destAccessCondition = null,
    BlobRequestOptions options = null,
    OperationContext operationContext = null
)

这是执行此操作的示例代码:

    private static void StartCopyAcrossAccount()
    {
        var sourceAccount = new CloudStorageAccount(new StorageCredentials("source-account-name", "source-account-key"), true);
        var sourceContainer = sourceAccount.CreateCloudBlobClient().GetContainerReference("source-container");
        var sourceBlob = sourceContainer.GetBlockBlobReference("blob-name");
        var sourceBlobSas = sourceBlob.GetSharedAccessSignature(new Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy()
            {
                SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),
                Permissions = Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.Read
            });
        var sourceBlobSasUrl = sourceBlob.Uri.AbsoluteUri + sourceBlobSas;

        var targetAccount = new CloudStorageAccount(new StorageCredentials("target-account-name", "target-account-key"), true);
        var targetContainer = targetAccount.CreateCloudBlobClient().GetContainerReference("target-container");
        var targetBlob = targetContainer.GetBlockBlobReference("blob-name");

        var copyId = targetBlob.StartCopy(new Uri(sourceBlobSasUrl), null, null);
    }