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

时间:2017-08-17 13:50:37

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

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

       $('select#opt2').change(function () {
        var stringArray = new Array();
        stringArray =$("#opt2>option").map(function() { return $(this).text(); }).get();
        var selectedValue = $('select#opt2').val();
        $.ajax({
            url: '@Url.Action("RetournerPostes", "Home", new { area = "Avion" })',
            data: {avion: selectedValue, types: stringArray},
            type: 'POST',
            dataType: 'JSON',
        //Rest Code

我们可以假设我还有一个blob存储的引用,以及我想要移动到文件存储的文件的uri。我怎么能这样做,最好不使用AzCopy,而是使用C#代码?

3 个答案:

答案 0 :(得分:2)

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

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

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

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

答案 1 :(得分:0)

  

引用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);

答案 2 :(得分:0)

我按照以下方式开始工作:

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。