The same sharp code runs fine if/when deployed to EC2 instance and run as node service.
The serverless deployment on AWS looks like sharp was installed fine along with libs, here are the lines from the install logs :
Downloading https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-linux-x86_64.tar.bz2
52 | Saving to /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2
53 | Receiving...
54 | 聽
55 | Received 22866K total.
56 | Extracting tar contents (via spawned process)
57 | Removing /codebuild/output/src921745175/src/node_modules/phantomjs-prebuilt/lib/phantom
58 | Copying extracted folder /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1581967411800/phantomjs-2.1.1-linux-x86_64 -> /codebuild/output/src921745175/src/node_modules/phantomjs-prebuilt/lib/phantom
59 | Writing location.js file
60 | Done. Phantomjs binary available at /codebuild/output/src921745175/src/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs
61 | 聽
62 | > [email protected] install /codebuild/output/src921745175/src/node_modules/sharp
63 | > (node install/libvips && node install/dll-copy && prebuild-install) \|\| (node-gyp rebuild && node install/dll-copy)
64 | 聽
65 | info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.9.0/libvips-8.9.0-linux-x64.tar.gz
66 | added 372 packages from 388 contributors and audited 1853 packages in 10.96s
INFO SHARP ERROR : Error: Input buffer contains unsupported image format
Are you able to provide a standalone code sample, without other dependencies, that demonstrates this problem?
Are you able to provide a sample image that helps explain the problem?
I've tried by uploading more than 10 different images and types (png, jpg, webp) with the same result with sharp on lambda, all of this still works fine on EC2 node web server.
CLIENT code from React form:
//upload button handler
const changeImage1 = (e) => {
const reader = new FileReader();
const file1 = e.target.files[0];
setFile(file1);
}
// save button
const saveImage1 = () => {
const fd1 = new FormData();
fd1.append("image", file1);
fd1.append("imageType",file1.type);
fd1.append("name", file1.name);
fd1.append("whichResident", 1);
fd1.append("residentId", props.resident._id);
props.postResidentImage(fd1); // superagent function below
}
SUPERAGENT api call to server:
export const postResidentImage = (imagePayload) => (dispatch, getState) => {
return new Promise(function (fulfill, reject) {
const dataUrl = ${URLS.API.POST_RESIDENT_IMAGE};
request.post(dataUrl)
.send(imagePayload)
.set('Accept', 'application/json')
.end(function(err, response) {
if(err || !response.ok) {
console.error(dataUrl, 'Error', err.toString());
} else {
console.log(response);
});
});
}
SERVER FUNCTION ::
postResidentImage = function (formData) {
const residentId = formData.residentId;
const whichResident = formData.whichResident;
const imageIn = formData.image;
const FName = ${residentId}_${whichResident}.webp;
return new Promise(function (fulfill, reject) {
sharp(imageIn)
.resize(90,90, {
fit: sharp.fit.inside,
withoutEnlargement: true
})
.webp()
.toBuffer()
.then(buf => {
AWSUtils.writeAssetS3(buf, 'rImages', FName)
.then(r => {
fulfill(r);
})
})
....
Are you also using AWS API Gateway? If so, have you read all the AWS docs about content type conversion and implemented these correctly?
Yes, I am using the api gateway.... let me take a look at the doc.
Added multipart/form-data, image/jpeg, image/png to binaryMedia types and added the Accept : multipart/form-data on the request since its a formData post. Everything works except the sharp function, same error about unsupported image format.
Bypassed API Gateway and used the lambda api endpoint, got same error from Sharp.
Okay, instead of dealing with the apiGateway and lambda binary drama, I changed the formData image to base64, and changed a line in the lambda function to....
const img = Buffer.from(formData.b64img, 'base64') and send that to sharp.
Working fine now.
@Cybernetixs can you share your full code?
Most helpful comment
Okay, instead of dealing with the apiGateway and lambda binary drama, I changed the formData image to base64, and changed a line in the lambda function to....
const img = Buffer.from(formData.b64img, 'base64') and send that to sharp.
Working fine now.