gpt4 book ai didi

node.js - AWS S3 存储桶 - getSignedUrl PUT 返回 400(错误请求)

转载 作者:搜寻专家 更新时间:2023-11-01 00:35:40 27 4
gpt4 key购买 nike

我在我的项目中遇到了一个奇怪的问题,我无法找到我做错了什么。我正在尝试将图像上传到我的博客,并且我正在使用 aws s3 配置来执行此操作。我相信我的配置是正确的,但以防万一我将它们添加到那里:

这里是我的cors配置

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
<CORSRule>
<AllowedOrigin>http://localhost:3000</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

我所有的 IAM 策略和用户 API 都已按顺序激活。这是我后端的代码:

const s3 = new AWS.S3({
accessKeyId: keys.aws.clientID,
secretAccessKey: keys.aws.clientSecret,
signatureVersion: "v4",
region: "eu-west-3"
})

const router = express.Router()
// @route GET api/posts/upload
// @desc Upload an image on amazone server API
// @access Private
router.get(
"/upload",
passport.authenticate("jwt", { session: false }),
(req, res) => {
const key = `${req.user.id}/${uuid()}.jpeg`
s3.getSignedUrl(
"putObject",
{
Bucket: "bebeyogini",
ContentType: "image/jpeg",
Key: key
},
(err, url) => res.send({ key, url })
)
}
)

当我调用/upload 端点时,它返回一个

<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you provided. Check your key and signing method.
</Message>

这就是为什么在我的前端(在 redux 中)我添加了一个 PUT,我在标题中添加了内容类型文件。但是在这个项目中还是不行

export const sendPost = (newPost, file, history) => async dispatch => {
dispatch(loading())
const uploadConfig = await axios.get("/api/posts/upload")
console.log(uploadConfig)
console.log(uploadConfig.data)
await axios.put(uploadConfig.data.url, file, {
headers: {
"Content-Type": file.type
}
})
const res = await axios.post("/api/posts", {
...newPost,
imageUrl: uploadConfig.data.key
})
history.push("/dashboard")
dispatch({
type: POSTS_FETCHED,
payload: res.data
})
}

我几个月前在另一个项目中取得了成功,使 aws s3 可以执行这些相同的配置,我在两者之间的唯一区别是成功的是使用 cookie session 设置的,而这个是 jwt-token在标题中。

如果有人有想法......我完全被困住了!这里是存储库

错误状态:

bebeyogini.s3.eu-west-3.amazonaws.com/5bc0eca10743075fecb360f2/8bb4ff60-d8e7-11e8-ad9d-f776f418fd42.jpeg?Content-Type=image%2Fjpeg&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIAMYTIX267RJ5E4A%2F20181026%2Feu-west-3%2Fs3%2Faws4_request&X-Amz-Date=20181026T062242Z&X-Amz-Expires=900&X-Amz-Signature=943652af4bea7e8e0e6d0f97503ea997817e7d68e0540c599643d612d71fe693&X-Amz-SignedHeaders=host:1 PUT https://bebeyogini.s3.eu-west-3.amazonaws.com/5bc0eca10743075fecb360f2/8bb4ff60-d8e7-11e8-ad9d-f776f418fd42.jpeg?Content-Type=image%2Fjpeg&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIAMYTIX267RJ5E4A%2F20181026%2Feu-west-3%2Fs3%2Faws4_request&X-Amz-Date=20181026T062242Z&X-Amz-Expires=900&X-Amz-Signature=943652af4bea7e8e0e6d0f97503ea997817e7d68e0540c599643d612d71fe693&X-Amz-SignedHeaders=host 400 (Bad Request)
createError.js:17 Uncaught (in promise) Error: Request failed with status code 400
at createError (createError.js:17)
at settle (settle.js:19)
at XMLHttpRequest.handleLoad (xhr.js:78)

https://github.com/erwanriou/bebeyogini

最佳答案

好吧,经过几个晚上的痛苦,我确实找到了问题的解决方案。请注意,AWS S3 不接受包含在他的 PUT 请求中的 jwt 不记名 token 。所以你必须在做请求时禁用它。在这里,我可以使用库 axios 在我的前端修复它...

export const sendPost = (values, file, history) => async dispatch => {
dispatch(loading())
const uploadConfig = await axios.get("/api/posts/upload")
delete axios.defaults.headers.common["Authorization"]
await axios.put(uploadConfig.data.url, file, {
headers: {
ContentType: file.type
}
})
const token = localStorage.getItem("jwtToken")
axios.defaults.headers.common["Authorization"] = token
const res = await axios.post("/api/posts", {
...values,
imageUrl: uploadConfig.data.key
})
dispatch({
type: POSTS_FETCHED,
payload: res.data
})
history.push("/dashboard")
}

关于node.js - AWS S3 存储桶 - getSignedUrl PUT 返回 400(错误请求),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52999712/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com