Azure 将文件从 Blob 存储移动到同一存储帐户中的文件存储



我需要传递一些文件,从 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;

现在,使用存储客户端 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();

了解更多信息:

使用 .Net 针对 Azure 文件存储进行开发

适用于 .NET 的存储客户端库参考 - MSDN

如果要将 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。

最新更新