2017-08-17 119 views
0

我需要将一些文件从blob存储传递到文件存储。这两个存储都位于同一个存储帐户中。在这里,我得到文件存储:Azure将文件从blob存储移动到同一存储帐户中的文件存储

var storage = GetStorageAccount(resourceGroup, storageName); 
CloudFileClient fileClient = storage.CreateCloudFileClient(); 
CloudFileShare share = fileClient.GetShareReference(projectId.ToString()); 

,我们可以假设,我也有一个参考Blob存储,以及我想移动到文件存储的文件的URI。我怎么能做到这一点,最好不使用AzCopy,但从C#代码做到这一点?

回答

0

我得到它通过以下方式工作:

CloudBlobClient blobClient = storage.CreateCloudBlobClient(); 
CloudBlobContainer container = blobClient.GetContainerReference("powershellscripts"); 
var blockBlob = container.GetBlockBlobReference("WriteToFile.ps1"); 

SharedAccessBlobPolicy adHocSAS = new SharedAccessBlobPolicy() 
     { 
      // When the start time for the SAS is omitted, the start time is assumed to be the time when the storage service receives the request. 
      // Omitting the start time for a SAS that is effective immediately helps to avoid clock skew. 
      SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24), 
      Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Create 
     }; 
string sasBlobToken = blockBlob.GetSharedAccessSignature(adHocSAS); 
// Create a new file in your target file storage directory 
CloudFile sourceFile = share.GetRootDirectoryReference().GetFileReference("MyExample.ps1"); 

Uri fileSasUri = new Uri(blockBlob.StorageUri.PrimaryUri.ToString() + sasBlobToken); 
await sourceFile.StartCopyAsync(blockBlob); 

所以,你首先需要从要复制团块得到令牌,然后只需创建一个简单的文件您的目标文件存储目录,并调用StartCopy。

2

你可能指的是使用相同的框架库代码:

首先,包括你所需要的课程,我们在这里包括存储客户端库,存储数据的移动图书馆和.NET线程,因为数据移动图书馆提供任务异步接口传输存储对象:

using System; 
using System.Threading; 
using Microsoft.WindowsAzure.Storage; 
using Microsoft.WindowsAzure.Storage.Blob; 
using Microsoft.WindowsAzure.Storage.DataMovement; 

现在使用的存储客户端的lib提供设置存储上下文(找到如何从.NET使用Blob存储更多细节)接口:

string storageConnectionString = "myStorageConnectionString"; 
CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString); 
CloudBlobClient blobClient = account.CreateCloudBlobClient(); 
CloudBlobContainer blobContainer = blobClient.GetContainerReference("mycontainer"); 
blobContainer.CreateIfNotExists(); 
string sourcePath = "path\\to\\test.txt"; 
CloudBlockBlob destBlob = blobContainer.GetBlockBlobReference("myblob"); 

一旦你设置存储BLOB情况下,你就可以开始使用WindowsAzure.Storage.DataMovement.TransferManager上传blob和跟踪上传进度,

// Setup the number of the concurrent operations 
TransferManager.Configurations.ParallelOperations = 64; 
// Setup the transfer context and track the upoload progress 
SingleTransferContext context = new SingleTransferContext(); 
context.ProgressHandler = new Progress<TransferStatus>((progress) => 
{ 
    Console.WriteLine("Bytes uploaded: {0}", progress.BytesTransferred); 
}); 
// Upload a local blob 
var task = TransferManager.UploadAsync(
    sourcePath, destBlob, null, context, CancellationToken.None); 
task.Wait(); 

了解更多:

​​

Storage Client Library Reference for .NET - MSDN

如果要复制一个blob到一个文件或文件到一个blob,则必须使用一个共享访问签名(SAS)来验证源对象,即使您在同一个存储帐户中进行复制。

0

有参考Blob存储,以及该文件的URI我想移动到文件存储

CloudFile class使我们使用初始化CloudFile类的新实例该文件的绝对URI。您可以参考以下示例代码将blob复制到Azure文件。

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("{connection_string}"); 

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 

CloudBlobContainer container = blobClient.GetContainerReference("mycontainer"); 

CloudBlockBlob blockBlob = container.GetBlockBlobReference("source.txt"); 
blockBlob.OpenRead();   

CloudFile desfile = new CloudFile(new Uri("https://{account_name}.file.core.windows.net/myfiles/des.txt"), new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("{sasToken}")); 

desfile.StartCopy(blockBlob); 
+0

我在最后一行得到一个404没有找到的异常,但是这个文件肯定存在。 – Iason

相关问题