I am exploring how to download document from Amazon S3 and then save it as an attachment on a Xero Invoice. Can I have help on this task? I am referencing my code using these two links:
API | Link |
---|---|
createInvoiceAttachmentByFileName | https://xeroapi.github.io/xero-node/accounting/index.html#api-Accounting-createInvoiceAttachmentByFileName |
s3 getObject | https://www.tabnine.com/code/javascript/functions/aws-sdk/S3/getObject |
Error: I keep receiving this error while sending it to Xero.
TypeError: body.on is not a function
Main
const sendInvoicesResponse = await xeroService.sendDocument(
mappedDocumentData,
integration.tenant.id,
);
const downloadedDocument = await awsService.downloadDocument(document.key);
await xeroService.sendAttachmentToInvoice(
integration.tenant.id,
sendInvoicesResponse.body.invoices[0].invoiceID,
document.name,
downloadedDocument.data,
downloadedDocument.mimetype,
);
AWS Service
module.exports.downloadDocument = async (key) => {
try {
const getParams = {
Bucket: config.aws.documentsBucket, // your bucket name,
Key: key,
};
const file = await s3.getObject(getParams).promise();
return {
data: file.Body,
mimetype: file.ContentType,
};
} catch (error) {
logger.error(`download document from AWS fail | message=${message}`);
throw new Error(message);
}
};
In Xero
module.exports.sendAttachmentToInvoice = async (
tenantId,
invoiceId,
fileName,
body,
contentType,
includeOnline = true,
) => {
try {
return client.accountingApi.createInvoiceAttachmentByFileName(
tenantId,
invoiceId,
fileName,
body,
includeOnline,
{
headers: {
'Content-Type': contentType,
},
},
);
} catch (error) {
logger.error(`attaching file attachment to xero fail | message=${message}`);
throw new Error(message);
}
};