Skip to content

Instantly share code, notes, and snippets.

@rjaus
Created February 14, 2018 01:14
Show Gist options
  • Save rjaus/0d17903b11d9202fc7cabcd78137504b to your computer and use it in GitHub Desktop.
Save rjaus/0d17903b11d9202fc7cabcd78137504b to your computer and use it in GitHub Desktop.
Read s3 bucket object and upload to Xero as attachment example code
// AWS s3
const aws = require('aws-sdk');
aws.config.loadFromPath('./aws.json');
aws.config.update({region:'us-east-1'});
const s3 = new aws.S3();
// Xero client
const xero = require('xero-node')
const fs = require('fs')
const config = require('./xero.json')
// Setup our Xero SDK (Private App Type)
if (config.privateKeyPath && !config.privateKey)
config.privateKey = fs.readFileSync(config.privateKeyPath);
const xeroClient = new xero.PrivateApplication(config);
var data = {
FileName: "test.pdf",
MimeType: "application/pdf"
};
var attachmentObj = xeroClient.core.attachments.newAttachment(data);
var sampleInvoiceID = "514f4038-412c-4cc7-9d91-6f12a646bb1a";
let rStream = s3.getObject({Bucket: 'xero-invoices-api-node', Key: 'test.pdf'})
.createReadStream()
console.log(rStream)
// Retrieve some entity to add the attachment to (e.g. Invoices)
xeroClient.core.invoices.getInvoice(sampleInvoiceID)
.then(function(invoice){
console.log(invoice)
attachmentObj.save("Invoices/" + invoice.InvoiceID, rStream, true)
.then(function(attachments){
//Attachment has been created
var myAttachment = attachments.entities[0];
console.log(myAttachment)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment