stream response from nodejs request to s3

后端 未结 3 1324
不知归路
不知归路 2020-12-19 02:02

How do you use request to download contents of a file and directly stream it up to s3 using the aws-sdk for node?

The code below gives me Object #

3条回答
  •  醉酒成梦
    2020-12-19 02:24

    As Request has been deprecated, here's a solution utilizing Axios

    const AWS = require('aws-sdk');
    const axios = require('axios');
    
    const downloadAndUpload = async function(url, fileName) {
      const res = await axios({ url, method: 'GET', responseType: 'stream' });
      const s3 = new AWS.S3(); //Assumes AWS credentials in env vars or AWS config file
      const params = {
        Bucket: IMAGE_BUCKET, 
        Key: fileName,
        Body: res.data,
        ContentType: res.headers['content-type'],
      };
      return s3.upload(params).promise();
    }
    

    Note, that the current version of the AWS SDK doesn't throw an exception if the AWS credentials are wrong or missing - the promise simply never resolves.

提交回复
热议问题