Skip to content

Instantly share code, notes, and snippets.

@jtubert
Created January 27, 2019 03:22
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtubert/177bc8d90f6251392162cdb3fdc7a8be to your computer and use it in GitHub Desktop.
Save jtubert/177bc8d90f6251392162cdb3fdc7a8be to your computer and use it in GitHub Desktop.
Upload photo to S3 from ios shortcut
const util = require('util');
const AWS = require('aws-sdk');
module.exports = function (context, cb) {
if(context.body && context.body.base64Image){
uploadFromShortcut(context, cb);
return;
}
};
async function uploadFromShortcut(context, cb){
if(!context.body.base64Image){
cb(null, { text: 'Error: you must pass a base64 encoded image as "base64Image"' });
return;
}
let path = "test/";
let filename = "test.jpg";
if(context.body.path){
path = context.body.path;
}
if(context.body.filename){
filename = context.body.filename;
}
const buf = new Buffer(context.body.base64Image, 'base64');
console.log(context.body.base64Image);
uploadBufferToS3(buf, path + filename, cb);
}
function uploadBufferToS3(buffer, filename, cb){
AWS.config.update({
accessKeyId: '{ENTER_HERE}',
secretAccessKey: '{ENTER_HERE}',
region: 'us-east-1'
});
// Create S3 service object
const s3 = new AWS.S3({apiVersion: '2006-03-01'});
// call S3 to retrieve upload file to specified bucket
let uploadParams = {Bucket: 'oneamerica-site', Key: '', Body: ''};
uploadParams.Body = buffer;
uploadParams.Key = filename;
// call S3 to retrieve upload file to specified bucket
s3.upload (uploadParams, function (err, data) {
if (err) {
cb(null, { text: 'Error: ' + err });
} if (data) {
console.log(data.Location);
cb(null, { text: data.Location });
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment