Bri
Bri

Reputation: 396

Silent Failure of S3 PutObject?

I have been trying to programmatically upload SNS messages to an S3 bucket using the S3.Object.put() method like so:

bucket_resource=boto3.resource('s3')
bucket_client = boto3.client('s3')
body = subject + message
object = bucket_resource.Object(bucket_name, folder+'/'+fn)
object.put(Body=body)

This has not worked, so I have tried the following to try and upload an object to a particular S3 bucket.

body = subject + message
folder = datetime.datetime.today().strftime('%Y-%m-%d')
fn = datetime.datetime.today().strftime('%H:%M:%S')
key = folder_name + '/' + fn
bucket_resource = s3.Bucket(bucket_name)
bucket.upload_file(body, key)

However, both of these methods are failing silently. I am not getting any access denials, error messages, etc. but I am also not uploading my message to a bucket. I'm not sure what's happening with each invocation of the function, and would appreciate any guidance for people who have successfully uploaded files to buckets programmatically.

Note: I have bucket policies in place where my account is the only account that can put objects in the bucket. Do I need an addendum to give Lambda permission to put objects in the bucket?

Upvotes: 2

Views: 6248

Answers (3)

Jatin Verma
Jatin Verma

Reputation: 73

I have not used python, but I faced a similar issue where my react app crashes when I do s3.upload(). The following answer helped me solve it. https://stackoverflow.com/a/57139331/5295723

I had to convert the base64 image buffer to binary and it worked.

Upvotes: 0

bluiska
bluiska

Reputation: 369

There was no error for me either but I'm using the .NET Core SDK. Turns out my problem was the function finishing before it was able to put the file in S3. In the .NET SDK the PutObject call is Asynchronous (PutObjectAsync) and even though I was waiting for it to finish, turns out I wasn't doing that correctly due to the layers of functions I had.

For anyone struggling with no errors and no files in S3, check that your function isn't finishing before the file is actually uploaded to S3.

Upvotes: 3

Deiv
Deiv

Reputation: 3097

See here on handling errors in python.

Basically try/catch exception around sdk call, something along the lines of:

import boto3
from botocore.exceptions import ClientError, ParamValidationError

try:
    bucket_resource=boto3.resource('s3')
    bucket_client = boto3.client('s3')
    body = subject + message
    object = bucket_resource.Object(bucket_name, folder+'/'+fn)
    object.put(Body=body)
except s3.exceptions.<fill in>:
    print("known error occured")
except ClientError as e:
    print("Unexpected error: %s" % e)

This is the link the original poster provided, and it shows possible exceptions you can catch for an S3 client

Upvotes: -1

Related Questions