如何将Azure文件复制到Azure Blob?

时间:2017-03-13 11:11:58

标签: azure azure-storage-blobs azure-storage-files

我有一些文件存放在Azure文件存储中。

我正在尝试以编程方式将它们归档到Azure Blob,而我不确定如何有效地

我一直看到code samples关于从一个blob容器复制到另一个blob容器......但不是从文件复制到Blob

是否可以不在本地下载整个文件内容然后上传此内容?也许使用Uri或其他什么?

更多信息:

  1. 文件和Blob容器位于同一个存储帐户中。
  2. 存储帐户为RA-GRS
  3. 以下是一些示例代码我思考正在做...但它感觉不对:((伪代码也是..带有验证和检查省略)。

    var file = await ShareRootDirectory.GetFileReference(fileName);
    using (var stream = new MemoryStream())
    {
        await file.DownloadToStreamAsync(stream);
    
        // Custom method that basically does:
        // 1. GetBlockBlobReference
        // 2. UploadFromStreamAsync
        await cloudBlob.AddItemAsync("some-container", stream);
    }
    

2 个答案:

答案 0 :(得分:2)

  

如何将Azure文件复制到Azure Blob?

我们也可以使用CloudBlockBlob.StartCopy(CloudFile)。 CloudBlockBlob.StartCopy函数也可以接受CloudFile类型。 如何将CloudFile复制到blob请参考document。以下演示代码是文档中的代码段。

// Parse the connection string for the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    Microsoft.Azure.CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create a CloudFileClient object for credentialed access to File storage.
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

// Create a new file share, if it does not already exist.
CloudFileShare share = fileClient.GetShareReference("sample-share");
share.CreateIfNotExists();

// Create a new file in the root directory.
CloudFile sourceFile = share.GetRootDirectoryReference().GetFileReference("sample-file.txt");
sourceFile.UploadText("A sample file in the root directory.");

// Get a reference to the blob to which the file will be copied.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("sample-container");
container.CreateIfNotExists();
CloudBlockBlob destBlob = container.GetBlockBlobReference("sample-blob.txt");

// Create a SAS for the file that's valid for 24 hours.
// Note that when you are copying a file to a blob, or a blob to a file, you must use a SAS
// to authenticate access to the source object, even if you are copying within the same
// storage account.
string fileSas = sourceFile.GetSharedAccessSignature(new SharedAccessFilePolicy()
{
    // Only read permissions are required for the source file.
    Permissions = SharedAccessFilePermissions.Read,
    SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
});

// Construct the URI to the source file, including the SAS token.
Uri fileSasUri = new Uri(sourceFile.StorageUri.PrimaryUri.ToString() + fileSas);

// Copy the file to the blob.
destBlob.StartCopy(fileSasUri);

注意:

  

如果要将blob复制到文件或将文件复制到blob,必须使用共享访问签名(SAS)来验证源对象,即使您在同一存储帐户中进行复制< /强>

答案 1 :(得分:1)

使用转移管理器: https://msdn.microsoft.com/en-us/library/azure/microsoft.windowsazure.storage.datamovement.transfermanager_methods.aspx

有从CloudFile复制到CloudBlob的方法。

添加&#34; Microsoft.Azure.Storage.DataMovement&#34; nuget包

using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.File;
using Microsoft.WindowsAzure.Storage.DataMovement;

private string _storageConnectionString = "your_connection_string_here";

public async Task CopyFileToBlob(string blobContainer, string blobPath, string fileShare, string fileName)
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_connectionString);
    CloudFileShare cloudFileShare = storageAccount.CreateCloudFileClient().GetShareReference(fileShare);
    CloudFile source = cloudFileShare.GetRootDirectoryReference().GetFileReference(fileName);

    CloudBlobContainer blobContainer = storageAccount.CreateCloudBlobClient().GetContainerReference(blobContainer);
    CloudBlockBlob target = blobContainer.GetBlockBlobReference(blobPath);

    await TransferManager.CopyAsync(source, target, true);
}