Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用Axios下载文件,然后上传到Amazon S3_Node.js_Amazon S3_Stream_Axios_Multipartform Data - Fatal编程技术网

Node.js 使用Axios下载文件,然后上传到Amazon S3

Node.js 使用Axios下载文件,然后上传到Amazon S3,node.js,amazon-s3,stream,axios,multipartform-data,Node.js,Amazon S3,Stream,Axios,Multipartform Data,关于使用Axios下载文件并将其上传到S3,我已经看到了各种各样的问题,但没有一个问题将它们联系在一起,我对流、BLOB、多部分表单等感到困惑。以下是我到目前为止的代码 下载文件。 const downloadResponse = await axios({ url: `https://example.com/test.jpg`, method: 'GET', responseType: 'stream' // Should this be blob, stream

关于使用Axios下载文件并将其上传到S3,我已经看到了各种各样的问题,但没有一个问题将它们联系在一起,我对流、BLOB、多部分表单等感到困惑。以下是我到目前为止的代码

下载文件。

const downloadResponse = await axios({
    url: `https://example.com/test.jpg`,
    method: 'GET',
    responseType: 'stream'   // Should this be blob, stream or arraybuffer?
})
不确定“downloadResponse.data”中包含的内容此时,typeof表示它是一个对象,而不是一个流

获得签名响应(这是通过Storyblok CMS而不是亚马逊完成的)

使用表单数据包创建表单数据

let form = new FormData()

for (var key in signedResponse.fields) {
    form.append(key, signedResponse.fields[key])
}

try {
    form.append('file', fs.createReadStream(downloadResponse.data))
} catch (error) {
    console.log("Error Creating Data", error)
}
上传到S3

try {
    
    const uploadResponse = await axios({
        method: 'post',
        url: signedResponse .data.post_url,
        data: form.getBuffer(),
        headers: form.getHeaders()
    })

} catch (error) {
    console.log("Error Uploading", error)
}
目前,在创建流时,这会导致以下错误

创建数据类型错误[ERR\u INVALID\u ARG\u TYPE]:“path”参数必须是string、Buffer或URL类型之一。已接收类型对象。

const downloadResponse = await axios({
    url: `https://example.com/test.jpg`,
    method: 'GET',
    responseType: 'stream'   // Should this be blob, stream or arraybuffer?
})
我曾尝试直接从第一个请求获取数据,但没有对其使用fs.createReadStream(),因为它应该已经是一个流,但这会产生相同的错误

我还尝试从第一个请求返回一个arraybuffer,使用Buffer.from()然后附加缓冲区,但这也不起作用,我认为Amazon给了我400个错误,所以我迷路了

任何帮助都将不胜感激。

这正是我所需要的-