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;
}
Pradeep SinghPosted Jan 19, 2023, 10:20 AM
I tryed this elarier StreamReader reader = new StreamReader(response.ResponseStream); , but this not write the data on SFTP file( 0KB )
Lokesh VarmanPosted Jan 19, 2023, 3:38 AM
It looks like the issue with the code you provided is that you are reading the S3 object's contents into a string using the StreamReader class, and then converting that string back into a byte array using the Encoding.Default.GetBytes method. When you upload the byte array to SFTP using the UploadFile method, the file may be getting corrupted.
Instead of reading the S3 object's contents into a string, you should directly read the object's contents into a stream, and then pass that stream to the SFTP client's UploadFile method. Here's an example of how you can do that:
Also, you need to check the permissions of the SFTP server and the file name that you are providing as the second parameter of the
UploadFilemethod. Also, it's always better to use the try catch block to check the exact exception.Please keep in mind that this is just a sample of how you can modify your existing code, and it may require further adjustments to work in your specific use case.