Download image from S3 bucket to Lambda temp folder (Node.js)

后端 未结 3 793
后悔当初
后悔当初 2020-12-31 15:55

Good day guys.

I have a simple question: How do I download an image from a S3 bucket to Lambda function temp folder for processing? Basically, I nee

相关标签:
3条回答
  • 2020-12-31 16:50
    // Using NodeJS version 10.0 or later and promises
    
    const fsPromise = require('fs').promises;
    
    try {
        const params = {
            Bucket: 's3Bucket',
            Key: 'file.txt',
        };
    
        const data = await s3.getObject(params).promise();
    
        await fsPromise.writeFile('/tmp/file.txt', data.Body);
    
    } catch(err) {
        console.log(err);
    }
    
    0 讨论(0)
  • You can get the image using the aws s3 api, then write it to the tmp folder using fs.

    var params = {   Bucket: "BUCKET_NAME",   Key: "OBJECT_KEY" };  
    
    s3.getObject(params, function(err, data){   if (err) {
        console.error(err.code, "-", err.message);
        return callback(err);   }
    
      fs.writeFile('/tmp/filename', data.Body, function(err){
        if(err)
          console.log(err.code, "-", err.message);
    
        return callback(err);   
      }); 
    });
    

    Out of curiousity, why do you need to write the file in order to attach it? It seems kind of redundant to write the file to disk so that you can then read it from disk

    0 讨论(0)
  • 2020-12-31 16:59

    If you're writing it straight to the filesystem you can also do it with streams. It may be a little faster/more memory friendly, especially in a memory-constrained environment like Lambda.

    var fs = require('fs');
    var path = require('path');
    
    var params = {
        Bucket: "mybucket",
        Key: "image.png"
    };
    
    var tempFileName = path.join('/tmp', 'downloadedimage.png');
    var tempFile = fs.createWriteStream(tempFileName);
    
    s3.getObject(params).createReadStream().pipe(tempFile);
    
    0 讨论(0)
提交回复
热议问题