![](/img/trans.png)
[英]How to upload an image file directly from client to AWS S3 using node, createPresignedPost, & fetch
[英]How to upload an image to Cloudflare Images from Node using fetch?
我正在尝试使用 Node 和 fetch API 将图像从我的计算机上传到 Cloudflare Images。他们为我提供了一个运行良好的 curl 命令。
curl -X POST -F file=@./<file_name> -H "Authorization: Bearer <api_token>" https://api.cloudflare.com/client/v4/accounts/<domain_id>/images/v1
当我尝试将其转换为 fetch 时,Cloudflare 一直在回复中向我发送 400。
const cloudflarePostBody = new FormData();
cloudflarePostBody.append("file", fs.createReadStream("testing.jpeg"));
const cloudflareResponse = await fetch("https://api.cloudflare.com/client/v4/accounts/<my_id>/images/v1", {
method: "POST",
headers: {
Authorization: `Bearer ${cloudflareApiToken}`,
"Content-Type": "multipart/form-data"
},
body: cloudflarePostBody,
});
我的猜测是我在使用createReadStream
读取文件的方式上做错了,但我查过的很多例子都表明了这一点。 有人有建议吗? 提前致谢。
尝试fs.readFile()
而不是fs.createReadStream()
:
fs.readFile("testing.jpeg", (err, data) => {
if (err) throw err;
cloudflarePostBody.append("file", new Blob([data]), "testing.jpeg");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.