作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 AWS Amplify Storage module使用 JavaScript API 存储和检索用户文件。当我使用 Parcel 服务或构建时,控制台打印以下错误信息:
TypeError: S3 is not a constructor
at AWSS3Provider._createS3 (AWSS3Provider.ts:501)
at AWSS3Provider.<anonymous> (AWSS3Provider.ts:130)
at step (AWSS3Provider.ts:19)
at Object.next (AWSS3Provider.ts:19)
at fulfilled (AWSS3Provider.ts:19)
v1.12.4
发生错误和 AWS Amplify
v2.2.4
.
package.json
有依赖性。
{
"main": "index.js",
"dependencies": {
"aws-amplify": "^2.2.4",
"lodash": "^4.17.15"
}
}
aws-exports.js
指定身份池和 S3 存储桶:
export const awsconfig = {
Auth: {
identityPoolId: "xx-xxxx-x:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
region: "xx-xxxx-x"
},
Storage: {
AWSS3: {
bucket: "a-bucket-name",
region: "xx-xxxx-x"
}
}
};
index.html
:
<!DOCTYPE html>
<html lang="en">
<body>
<main></main>
<script src="./index.js"></script>
</body>
</html>
index.js
:
import Amplify, { Storage } from "aws-amplify";
import { awsconfig } from "./aws-exports";
Amplify.configure(awsconfig);
// not required, but helps to confirm everything else worked
window.LOG_LEVEL = "DEBUG";
Storage.get("test.txt")
.then(result => console.log(result))
.catch(err => console.log(err));
npm install
parcel index.html
Storage.get()
与
Storage.configure()
不会产生错误。
webpack
捆绑在一起配置,我没有观察到错误。这让我怀疑我可能需要覆盖一些
parcel
配置。
最佳答案
我在使用 @aws-cdk/aws-lambda-nodejs
部署 Lambda 时遇到了这个问题.
我通过使用 aws-sdk
解决了这个问题。在 Lambda 运行时环境中可用,而不是将我自己的副本捆绑在每个 Lambda 的 Assets 中。它应该同样适用于最终在 Lambda 中运行的其他 Assets 。
npm install --save-dev parcel-plugin-externals
package.json
:
"externals": [
"aws-sdk"
]
import * as AWS from 'aws-sdk';
const s3 = new AWS.S3();
import * as S3 from 'aws-sdk/clients/s3';
const s3 = new S3();
关于aws-amplify - AWS 放大类型错误 : S3 is not a constructor when bundling with Parcel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60159991/
我是一名优秀的程序员,十分优秀!