【问题标题】:Copy Azure block blob to Azure file share?将 Azure 块 blob 复制到 Azure 文件共享?
【发布时间】:2019-08-12 13:00:33
【问题描述】:

如何将 Azure 存储中的块(或页)blob 复制到 Azure 文件共享? 如果我将块 blob 下载到本地文件,我的示例代码可以正常工作,但似乎没有下载到 Azure 文件共享的方法。我查看了 Azure 数据移动库,但没有关于如何执行此操作的示例。

void Main()
{
    string myfile =@"Image267.png";

    CredentialEntity backupCredentials = Utils.GetBackupsCredentials();
    CloudStorageAccount backupAccount = new CloudStorageAccount(new StorageCredentials(backupCredentials.Name, backupCredentials.Key), true);
    CloudBlobClient backupClient = backupAccount.CreateCloudBlobClient();
    CloudBlobContainer backupContainer = backupClient.GetContainerReference(@"archive");
    CloudBlockBlob blob = backupContainer.GetBlockBlobReference(myfile);

    CredentialEntity fileCredentails = Utils.GetFileCredentials();
    CloudStorageAccount fileAccount = new CloudStorageAccount(new StorageCredentials(fileCredentails.Name,fileCredentails.Key), true);
    CloudFileClient fileClient =    fileAccount.CreateCloudFileClient();
    CloudFileShare share = fileClient.GetShareReference(@"xfer");

    if (share.Exists())
    {
        CloudFileDirectory rootDir = share.GetRootDirectoryReference();
        CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("hello");
        if (sampleDir.Exists())
        {
             CloudFile file = sampleDir.GetFileReference(myfile);
//           blob.DownloadToFile(file.ToString());
        }       
    }
}

不起作用的部分是注释掉的行blob.DownloadToFile

关于如何做到这一点的任何想法?

【问题讨论】:

    标签: azure azure-blob-storage


    【解决方案1】:

    官方文档here中有一个例子:它用于将文件从文件共享复制到blob存储,但您可以稍微更改一下从blob存储复制到文件共享。我还写了一个示例代码,用于从 blob 存储复制到文件共享,您可以查看如下。

    您可以使用 SAS 令牌(用于源 blob 或源文件)将文件复制到 blob/或将 blob 复制到同一存储帐户或不同存储帐户中的文件。

    示例代码如下(将blob复制到同一存储帐户中的文件,如果它们在不同的存储帐户中,您可以稍作更改):

    static void Main(string[] args)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection string");
    
        var blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference("t0201");
        CloudBlockBlob sourceCloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("test.txt");
    
        //Note that if the file share is in a different storage account, you should use CloudStorageAccount storageAccount2 = CloudStorageAccount.Parse("the other storage connection string"), then use storageAccount2 for the file share.
        CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
        CloudFileShare share = fileClient.GetShareReference("testfolder");
        CloudFile destFile = share.GetRootDirectoryReference().GetFileReference("test.txt");
        
        //Create a SAS for the source blob
        string blobSas = sourceCloudBlockBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
        {
            Permissions = SharedAccessBlobPermissions.Read,
            SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
        });
    
        Uri blobSasUri = new Uri(sourceCloudBlockBlob.StorageUri.PrimaryUri.ToString()+blobSas);
        destFile.StartCopy(blobSasUri);
    
        Console.WriteLine("done now");
        Console.ReadLine();
    }
    

    它在我身边效果很好,希望对你有帮助。

    【讨论】:

    • 谢谢伊万,这对我有用。我缺少的一点是在源 blob 上创建 SAS。
    • 我还需要在第二遍中将文件复制回 blob,原因是您必须将页面 blob 复制到文件然后作为块 blob 复制才能设置存档在 Azure 中分层以“存档”。
    • @Dave,因为它适用于将 blob 复制到文件共享,您能否将其标记为答案?谢谢。对于另一个问题,您能否提出一个新问题,并让我知道链接?谢谢。
    • 抱歉,我以为我已标记为答案。现在完成了,干杯。
    【解决方案2】:

    here 中有一些样本,但在 dot net 中。与代码无关的替代方法是使用Azcopy,它有助于将数据从 blob 传输到文件共享,反之亦然。

    外部建议: 以下名为Blobxfer 的工具似乎支持传输但在python 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-20
      • 2016-05-29
      • 2021-03-09
      • 2021-11-11
      • 2022-01-07
      • 2021-02-22
      • 1970-01-01
      • 2017-08-03
      相关资源
      最近更新 更多