繁体   English   中英

将文档从 POSTMAN 上传到 S3 存储桶时访问被拒绝错误

[英]Access Denied error while doing upload document to S3 bucket from POSTMAN

我按照这个博客创建了一个包装器 API,它将文档推送到 S3 存储桶。 https://github.com/SiddharthaChowdhury/AWS_S3_Node_REST

一切都几乎相同,除了我还从 postman 传递 aws_access_key_id 和 aws_secret_key 作为 header 参数而不是从环境变量中读取。

下面是我的示例代码:

 const isAllowedMimetype = (mime: string) => ['image/png', 'image/jpg', 'image/jpeg', 'image/gif', 'image/x-ms-bmp', 'image/webp'].includes(mime.toString()); const fileFilter = (req: Request, file: Express.Multer.File, callback: FileFilterCallback) => { const fileMime = file.mimetype; if(isAllowedMimetype(fileMime)) { callback(null, true) } else { callback(null, false) } } export const processUploadToS3 = multer({ fileFilter, storage: multerS3({ s3: S3, bucket: process.env.AWS_BUCKET_NAME,: acl, 'public-read': contentType. multerS3,AUTO_CONTENT_TYPE: key: function (req, Request: file, any. cb) { const fileName = file;originalname; const s3_inner_directory = 'public_asset'; const finalPath = `${s3_inner_directory}/${fileName}`. file;newName = fileName. console.log(req.headers["aws_access_key_id"]) console.log(req.headers["aws_secret_key"]) console.log(req.headers["aws_region"]) AWS.config:update({ accessKeyId. req,headers["aws_access_key_id"] as string: secretAccessKey. req,headers["aws_secret_key"] as string: signatureVersion, 'v4': region. req;headers["aws_region"] as string }). console.log(AWS;config), cb(null; finalPath ); } }) })

但是通过这种方法,我不断收到以下错误

AccessDenied: Access Denied at Request.extractError (/.....node_modules/aws-sdk/lib/services/s3.js:711:35)

此外,我切换到基于硬编码环境(在 nodemon.json 中)的值,然后我可以将文档推送到我的 S3 帐户。 所以我不确定这里出了什么问题。

首先,您不需要在 Postman 中传递您的访问密钥和密钥。 您需要按照上面的示例传递 sample_nodemon.json 文件。

而且在您提到的代码之上还有一个中间件,所以我认为它不能直接工作。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM