简体   繁体   English

如何复制Azure容器和Blob

[英]How to Copy Azure Containers & Blobs

I am attempting to copy all the blobs to different storage: 我正在尝试将所有Blob复制到不同的存储中:

CloudBlobClient srcblobClient = sourceStorageAccount.CreateCloudBlobClient();
CloudBlobClient targetBlobClient = targetStorageAccount.CreateCloudBlobClient();

foreach (CloudBlobContainer cont in srcblobClient.ListContainers())
{
    foreach (IListBlobItem srcBlob in cont.ListBlobs(useFlatBlobListing: true))
    {                        
        var targetContainer = targetBlobClient.GetContainerReference(cont.Name);
        targetContainer.CreateIfNotExists();

        Uri thisBlobUri = srcBlob.Uri;
        var serverBlob = srcblobClient.GetBlobReferenceFromServer(thisBlobUri);

        ICloudBlob targetBlob = targetContainer.GetBlobReferenceFromServer(serverBlob.Name);

        targetBlob.StartCopyFromBlob(thisBlobUri);
    }
}

I am able to see the listing of blobs & copy method is being invoked targetBlob.StartCopyFromBlob(thisBlobUri); 我能够看到blob列表和复制方法正在被调用targetBlob.StartCopyFromBlob(thisBlobUri);

However copy is not actually happening. 但是,复制实际上并没有发生。 Any Idea? 任何想法?

PS I am using Azure Storage SDK 4.3 & target storage is development storage. PS我正在使用Azure存储SDK 4.3,目标存储是开发存储。

EDIT 2: 编辑2:

For remote azure storage copy above code works fine. 对于远程天蓝色存储,复制上面的代码可以正常工作。

However for Emulated storage I get 400 BadRequest error, when trying to create container: targetContainer.CreateIfNotExists(); 但是对于模拟存储,尝试创建容器时出现400 BadRequest错误:targetContainer.CreateIfNotExists();

My emulated storage version is 3.0, it seems there is conflict between azure SDK & emulator version. 我的仿真存储版本是3.0,似乎azure SDK和仿真器版本之间存在冲突。

Which version of storage client library works well with storage emulator 3.0? 哪个版本的存储客户端库可以与存储模拟器3.0一起很好地工作?

The reason you're getting this error is indeed because of version mismatch. 您收到此错误的原因确实是由于版本不匹配。 If I am not mistaken, Storage Emulator version 3.0 uses REST API version 2013-08-15 where as the latest version of storage client library uses REST API version 2014-02-14 (Ref: http://msdn.microsoft.com/en-us/library/azure/dn744252.aspx ). 如果我没记错的话,Storage Emulator 3.0版将使用REST API版本2013-08-15 ,其中最新版本的存储客户端库将使用REST API版本2014-02-14 (Ref: http : //msdn.microsoft.com/ zh-CN / library / azure / dn744252.aspx )。 What you could is make use of an older version of storage client library. 您可能会利用旧版本的存储客户端库。 You can install appropriate version via Nuget. 您可以通过Nuget安装适当的版本。 For example, if you want to install Storage Client Library version 3.2.1, you can do so by doing the following: 例如,如果要安装Storage Client Library版本3.2.1,则可以执行以下操作:

Install-Package WindowsAzure.Storage -Version 3.2.1 (Ref: http://www.nuget.org/packages/WindowsAzure.Storage/3.2.1 ) 安装包WindowsAzure.Storage-版本3.2.1(参考: http ://www.nuget.org/packages/WindowsAzure.Storage/3.2.1)

Please try it out and see if that fixes the problem. 请尝试一下,看看是否可以解决问题。

Also looking at your code, I would also recommend some changes: 在查看您的代码时,我还建议一些更改:

  • I would not recommend changing the permission on Blob Container to Public . 我不建议将Blob容器的权限更改为Public It kind of exposes your blob storage and makes it available via anonymous access. 它公开了您的Blob存储并通过匿名访问使其可用。 What I would recommend is that you create SAS URLs with Read Permission on your source blobs and copy using those SAS URLs. 我建议您在源Blob上创建具有Read Permission SAS URL,并使用这些SAS URL复制。 Since the blob copy is asynchronous, I would recommend keeping the SAS URL valid for 7 days (maximum time allocated for copy operation). 由于Blob复制是异步的,因此我建议保持SAS URL有效7天(为复制操作分配的最大时间)。
  • I see that you're doing GetBlobReferenceFromServer on both source blob and target blob. 我看到您在源Blob和目标Blob上都在执行GetBlobReferenceFromServer This method is not recommended for source blob as it actually makes a network call thus for each blob you already got through listing. 不建议对源Blob使用此方法,因为它实际上会进行网络调用,因此对于您已经通过列表获得的每个Blob。 It is not recommended on target blob because this method will throw a Not Found (404) if your target blob does not exist. 不建议在目标Blob上使用,因为如果目标Blob不存在,则此方法将抛出Not Found (404)

Instead what I would recommend is that you cast the blobs you got through listing into appropriate blob type (Block or Page) and then get SAS URL. 相反,我建议您将通过列表获得的Blob转换为适当的Blob类型(块或页面),然后获取SAS URL。 If you know that all of your blobs are block blobs, you can simply cast them into CloudBlockBlob object without worrying about the casting. 如果您知道所有Blob都是块Blob,则只需将它们强制转换为CloudBlockBlob对象,而不必担心强制转换。

One thing I am not sure of is how page blobs will get copied. 我不确定的一件事是如何复制页面Blob。 When you're copying between storage accounts, a page blob gets copied as a page blob. 在存储帐户之间进行复制时,页面Blob将作为页面Blob复制。 However I have not tried copying from a storage account to development storage account. 但是,我还没有尝试从存储帐户复制到开发存储帐户。 But if you don't have page blobs, you don't have to worry about it :). 但是,如果您没有页面Blob,则不必担心:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM