javascript - AWS S3 使用预签名 URL 更新镜像(Axios-PUT 请求)

标签 javascript amazon-s3 axios jpeg

我正在尝试使用 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;
     });

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

最佳答案

它无法使 AXIOS 正常工作以上传图像。

节点获取模块以二进制形式发送图像并指定“内容类型”。

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

 //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;
});

关于javascript - AWS S3 使用预签名 URL 更新镜像(Axios-PUT 请求),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66339330/

相关文章:

javascript - 如何将javascript数组传递给php

javascript - 悬停后检查新的悬停目标(Javascript/JQuery)?

amazon-web-services - 如何绕过 AWS API 网关的 10MB 限制并将大文件发布到 AWS lambda?

error-handling - 自定义Axios处理错误

.net - 如何制作 React App windows 身份验证

javascript - Vue 原型(prototype) Axios

Javascript - 如何在对对象文字的ajax调用中绑定(bind) 'this'

javascript - 将 JavaScript 数组转换为 JSON 对象

ruby-on-rails - 将 CSV 流从 Ruby 上传到 S3

amazon-s3 - 从云端发送 "Cache-Control : no-cache"