![](/img/trans.png)
[英]Copy Specific files in a blob from a storage account to a different storage account
[英]Azure move files from blob storage to file storage in the same storage account
我需要將一些文件從Blob存儲傳遞到文件存儲。 這兩個存儲都在同一個存儲帳戶中。 在這里,我得到文件存儲:
var storage = GetStorageAccount(resourceGroup, storageName);
CloudFileClient fileClient = storage.CreateCloudFileClient();
CloudFileShare share = fileClient.GetShareReference(projectId.ToString());
我們可以假設我也有對Blob存儲的引用,以及我要移至文件存儲的文件的uri。 我該如何做到這一點,最好不使用AzCopy,而是從C#代碼中做到這一點?
您可以參考使用相同框架庫的代碼:
首先,包括您需要的類,這里我們包括存儲客戶端庫,存儲數據移動庫和.NET線程,因為數據移動庫提供了任務異步接口來傳輸存儲對象:
using System;
using System.Threading;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.DataMovement;
現在,使用存儲客戶端庫提供的接口來設置存儲上下文(在.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();
學到更多:
如果要將Blob復制到文件,或者將文件復制到Blob,則即使使用相同的存儲帳戶進行復制,也必須使用共享訪問簽名(SAS)對源對象進行身份驗證。
有對Blob存儲的引用,以及我要移至文件存儲的文件的uri
CloudFile類使我們能夠使用文件的絕對URI初始化CloudFile類的新實例。 您可以參考以下示例代碼將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);
我以以下方式工作:
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);
因此,您首先需要從要復制的Blob中獲取一個令牌 ,然后只需在目標文件存儲目錄中創建一個簡單文件,然后調用StartCopy。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.