javascript - 如何使用带有 Nuxt.js 和 Axios 的预签名 URL 将文件上传到 S3 存储桶?
问题描述
我想在我的 Nuxt.js 网站上使用 Axios 将文件上传到 S3 存储桶中。我已经管理了获取预签名 URL 的部分。当我尝试将预签名 URL 与 Postman 一起使用时,一切正常,但是当我与 Axios 一起尝试时,我收到此错误:Uncaught (in promise) ReferenceError: Access is not defined
.
这是我的代码:
<template>
<v-file-input label="Select your file to upload" accept="image/*" v-model="myFile">
File to upload to S3
</v-file-input>
</template>
<script>
export default {
data() {
return {
myFile: null,
ApiGatewayUrl: "https://xxxxxx.execute-api.eu-central-1.amazonaws.com/"
}
},
methods: {
uploadFile() {
// get the pre-signed URL
this.$axios.get(this.ApiGatewayUrl, {
headers: {
Authorization: this.$auth.strategy.token.get()
}
}).then((response) => {
// now do a PUT request to the pre-signed URL
this.$axios.put(response.data, {
files: this.myFile
}, {
headers: {
[Content-Type]: 'multipart/form-data'
}
}).then((response) => {
this.setState({
statusCode: response.status,
})
})
})
}
}
}
</script>
由于它与 Postman 一起工作,我猜错误来自我的 Axios 配置。
解决方案
您指定的内容类型标题错误。
代替:
headers: {
[Content-Type]: 'multipart/form-data'
}
和:
headers: {
"Content-Type": "multipart/form-data"
}
推荐阅读
- python - How can I overload the __repr__ method to display all items in a linked list Stack?
- python - Pandas 在其他列约束下找到最小化列总和的行子集
- graph - 一个关于PPG(进程优先图)的问题,能不能解释下为什么下图是错误的
- ios - 从 Django 后端推送通知到 iOS/Swift 前端
- wordpress - 无法从服务器加载 admin-ajax
- r - 删除R中每个像素的时间序列栅格中的异常值
- reactjs - 与 useCallback 不一致的 linter 警告
- python - 在不上采样 1 分钟的情况下合并 1 分钟、5 分钟和每日 OHLC 数据帧?
- javascript - 如何覆盖从javascript中的模块返回的类中的函数
- java - 如何在 IDEA 中运行单个 java 文件?