AWS C# lambda File upload to S3

2.2k Views Asked by At

I have created a Serverless for AWS using visual studio empty template. I am trying to send a file to it which internally gets uploaded to S3 using C#. I am able to upload the file through a console application. I need help on: a. how to send file to API through Insomnia or Postman -- able to do it now b. How the receive the file so that when I upload it S3 I am able to download it directly the way I sent in the API.-- able to do it now

[EDIT] c. When trying to save the file to bucket the file size is less than the uploaded and is corrupted.

Code Snippet:

public APIGatewayProxyResponse Get(APIGatewayProxyRequest request, ILambdaContext context)
{

context.Logger.LogLine(Encoding.ASCII.GetByteCount(request.Body).ToString()); MemoryStream ms = new MemoryStream(); TransferUtility utility = new TransferUtility(new AmazonS3Client("<AccessKey>", "<SecretKey>", Amazon.RegionEndpoint.USEast1)); var checker = new TransferUtilityUploadRequest() { InputStream = new MemoryStream(Encoding.ASCII.GetBytes(request.Body)), BucketName = "<BucketName>", Key = "<FileName>.pdf" }; utility.Upload(checker); var response = new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.OK, Body = JsonConvert.SerializeObject(checker), Headers = new Dictionary<string, string> { { "Content-Type", "application/json" }, { "Access-Control-Allow-Origin", "*" } } }; return response; }

Note: The file could be docx or pdf. Also I have the code to upload file stream to S3 Just need info on receiving the file through APIGatewayProxyRequest type and converting to stream.

Thanks in advance.

0

There are 0 best solutions below