Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Azure将文件从blob存储移动到同一存储帐户中的文件存储_C#_.net_Azure_Cloud_Azure Storage Blobs - Fatal编程技术网

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

C# Azure将文件从blob存储移动到同一存储帐户中的文件存储,c#,.net,azure,cloud,azure-storage-blobs,C#,.net,Azure,Cloud,Azure Storage Blobs,我需要传递一些文件,从blob存储到文件存储。这两个存储都在同一个存储帐户中。这里我得到了文件存储: var storage = GetStorageAccount(resourceGroup, storageName); CloudFileClient fileClient = storage.CreateCloudFileClient(); CloudFileShare share = fileClient.GetShareReference(projectId.ToString())

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

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

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

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

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

using System;
using System.Threading;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.DataMovement;
现在,使用存储客户端库提供的接口来设置存储上下文(有关如何从.NET使用Blob存储的更多详细信息,请参阅):

设置存储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();
//设置并发操作的数量
TransferManager.Configurations.ParallelOperations=64;
//设置传输上下文并跟踪upoload进度
SingleTransferContext=新的SingleTransferContext();
context.ProgressHandler=新进度((进度)=>
{
WriteLine(“上传的字节:{0}”,progress.ByTestTransfered);
});
//上载本地blob
var task=TransferManager.UploadAsync(
sourcePath、destBlob、null、上下文、CancellationToken.None);
task.Wait();
了解更多信息:


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

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

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

using System;
using System.Threading;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.DataMovement;
现在,使用存储客户端库提供的接口来设置存储上下文(有关如何从.NET使用Blob存储的更多详细信息,请参阅):

设置存储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();
//设置并发操作的数量
TransferManager.Configurations.ParallelOperations=64;
//设置传输上下文并跟踪upoload进度
SingleTransferContext=新的SingleTransferContext();
context.ProgressHandler=新进度((进度)=>
{
WriteLine(“上传的字节:{0}”,progress.ByTestTransfered);
});
//上载本地blob
var task=TransferManager.UploadAsync(
sourcePath、destBlob、null、上下文、CancellationToken.None);
task.Wait();
了解更多信息:

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

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

使我们能够使用文件的绝对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);
有一个对blob存储的引用,以及我要移动到文件存储的文件的uri

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

我通过以下方式使其工作:

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。

我通过以下方式使其工作:

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。

我在最后一行得到一个404未找到异常,但该文件肯定在那里。我在最后一行得到一个404未找到异常,但该文件肯定在那里。