Pradeep Singh

Pradeep Singh

  • NA
  • 11
  • 767

Trying to send zip or Excel files on SFTP using Lambda from S3

Jan 17 2023 7:22 PM

Hi,

I am new to AWS and Lambda and trying to copy S3 object like zip or Excel(txt/csv working) files from S3 bucket on S3 trigger and need to send those files on SFTP server. But the issue is when zip or excel files when recieved on SFTP server not getting open and showing currupted. Same functinon working on Onprem enviroment.

Code I am trying is below, Can someone help me how to convert Zip/Excel files in correct strem and SFTP.

var s3Event = evnt.Records?[0].S3;
if (s3Event == null)
{
    return "NoBucketFound";
}
try
{
    String[] k = s3Event.Object.Key.Split("/");
    GetObjectResponse response = new GetObjectResponse();
    
    response = await this.S3Client.GetObjectAsync(s3Event.Bucket.Name, s3Event.Object.Key);
    StreamReader reader = new StreamReader(response.ResponseStream);
    
    var content = reader.ReadToEnd();
    // convert string to stream
    byte[] byteArray = Encoding.Default.GetBytes(content);
    MemoryStream stream = new MemoryStream(byteArray);
   Console.Write("Hi...this is a Content of files : " + content);
    using (SftpClient sftp = new SftpClient("sftpserver",22, "Test", "test"))
    {
        sftp.Connect();
        if (sftp.IsConnected)
        {
            try
            {                                
                    sftp.BufferSize = 4 * 1024;
                    sftp.UploadFile(stream, "/"+ s3Event.Object.Key.ToString(), true, null);
                    stream.Dispose();
                    Console.Write("SFTP RUN" + s3Event.Object.Key);
                
            }
            catch (Exception exp)
            {
                Console.Write(exp.Message);
            }
        }
        sftp.Disconnect();
    }

    return response.ToString();
}
catch (Exception e)
{
    context.Logger.LogLine($"Error getting object {s3Event.Object.Key} from bucket {s3Event.Bucket.Name}. Make sure they exist and your bucket is in the same region as this function.");
    context.Logger.LogLine(e.Message);
    context.Logger.LogLine(e.StackTrace);
    throw;
}

Answers (2)