2017-08-17 123 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

相關問題