无法将文件上传到 AWS Amplify 中的 S3 存储桶

时间:2021-06-24 12:42:02

标签: javascript reactjs amazon-web-services amazon-s3

我使用以下命令将我的 React 项目部署到 AWS-Amplify。

amplify init
amplify add auth api storage

我在使用“aws-amplify”节点模块上传图像时遇到错误。

我的代码:

import { Storage } from "aws-amplify"
...

    try {
      Storage.put(file.name, file, {
        contentType: file.type,
      }).then((res) => {
        console.log(res, "Upload result")
      })
    } catch (error) {
      console.error("Error uploading file: ", error)
    }

错误状态:

Error screen shot

如您所见,现在 Storage.put() 函数返回 400 错误代码。 有没有人解决这个问题?

1 个答案:

答案 0 :(得分:0)

ContentType 是可选的,如果您不确定您的代码格式是否正确,您可以尝试简化它,直到它起作用,然后添加额外的参数。

你可以看看放置示例here

您可能还想了解另一件事,即 s3 存储桶策略,请检查您可以做什么:

aws s3api get-bucket-policy --bucket my-bucket

如果您不知道如何使用 was cli,请查看 here

相关问题