Hi,
I’m trying to download a file from s3 and then upload it to another server. Below mentioned is my code. The file gets downloaded from s3 successfully but i guess the code breaks after i make call to 3rd party APIs which are in the separate function callNGUpload().
When i run this code offline or local invoke it runs perfectly but when i run it through endpoint after deploying it gives “internal server error”.
I think there is some issue related to proper response object return. I tried independent return at the end as well as callback method but nothing worked. I’m unable to find what exactly the problem is. Did someone face the same issue?
‘use strict’;
var AWS = require(‘aws-sdk’);
var s3 = new AWS.S3();
var fs = require(‘fs’);
var apiContext = require(‘mozu-node-sdk/clients/platform/application’)();
var documentResource = require(‘mozu-node-sdk/clients/content/documentlists/document’)(apiContext);
module.exports.imageUpload = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
console.log("Event : "+JSON.stringify(event.queryStringParameters.name));
var qryString = event.queryStringParameters.name;
// var original = qryString.split('-bdh-')[0];
// var newFileName = qryString.split('-bdh-')[1];
var original = event.queryStringParameters.original;
var newFileName = event.queryStringParameters.name;
console.log("Original : "+original);
console.log("New File : "+newFileName);
console.log("Params : "+JSON.stringify(event.queryStringParameters));
// var fileNm = "Capture1.JPG"
const params = {
Bucket: "kibocustomerfiles",
Key: original
}
var s3client = new AWS.S3({
accessKeyId:"AKIAXN3ILGE3LB7BDA44",
secretAccessKey:"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"
// accessKeyId:"AKIA4CBYWWYGDQ5VGFT5",
// secretAccessKey:"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"
});
s3client.getObject(params, (err, data) => {
if(err) {
console.log(err);
callback(null, err);
} else {
console.log("LENGTH : "+data.Body.length);
var img = data.Body;
console.log("Length : "+img.length);
callNGUpload(data.Body, newFileName, callback);
}
})
}
function callNGUpload(img, fileName) {
// img = fs.readFileSync(img);
// console.log("SIZE : "+img.length);
// console.log("Image : "+img);
documentResource.createDocument({
documentListName: 'files@mozu'
}, {
body:{
contentLength: img.length,
contentMimeType: "image/JPG",
documentTypeFQN: "image@mozu",
listFQN: "files@mozu",
name: fileName,
publishState: "Active"
}
}).then(data => {
console.log("DATA SOURCE");
return documentResource.updateDocumentContent({documentListName: "files@mozu", documentId: data.id}, {body: img, headers: {'Content-Type': 'image/JPG'}})
.then((result)=>{
console.log("Success !!");
console.log(result);
callback(null, "Successfully uploaded");
}).catch((err)=>{
console.log("Not Uploaded !!");
console.error(err);
callback(null, "Upload failed");
});
})
}