【问题标题】:Azure move files from blob storage to file storage in the same storage accountAzure 将文件从 blob 存储移动到同一存储帐户中的文件存储
【发布时间】:2018-01-25 23:30:36
【问题描述】:

我需要传递一些文件,从 blob 存储到文件存储。这两个存储都在同一个存储帐户中。这里我得到 file 存储:

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

我们可以假设我也有对 blob 存储的引用,以及我想要移动到文件存储的文件的 uri。我该如何做到这一点,最好不使用 AzCopy,而是从 C# 代码中做到这一点?

【问题讨论】:

    标签: c# .net azure cloud azure-blob-storage


    【解决方案1】:

    您可以参考使用相同框架库的代码:

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

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

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

    了解更多:

    Develop for Azure File storage with .Net

    Storage Client Library Reference for .NET - MSDN

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

    【讨论】:

      【解决方案2】:

      有对 blob 存储的引用,以及我要移动到文件存储的文件的 uri

      CloudFile class 使我们能够使用文件的绝对 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);
      

      【讨论】:

      • 我在最后一行得到一个 404 not found 异常,但文件肯定在那里。
      【解决方案3】:

      我让它通过以下方式工作:

      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。

      【讨论】:

        猜你喜欢
        • 2017-08-18
        • 2021-11-28
        • 2016-11-25
        • 2018-07-13
        • 2017-01-19
        • 2020-08-25
        • 2021-02-22
        • 1970-01-01
        • 2012-05-20
        相关资源
        最近更新 更多