3

I am trying to save a file downloaded from an SFTP site using the WinSCP .NET library, and then save it Azure Blob storage. I don't want to use CloudBlobContainer as it is deprecated. I keep getting the error message:

Offsets with value of non-zero are not supported when executing .... await blobClient.UploadAsync

using WinSCP;
using Microsoft.Azure.Storage.Blob;

BlobServiceClient blobServiceClient = new BlobServiceClient(_connectionString);
BlobContainerClient blobContainerClient =
    blobServiceClient.GetBlobContainerClient(containername);
BlobClient blobClient = blobContainerClient.GetBlobClient(filename);

using (Session session = new Session())
{
    session.Open(sessionOptions);
    string remotePath = "/myfile.zip";
    using (System.IO.Stream stream = session.GetFile(remotePath, transferOptions))
    {
        await blobClient.UploadAsync(stream,new BlobHttpHeaders
        {
            ContentType = "application/zip"
        }
    );
};

I am able to write the stream to disk.

using (var fileStream = new FileStream(newfile, FileMode.Create, FileAccess.Write))
{
   stream.CopyTo(fileStream);
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
laney
  • 57
  • 9

1 Answers1

2

Indeed, the WinSCP .NET assembly stream implementation in earlier releases was not compatible with Azure Blob API.

This is solved in WinSCP 6.1. Please upgrade.


Old answer, before the fix was released: You can work it around by copying the SFTP file to temporary in-memory buffer:

using (var stream = session.GetFile(remotePath))
using (var memoryStream = new MemoryStream())
{
    stream.CopyTo(memoryStream);
    memoryStream.Position = 0;

    await blobClient.UploadAsync(memoryStream, new BlobHttpHeaders
        {
            ContentType = "application/zip"
        }
    );
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992