vb.net Azure 存储将 blob 移动到其他容器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40260583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:06:15  来源:igfitidea点击:

Azure Storage move blob to other container

c#vb.netazurestorage

提问by Jan

I'm looking for an approach to move a blob in Azure from one container to another. The only solution I found is to use the Azure Storage Data Movement Library, but this seems to work between different accounts. I would like to move the blob within the same account to an other container.

我正在寻找一种将 Azure 中的 blob 从一个容器移动到另一个容器的方法。我找到的唯一解决方案是使用 Azure 存储数据移动库,但这似乎适用于不同帐户。我想将同一帐户中的 blob 移动到另一个容器。

回答by Gaurav Mantri-AIS

I have not used Azure Storage Data Movement Librarybut I am pretty sure that it will work in the same storage account as well.

我没有使用过,Azure Storage Data Movement Library但我很确定它也可以在同一个存储帐户中使用。

Coming to your question, since Moveoperation is not natively supported by Azure Storage what you can do is implement this by invoking Copy Blobfollowed by Delete Blob. In general Copyoperation is async however when a blob is copied in same storage account, it is a synchronous operation i.e. copy happens instantaneously. Please see sample code below which does just this:

谈到您的问题,由于MoveAzure 存储本身不支持操作,您可以通过调用Copy Blob后跟Delete Blob. 通常Copy操作是异步的,但是当在同一存储帐户中复制 blob 时,它是同步操作,即复制立即发生。请参阅下面的示例代码,它就是这样做的:

    static void MoveBlobInSameStorageAccount()
    {
        var cred = new StorageCredentials(accountName, accountKey);
        var account = new CloudStorageAccount(cred, true);
        var client = account.CreateCloudBlobClient();
        var sourceContainer = client.GetContainerReference("source-container-name");
        var sourceBlob = sourceContainer.GetBlockBlobReference("blob-name");
        var destinationContainer = client.GetContainerReference("destination-container-name");
        var destinationBlob = destinationContainer.GetBlockBlobReference("blob-name");
        destinationBlob.StartCopy(sourceBlob);
        sourceBlob.Delete(DeleteSnapshotsOption.IncludeSnapshots);
    }

However, please keep in mind that you use this code only for moving blobs in the same storage account. For moving blobs across storage account, you need to ensure that copy operation is complete before you delete the source blob.

但是,请记住,此代码仅用于移动同一存储帐户中的 Blob。对于跨存储帐户移动 Blob,您需要在删除源 Blob 之前确保复制操作已完成。

回答by Darrelk

Here is what worked for me (answer edited after better answer by @Deumber was posted):

这是对我有用的方法(在@Deumber 发布了更好的答案后编辑了答案):

    public async Task<CloudBlockBlob> Move(CloudBlockBlob srcBlob, CloudBlobContainer destContainer)
    {
        CloudBlockBlob destBlob;

        if (srcBlob == null)
        {
            throw new Exception("Source blob cannot be null.");
        }

        if (!destContainer.Exists())
        {
            throw new Exception("Destination container does not exist.");
        }

        //Copy source blob to destination container
        string name = srcBlob.Uri.Segments.Last();
        destBlob = destContainer.GetBlockBlobReference(name);
        await destBlob.StartCopyAsync(srcBlob);
        //remove source blob after copy is done.
        srcBlob.Delete();
        return destBlob;
    }

回答by RJardines

The answer accepted in this question will move the file to your server memory and then upload the file again to azure.

此问题中接受的答案会将文件移动到您的服务器内存,然后再次将文件上传到 azure。

It's better let the work to azure

不如让作品蔚蓝

CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();
    CloudBlobContainer sourceContainer = blobClient.GetContainerReference(SourceContainer);
    CloudBlobContainer targetContainer = blobClient.GetContainerReference(TargetContainer);

    CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(fileToMove);
    CloudBlockBlob targetBlob = targetContainer.GetBlockBlobReference(newFileName);
                    await targetBlob.StartCopyAsync(sourceBlob);