生成预签名的网址后出现NoSuchKey错误 [英] I get NoSuchKey error after generating presigned url
问题描述
我正在尝试为我的s3存储桶生成预签名的url.之后,当我将生成的网址复制粘贴到浏览器中时,出现此错误
I am trying to generate presigned url for my s3 bucket. After that when i copy paste the generated url in browser i get this error
<Error>
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
<Key>xx</Key>
<RequestId>xx</RequestId>
<HostId>xx</HostId>
</Error>
我的代码:
const aws = require('aws-sdk');
const multer = require('multer');
const multerS3 = require('multer-s3');
const dotenv = require('dotenv');
dotenv.config();
let region = 'us-east-1';
aws.config.update({
secretAccessKey: process.env.SECRET_ACCESS_KEY,
accessKeyId: process.env.ACCESS_KEY_ID,
region: region,
signatureVersion: 'v4'
});
var s3 = new aws.S3({
region:region
});
const fileFilter = (req, file, cb) => {
if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
cb(null, true);
} else {
cb(new Error('Invalid file type, only JPEG and PNG is allowed!'), false);
}
};
let bucketName = 'somebucketname123';
const signedUrlExpireSeconds = 60 * 5;
const url = s3.getSignedUrl('getObject', {
Bucket: bucketName,
Key: process.env.SECRET_ACCESS_KEY,
Expires: signedUrlExpireSeconds
})
console.log('url', url);
const upload = multer({
fileFilter: fileFilter,
storage: multerS3({
acl: 'public-read',
s3,
bucket: bucketName,
key: function(req, file, cb) {
req.file = file.originalname;
cb(null, file.originalname);
}
})
});
module.exports = upload;
我是aws的新手,我不知道自己在这里犯错了...
i am new to aws and i don't know what i am making wrong here...
当我将新生成的预签名URL粘贴到浏览器中时,应该怎么办?是否为某些特定文件生成了此预装网址?如果这是我应该在代码中的哪个位置,请告诉我s3存储桶中的哪个文件名
What should happen when i paste the new generated presigned url in the browser ? Is this preigned url generated for some specific file ? If it is where in my code should i tell, for which file name in my s3 bucket
推荐答案
bucketParms中的密钥应该是s3对象的密钥.
KEY in bucketParms should be the key of the s3 object.
用于从S3下载对象的签名网址
Signed Url for downloading an object from S3
桶:测试事件ObjectKey:myfolder/test.json
Bucket: test-events ObjectKey: myfolder/test.json
const bucketParms = {
Bucket: "test-events",
Key: "myfolder/test.json",
Expires: 60,
};
用于下载:
s3.getSignedUrl("getObject", bucketParms, (error, url) => {
if (error) console.log("error", error);
if (url) console.log("url", url);
});
对于将对象上传到S3,我们仍然需要指定要上传到的存储桶和密钥.
for Uploading the object to S3 , we will still need to specify Bucket and Key to which we want to upload.
s3.getSignedUrl("putObject", bucketParms, (error, url) => {
if (error) console.log("error", error);
if (url) console.log("url", url);
});
要使用此URL进行上传,我们可以做卷发或邮递员
For using this URL for uploading, we can do a curl or postman
curl --location --request PUT 'https://test-events.s3.amazonaws.com/98..?X....' \
--header 'Content-Type: image/png' \
--data-binary '/Users/user/image/path'
或从带有正文 binary
的PostMan PUT/POST中选择文件.对象将上传到 myfolder/test.json
(所选文件的名称无关紧要)
or
From PostMan PUT/POST with Body binary
and selecting the file. Object will be uploaded to myfolder/test.json
(name of the file selected doesn't matter)
这篇关于生成预签名的网址后出现NoSuchKey错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!