Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const aws = require('aws-sdk');
- const DynamoDB = new aws.DynamoDB.DocumentClient({region: 'us-west-2'});
- const s3 = new aws.S3({ apiVersion: '2006-03-01' });
- var m = require('./methods.js');
- exports.handler = (event, context) => {
- // Get the object from the event and show its content type
- const bucket = event.Records[0].s3.bucket.name;
- const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
- const fileInfo = {
- Bucket: bucket,
- Key: key,
- };
- s3.getObject(fileInfo, (err, data) => {
- if (err) {
- console.log(err);
- const message = `Error getting object from bucket. Make sure they exist and your bucket is in the same region as this function.`;
- console.log(message);
- } else {
- var filesize = m.formatBytes(data.ContentLength);
- var params = {
- Item: {
- "FileName": {
- S: key
- },
- "FileSize": {
- S: filesize
- },
- "UploadedBy": {
- S: data.Metadata.uploadedby
- }
- },
- TableName: "FileMetaData"
- };
- DynamoDB.putItem(params, function(err, data) {
- if (err) console.log(err, err.stack); // an error occurred
- else console.log(data); // successful response
- });
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement