Advertisement
Javi

AWS: Lambda function to transfer any URL to S3 v2

Apr 21st, 2023
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import * as fs from 'fs';
  2. import * as https from 'https'
  3. import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
  4.  
  5. export const handler = async(event) => {
  6.  
  7. // get url from querystring in the event
  8. const url = event.queryStringParameters.url;
  9. // extract file name from url
  10. const fileName = url.substring(url.lastIndexOf('/') + 1);
  11. await transferFile(url, '/tmp/' + fileName);
  12. return {
  13. statusCode: 200,
  14. body: JSON.stringify('ok'),
  15. };
  16. };
  17.  
  18. function uploadFileToS3(filePath, bucketName) {
  19. const s3Client = new S3Client({ region: 'us-east-1' });
  20. const command = new PutObjectCommand({
  21. Bucket: bucketName,
  22. Key: filePath,
  23. Body: fs.readFileSync(filePath),
  24. });
  25. return s3Client.send(command);
  26. }
  27.  
  28. // function for downloading a file from an url to disk and resolve on closes
  29. function downloadFile(url, file) {
  30. return new Promise((resolve, reject) => {
  31. const fileStream = fs.createWriteStream(file);
  32. https.get(url, (response) => {
  33. response.pipe(fileStream);
  34. fileStream.on('close', () => {
  35. resolve();
  36. });
  37. }).on('error', (err) => {
  38. reject(err);
  39. });
  40. });
  41. }
  42.  
  43. // async function for transferring a file from an url to a bucket
  44. async function transferFile(url, filePath) {
  45. await downloadFile(url, filePath);
  46. await uploadFileToS3(filePath, 'demo-lambda-codewhisperer');
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement