简体   繁体   English

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

[英]AWS S3 update image using pre-signed URL (Axios-PUT Request)

I'm trying to update a local JPG image file into an S3 bucket using the REST PUT request and Axios.我正在尝试使用 REST PUT 请求和 Axios 将本地 JPG 图像文件更新到 S3 存储桶中。

I managed to send the PUT request and to get a positive answer from AWS S3 Service but what it's been upload is not a JPG file but a JSON file .我设法发送了 PUT 请求并从 AWS S3 服务获得了肯定的答复,它上传的不是 JPG 文件,而是 JSON 文件

This is the code that I'm using:这是我正在使用的代码:

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

I have already try to change the headers to {'Content-Type': 'application/octet-stream' } but I obtained the same result.我已经尝试将标题更改为{'Content-Type': 'application/octet-stream' }但我得到了相同的结果。

It did not manage to make AXIOS work in order to upload an image.它没有设法使 AXIOS 工作以上传图像。

The node-fetch module did it sending the image as a binary and specifying the "Content-type". node-fetch 模块将图像作为二进制文件发送并指定“内容类型”。

If I try to the same using AXIOS it the image was always packet into a form-data and the result was JSON file uploaded into the S3 bucket instead of the image.如果我尝试使用 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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用预签名 URL 从 PUT 到 S3 的 400 错误请求 - 400 Bad Request from PUT to S3 using pre-signed URL 通过预签名URL将PUT上传到S3时拒绝签名 - Signature Denied in PUT upload to S3 with Pre-Signed URL S3使用预先签名的URL从浏览器上传图片 - S3 Upload image with pre-signed url from browser 如何使用预签名的 URL 而不是凭证直接从浏览器上传到 AWS S3? - How to upload to AWS S3 directly from browser using a pre-signed URL instead of credentials? 使用预签名 URL 将文件上传到 S3 - Uploading file to S3 using a pre-signed URL S3 为使用 aws-sdk v3 预签名的 PutObject 命令 url 提供 SignatureDoesNotMatch 错误 - S3 gives SignatureDoesNotMatch error for PutObject command pre-signed url using aws-sdk v3 S3 PUT不适用于javascript中的预签名URL - S3 PUT doesn't work with pre-signed URL in javascript 使用angular + aws-sdk-js +预签名网址将文件上传到S3 - Uploading a file to S3 with angular + aws-sdk-js + pre-signed url 使用预先签名的POST URL将文件上载到AWS S3时设置随机文件名 - Set random file name when upload file to AWS S3 with pre-signed POST url 如何在另一个选项卡中打开 S3 预签名 URL? - How to open an S3 pre-signed URL in another tab?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM