c# - 如何将blob文件下载到内存流中?

标签 c# azure

我编写的代码仅将第一个文件提取到内存流中,我需要在 SFTP 上发送该文件,因此只有第一个文件被发送到 SFTP 并且我有 3 个 blob 文件。 这是我的代码。

foreach (var blob in blobs)
{
    string str = blob.StorageUri.PrimaryUri.LocalPath;
    string fileName = blob.StorageUri.PrimaryUri.LocalPath.Replace("/output/ServiceNowExtract/", "");
    var blobPath = string.Format("{0}", blob.StorageUri.PrimaryUri.OriginalString);
    CloudBlockBlob blobSNow = container.GetBlockBlobReference(fileName.Replace(fileName, blob.StorageUri.PrimaryUri.LocalPath.Replace("/output/", "")));
    string ftpFilePathSNow = string.Format("{0}/{1}", ftpUploadPathSNow, fileName);
    var latestblob = container.ListBlobs();
    using (var stream = new MemoryStream())
    {

        // Downloading the blob containt to the memory stream
        blobSNow.DownloadToStream(stream);
        try
        {
            using (var client = new SftpClient(ftpConnectionSNow))
            {
                client.BufferSize = 999424;
                client.Connect();
                stream.Position = 0;
                client.UploadFile(stream, ftpFilePathSNow, true);
                client.Disconnect();
            }
        }

最佳答案

请尝试使用以下代码,它在我这边工作正常。我测试了容器中的 4 个 blob,blob 构造如下。

enter image description here

演示代码:

  var connectionString = "DefaultEndpointsProtocol=https;AccountName=accountxxxx;AccountKey=xxxxxxxxx;EndpointSuffix=core.windows.net";
  CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
  CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
  CloudBlobContainer container = blobClient.GetContainerReference("output");
  var blobs = container.ListBlobs();
  var ftpConnectionSNow = new ConnectionInfo("HostName", "username", new PasswordAuthenticationMethod("username","password"));
  const string ftpUploadPathSNow = "/home/xxx/sftptest4tom"; //sftp path
  foreach (var blob in blobs)
  {
       CloudBlockBlob blobSNow = (CloudBlockBlob) blob;
       var fileName = blobSNow.Name;
       Console.WriteLine($"BlobName:{fileName} ---BlobSize:{blobSNow.Properties.Length}");
       var ftpFilePathSNow = $"{ftpUploadPathSNow}/{fileName}";
       using (var stream = new MemoryStream())
       {
          // Downloading the blob containt to the memory stream
          blobSNow.DownloadToStream(stream);
          try
            {
               using (var client = new SftpClient(ftpConnectionSNow))
               {
                   client.BufferSize = 999424;
                   client.Connect();
                   stream.Position = 0;
                   client.UploadFile(stream, ftpFilePathSNow, true);
                   client.Disconnect();
                }
            }
            catch (Exception)
            {
                        // ToDo
            }
       }
  }

通过命令检查上传的 blob

enter image description here

关于c# - 如何将blob文件下载到内存流中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46253834/

相关文章:

c# - 为什么 .NET Math.Round 方法在处理负值时四舍五入而不是四舍五入?

c# - 在 C# 函数中生成 Linq 查询

javascript - 将查询参数从 HTML/JS 应用程序传递到 Azure 服务器脚本

angular - 如果我无法确定生产中需要什么重定向 URI,我该如何使用 OAuth2?

azure - 无法删除 AKS 群集

c# - 等待多个任务会比第一个异常观察到更多吗?

c# - 如何根据键值获取在文本文件中输入的记录

asp.net - 将asp.net core应用程序部署到azure后出错

C#对象池模式实现

java - 从 EventHostProcessor 中的最后一个检查点重新开始消费