Open fhackenb opened 5 years ago
My work around ended up being to follow this guide: https://www.youtube.com/watch?v=BrYJlR0yRnw and switch to this parse-multipart pr: https://github.com/freesoftwarefactory/parse-multipart/pull/7
Hi, @fhackenb I was facing same S3 uploading issue. Now I solved it. You can refer my repo Hope it'll help you.
I am facing the same issue. I can not for the life of me get s3 to load a pdf file. It keeps telling me the file can't be opened.
---- update
I was able to resolve this issue using this repo. https://github.com/dudewheresmycode/node-lambda-multipart
NOTE: If using api gateway + SAM FILES you may have to manually deploy the API for the binary media types to take effect.
Thanks @Wolfie010, I was about to pull out all my hairs on this one. Now everything is working as expected.
Before, fields are parsed but corrupted upload image. And when I set to binary for "multipart/form-data", the image works but no fields. I spend an entire day pulling out/losing some hairs on this one.
---- update Even though 'node-lambda-multipart' works, I ended up just using busboy: https://github.com/niiknow/lambda-form/blob/596e1a663faa01d028f2df16d454e68051b07884/lib/formparser.js#L39
Just reporting that 'busboy' module is also a good alternative.
Everyone, I figured out a workaround for S3:
exports.handler = function(event, context, callback) { console.log(event); let body = event.body; let decodedImage = Buffer.from(body, 'base64'); event.body = decodedImage.toString('latin1'); let data = multipart.parse(event, true); let response = { statusCode: 200, body: JSON.stringify(data) }; callback(null, response); };
I also emailed the owner so we can figure out how to incorporate this into the repo
I have solved this issue using binary
encoding to create Buffer
. The UTF-8
and base64
didn't worked for me.
This is my multipart event parsing function which worked for me which I have used with Busboy.
busboy.on('file', (_fieldname, file, filename, _encoding, mimetype) => {
const fileContent = {
content: null,
filename: null,
contentType: null
}
let chunks;
file.on('data', data => {
chunks = data;
});
file.on('end', () => {
const fileData = Buffer.from(chunks, 'binary')
fileContent.content = fileData;
fileContent.filename = filename;
fileContent.contentType = mimetype;
});
result.file = fileContent;
});
busboy.on('field', (fieldname, value) => {
result[fieldname] = value;
});
busboy.on('error', error => reject(error));
busboy.on('finish', () => {
event.body = result;
resolve(event);
});
busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'binary');
busboy.end();
Hi everybody,
This worked for me:
const { PutObjectCommand, S3Client } = require("@aws-sdk/client-s3"); const parser = require('lambda-multipart-parser');
const client = new S3Client({ region: "us-east-1" });
exports.handler = async (event) => {
if (event.isBase64Encoded) {
//decode base64 to binary
var buf = Buffer.from(event.body, 'base64');
event.body = buf;
}
const result = await parser.parse(event, true);
const command = new PutObjectCommand({
Bucket: "buket-name-here",
Key: "test.png",
Body: result.files[0].content,
ContentType: "image/png"
});
try {
const res = await client.send(command);
const response = {
statusCode: 200,
body: JSON.stringify({ "Message": "Success", "result": result.files[0].content }),
};
return response;
} catch (err) {
const response = {
statusCode: 200,
body: JSON.stringify({ "Message": "Fail", "result": err}),
};
return response;
}
};
Any progress or work arounds for the S3 uploading issue? I'm attempting to use this to upload to S3 and I am seeing the issue you mentioned (I hadn't seen that initially)