amazon-s3aws-api-gatewayserverlessserverless-offline

putObject upload broken files on S3 only when its by API


I have a problem when i try to upload a file on S3 through my API

I use the "putObject" method, and the thing that surprise me it's it work when i run my serverless application locally, with serverless-offline, i can push the all file on s3 and i can open it

But when i deploy my application on API Gateway, if i use the API Gateway route, the file is lightweight than original, and i can't open it, it tell me that the file is corrupted

If anyone has an idea, it could really help me

Thanks

My putObject method looks like this

const bucketName = _.get(getBucket,'bucketName');
        const extension = _.get(data,'media.filename').split('.').pop();
        const keyName = _.get(data,'keyName') + '.' + extension;
        const content = _.get(data,'media.content')
        
        let params = {
            Bucket: bucketName,
            Key: keyName,
            ContentType: _.get(data,'media.contentType'),
            Body: content,
            ACL: 'public-read'
        };

        return new Promise((resolve, reject) => {
        s3.putObject(params, function(err, data) {
            err
                ?
                reject(console.log(err)) :
                resolve(
                    response(200,"Object Added")
                );
        });
        });

Solution

  • Uploading files via ApiGateway is not a good idea, I'd strongly advise using resigned URL https://docs.aws.amazon.com/AmazonS3/latest/dev/PresignedUrlUploadObject.html

    If you want to upload through ApiGateway, than provided information is not enough. Try to log the received lambda event, that should help