Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
AWS Lambda C# Upload Object to S3

I have a Lambda function written in C# that is unsuccessfully attempting to upload an object to an S3 bucket. For testing purposes, I am converting the input string to a byte array and using that as the object contents. My handler function is defined below:

public void FunctionHandler(string input, ILambdaContext context)
{
    IAmazonS3 client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1);

    byte[] bytes = new byte[input.Length * sizeof(char)];
    Buffer.BlockCopy(input.ToCharArray(), 0, bytes, 0, bytes.Length);

    using (MemoryStream ms = new MemoryStream())
    {
        foreach (Byte b in bytes)
        {
            ms.WriteByte(b);
        }

        PutObjectRequest request = new PutObjectRequest()
        {
            BucketName = "BUCKET_NAME",
            Key = "OBJECT_KEY",
            InputStream = ms
        };

        client.PutObjectAsync(request);
    }
}

The function runs without error, but the object is not written to S3. I feel that it might have something to do with the PutObjectAsync method, but I'm not positive. The IAmazonS3 interface includes a PutObject method, but when attempting to use that method I receive the following error:

'IAmazonS3' does not contain a definition for 'PutObject'

What is the best way to upload an object to an S3 bucket in a C# Lambda function?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This is the helper function I use to put S3 objects to S3, from a C# lambda function (and it works). You might be able to use this as a starting point for yours.

Not sure why you are converting your string to bytes, and in C#/Lambda you need to use the PutObjectAsync method, not the PutObject method:

 public static async Task<bool> PutS3Object(string bucket, string key, string content)
        {
            try
            {
                using (var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1))
                {
                    var request = new PutObjectRequest
                    {
                        BucketName = bucket,
                        Key = key,
                        ContentBody = content
                    };
                    var response = await client.PutObjectAsync(request);
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in PutS3Object:" + ex.Message);
                return false;
            }
        }
about 4 years ago · Santiago Trujillo Report

0

You should apply await with the Async

await client.PutObjectAsync(request);

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!