Skip to content

Instantly share code, notes, and snippets.

@Udith
Created June 17, 2020 07:41
Show Gist options
  • Save Udith/ad45039bb50adf1a0ef08e242a1735ee to your computer and use it in GitHub Desktop.
Save Udith/ad45039bb50adf1a0ef08e242a1735ee to your computer and use it in GitHub Desktop.
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
let mime = require('mime-types')
exports.handler = async (event) => {
console.log("Request received");
// Extract file content
let fileContent = event.isBase64Encoded ? Buffer.from(event.body, 'base64') : event.body;
// Generate file name from current timestamp
let fileName = `${Date.now()}`;
// Determine file extension
let contentType = event.headers['content-type'] || event.headers['Content-Type'];
let extension = contentType ? mime.extension(contentType) : '';
let fullFileName = extension ? `${fileName}.${extension}` : fileName;
// Upload the file to S3
try {
let data = await s3.putObject({
Bucket: "file-upload-bucket-udith",
Key: fullFileName,
Body: fileContent,
Metadata: {}
}).promise();
console.log("Successfully uploaded file", fullFileName);
return "Successfully uploaded";
} catch (err) {
console.log("Failed to upload file", fullFileName, err);
throw err;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment