Put object to s3 error from serverless

Hi, I am writing API to upload image to s3 with serverless framework. I can successfully upload image to s3 bucket but when I open it, it show like "

. So I tried to upload by using the same code(uploading parts) in normal node js environment, it works. What would be the problem? At the current stage I give my bucket to public read and write access. Do I need to specify security detail in the code.

This is the code that I used to upload image to s3.

s3.putObject({
//Body: data[‘base64’],
Body: imageCode,
Key: ‘testing84.jpg’,
Bucket: bucketName,
//ACL: “public-read-write”,
ContentType: “image/jpeg”

    //ContentType: "text/plain"

},function(error, data){
        callback(null, {
            statusCode: 501,
            headers: { 'Content-Type': 'text/plain' },
            body: 'Couldn\'t create the type item.',
        });
        return;})
callback(null, {statusCode: 200, headers: corsHeaders, body: 'ok'})

};

Many thanks,
Kind Regards,
John

Finally, I can solve the problem. The problem is that I need to give binary data inside Body of s3.putObject function. :slight_smile:

Cheers,
John

You may also want to consider making AWS S3 presigned URLs in a lambda function and then pass those URLs back to the user app. Doing it that way can be more secure since the user can’t mess with the key and bucket name.

Also check out AWS Amplify which has built in support for uploading images.

Thanks @jonsmirl I will check aws-amplify.
Cheers,
John

could you share the detail codes to fix your issue?

Thanks.

Hi bill, I tried to send base64, that is turned in client side, code of image in JSON rather than actual binary image file and send it to lambda. Then the 64 code is turned back to binary data (let binary = new Buffer[‘base64’], ‘base64’) and put it inside s3’s putObject function(Body: binary). That’s it.

Cheers,
Bill

Still not sure if I fully understand without see the real updates from your original codes. , but anyway, thanks a lot.

Bill, he first passed image in base64 format as which he had shown in figure and that caused the problem, later he changed that base64 to binary format and passed… finally that solved the problem

problem occurred at Body: data[‘base64’],
solved at Body: imageCode,

1 Like

I am having issues while uploading images. Could you please help me on it