2017-09-16 72 views
0

我写这是只读取第一个文件到我需要在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(); 
      } 
     } 
+2

'我的代码是不是取大file'是它抛出一个异常的斑点?还有别的吗? – mjwills

+0

有多个文件上的斑点,但第二文件大小是不是由代码提取33 MB,但另一个小文件被发送到SFTP小于1 MB。 – user8123152

+2

'不是由code'是什么意思进账? – mjwills

回答

1

请试试用下面的代码,它工作正常在我的身边。我测试容器中的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 
      } 
     } 
    } 

检查上传的命令

enter image description here