我正在尝试AWS s3通过向预签名 URL发出PUT 请求来上传文件。我已经在 s3 仪表板上配置了 CORS。
反应代码
const submit = async () => {
const response = await axios.get("http://localhost:8080/api/upload");
console.log(response.data);
const upload = await axios.put(response.data.url, file, {
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": file.type
}
});
};
Run Code Online (Sandbox Code Playgroud)
Node.js 代码
const s3 = new AWS.S3({
accessKeyId: config.aws.accessKeyId,
secretAccessKey: config.aws.secretAccessKey
});
app.get("/api/upload", (req, res) => {
const key = `${uuid()}.jpeg`;
s3.getSignedUrl(
"putObject",
{
Bucket: "bucket-name",
ContentType: "image/jpeg",
Key: key
},
(err, url) => {
res.status(200).json({ key, url }); …Run Code Online (Sandbox Code Playgroud)