Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. console.log('Loading function');
  2.  
  3. const aws = require('aws-sdk');
  4.  
  5. const s3 = new aws.S3({ apiVersion: '2006-03-01' });
  6.  
  7.  
  8. exports.handler = async (event, context) => {
  9. console.log('Received event:', JSON.stringify(event, null, 2));
  10.  
  11. // Get the object from the event and show its content type
  12. const bucket = event.Records[0].s3.bucket.name;
  13. const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
  14. const params = {
  15. Bucket: bucket,
  16. Key: key,
  17. };
  18. try {
  19. const { ContentType } = await s3.getObject(params).promise();
  20. console.log('CONTENT TYPE:', ContentType);
  21. return ContentType;
  22. } catch (err) {
  23. console.log(err);
  24. const message = `Error getting object ${key} from bucket ${bucket}. Make sure they exist and your bucket is in the same region as this function.`;
  25. console.log(message);
  26. throw new Error(message);
  27. }
  28. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement