微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用预签名 URL 的 AWS S3 更新映像Axios-PUT 请求

如何解决使用预签名 URL 的 AWS S3 更新映像Axios-PUT 请求

我正在尝试使用 REST PUT 请求和 Axios 将本地 JPG 图像文件更新到 S3 存储桶中。

我设法发送了 PUT 请求并从 AWS S3 服务获得肯定答复但是上传不是 JPG 文件而是 JSON 文件

这是我正在使用的代码

    //Create the FormData
    var data = new FormData();
    data.append('file',fs.createReadStream(image_path));


   //Send the file to File-system
   console.log("Sending file to S3...");
   const axiosResponse = await axios.put(image_signed_url,{
       data: data,headers: { 'Content-Type': 'multipart/form-data' }
     }).catch(function(error) {
      console.log(JSON.stringify(error));
      return null;
     });

我已经尝试将标题更改为 {'Content-Type': 'application/octet-stream' },但我获得了相同的结果。

解决方法

它没有设法使 AXIOS 工作以上传图像。

节点获取模块将图像作为二进制文件发送并指定“内容类型”。

如果我尝试使用 AXIOS 进行同样的操作,图像总是被打包成表单数据,结果是 JSON 文件上传到 S3 存储桶而不是图像。

 //Send the file to File-system
console.log("Sending file to S3...");
const resp = await fetch(image_signed_url,{
    method: 'PUT',body: fs.readFileSync(image_path),headers: {
      'Content-Type': 'image/jpeg',},}).catch( err => {
  console.log(err);
  return null;
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。