user8123152
user8123152

Reputation: 61

How to download blob file into memory stream?

I've written code which is fetching only first file into memory stream which I need to send on SFTP, so only first file is sent to SFTP & I've 3 files in blobs. here is my code.

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

Upvotes: 3

Views: 7809

Answers (1)

Tom Sun
Tom Sun

Reputation: 24529

Please have a try to use the following code,it works correctly on my side. I test the 4 blobs in the container, the blob constructs as following.

enter image description here

Demo code:

  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
            }
       }
  }

Check uploaded blobs from the command

enter image description here

Upvotes: 3

Related Questions