Advertisement
Javi

AWS: Lambda function to transfer any URL to S3

Apr 21st, 2023
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 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. const url = event.queryStringParameters.url;
  7.  
  8. try {
  9. const fileName = await transferFileFromUrlToS3(url, 'demo-lambda-codewhisperer');
  10. console.info(`Done.`);
  11. // get size of a file
  12. const stats = fs.statSync('/tmp/' + fileName);
  13. return {
  14. statusCode: 200,
  15. body: JSON.stringify(stats),
  16. };
  17.  
  18. } catch (error) {
  19. console.log(JSON.stringify(error));
  20. return {
  21. statusCode: 500,
  22. body: JSON.stringify(error),
  23. };
  24. }
  25. };
  26.  
  27.  
  28. // Function for downloading a file from url parameter returning a promise
  29. async function downloadFileFromUrl(url) {
  30. return new Promise((resolve, reject) => {
  31. https.get(url, (response) => {
  32. // get the name of the file from a url
  33. const fileName = url.substring(url.lastIndexOf('/') + 1);
  34. const file = fs.createWriteStream('/tmp/' + fileName);
  35. console.log(`Downloading ${url} to /tmp/${fileName}.`);
  36. response.pipe(file);
  37. file.on('finish', () => {
  38. file.close(() => {
  39. resolve(fileName);
  40. });
  41. });
  42. }).on('error', (error) => {
  43. reject(error);
  44. });
  45. });
  46. }
  47.  
  48. // function to upload a file to an s3 bucket piping the content
  49. async function uploadFileToS3(bucketName, fileName) {
  50. const fileContent = fs.readFileSync('/tmp/' + fileName);
  51. const s3Client = new S3Client({ region: 'us-east-1' });
  52. const uploadResult = await s3Client.send(new PutObjectCommand({
  53. Bucket: bucketName,
  54. Key: fileName,
  55. Body: fileContent,
  56. }));
  57. return uploadResult;
  58. }
  59.  
  60. async function transferFileFromUrlToS3(url, bucketName) {
  61. console.info(`Transferring ${url} to ${bucketName}.`);
  62. // download the file from the url
  63. const fileName = await downloadFileFromUrl(url);
  64. // upload the file to s3 bucket
  65. const response = await uploadFileToS3(bucketName, fileName);
  66. return fileName;
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement