1

我有一些文件位于 Azure 文件存储中。

我正在尝试以编程方式将它们存档到 Azure Blob,但我不确定如何有效地做到这一点。

我不断看到有关从一个 blob 容器复制到另一个 blob 容器的代码示例.. 但不是从File 复制到 Blob

是否可以不在本地下载整个文件内容然后上传此内容?也许使用 Uri 之类的?

更多信息:

  1. 文件和 Blob 容器位于同一个存储帐户中。
  2. 存储帐户是RA-GRS

这是我正在考虑做的一些示例代码..但感觉不对:((伪代码也是..省略了验证和检查)。

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);
}
4

2 回答 2

4

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

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

// 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) 对源对象进行身份验证,即使是在同一存储帐户中复制也是如此

于 2017-03-13T12:04:05.573 回答
1

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

有一些方法可以从 CloudFile 复制到 CloudBlob。

添加“Microsoft.Azure.Storage.DataMovement”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);
}
于 2017-08-16T13:18:26.833 回答