S3.upload在生产环境中不起作用,但在localhost NodeJS中起作用

问题描述

我喜欢使用AWS,但是我似乎无法使用Node.js将任何应用程序上传到生产环境中的存储桶中,但是在localhost开发中,它似乎没有问题。我的应用托管在生产中的netlify上。

问题出在生产中,回调从未触发,我在netlify服务器中看到日志,例如“ this is image Metadeta”日志,以及除回调之外的所有其他触发信号。

有人知道为什么这个问题只会在生产中发生吗?我将所有存储桶策略/用户角色设置为允许使用“ AmazonS3FullAccess”策略进行所有访问

还有我的角色政策

{
    "Version": "2012-10-17","Statement": [
        {
           "Sid": "VisualEditor0","Effect": "Allow","Action": "s3:*","Resource": [
              "arn:aws:s3:::testbucket
              "arn:aws:s3:::testbucket/testbucket"
           ]
       }
   ]
}

这是我的代码,用于将图像上传到存储桶uploadProfilePicToS3函数

function uploadProfilePicToS3(file) {
let s3bucket = new AWS.S3({
 accessKeyId: IAM_USER_KEY,secretAccessKey: IAM_USER_SECRET,Bucket: BUCKET_NAME,});
s3bucket.createBucket(function () {
 var params = {
   Bucket: BUCKET_NAME,Key: `instacloneprofilepics/${file.name}`,Body: file.data,ACL: "public-read",ContentType: file.mimetype,};
 console.log("this is the image Metadeta",params);
 s3bucket.upload(params,function (err,data) {
   if (err) {
     console.log("error in callback");
     console.log(err);
     return;
   }

   console.log("POST UPLOADED SUCCESS FROM CALLBACK");

   console.log(data);
   return data;
 });
});
}

然后我只需在路由中调用从前端发送的数据/文件中的函数即可。

   const file = req.files.img;

   busboy.on("finish",function () {
     console.log("Upload finished");

     console.log(file);
     uploadProfilePicToS3(file);
   });

   req.pipe(busboy);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)