gpt4 book ai didi

aws-amplify - AWS 放大类型错误 : S3 is not a constructor when bundling with Parcel

转载 作者:行者123 更新时间:2023-12-04 13:38:48 25 4
gpt4 key购买 nike

我正在使用 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)

Parcel v1.12.4 发生错误和 AWS Amplify v2.2.4 .

重现的最小步骤

根据 AWS Amplify Storage documentation 适当配置的 Cognito 身份池和 S3 存储桶需要。

以下文件应该都在同一目录中。

一个 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"
]

使用实例化 S3 客户端

import * as AWS from 'aws-sdk';
const s3 = new AWS.S3();

代替

import * as S3 from 'aws-sdk/clients/s3';
const s3 = new S3();

引用:
  • https://github.com/aws/aws-cdk/issues/7685
  • 关于aws-amplify - AWS 放大类型错误 : S3 is not a constructor when bundling with Parcel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60159991/

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