是否可以在不使用 cli 的情况下使用放大框架?

Is it possible to use the amplify framework without using the cli?

放大常见问题解答明确说明你可以。但是 github 链接现在只是将您重定向到主放大页面,并且说明仅讨论使用 cli。

问:即使不使用 CLI,我也可以使用 Amplify Framework 库吗? 是的。这些库可用于访问在没有 Amplify CLI 的情况下创建的后端资源。

你可以,如果你知道你在做什么。细节决定成败。文档说:

Can I use the Amplify Framework libraries even if I do not use the CLI? Yes.

注意它是如何明确说明框架库的。这意味着您无法手动生成资源。 (从技术上讲,您可以自己编写模板,但据我所知,您仍然需要 CLI 的 amplify push 命令来影响云。)但是您可以使用框架组件。

这意味着,例如,您可以手动配置 AWS Amplify 以使用自定义 GraphQL 端点,然后使用框架公开的帮助程序、组件和方法(例如 graphqlOperation)发出您的请求。

我正在使用带有手动设置的 Auth、AppSync 和存储的反应放大库,所以它是可行的。

要设置 S3,您可以按照指南 here 进行操作。 对于 AppSync 设置,我使用 this plugin.

我了解到您可以在没有 Amplify CLI 的情况下使用 amplify 库。

为此,您只需照常安装放大库即可。

在反应网络中:

npm install --save aws-amplify
npm install --save aws-amplify-react

之后,您需要手动配置要与 Amplify.configure(); 一起使用的任何功能。您可以在您计划使用的每个库的 Amplify 文档中找到手动配置。

这是一个使用 Cognito 的示例:

https://aws-amplify.github.io/docs/js/authentication#manual-setup

Amplify.configure({
    "aws_project_region": process.env.REACT_APP_REGION,
    "aws_cognito_identity_pool_id": process.env.REACT_APP_IDENTITY_POOL_ID,
    "aws_cognito_region": process.env.REACT_APP_REGION,
    "aws_user_pools_id": process.env.REACT_APP_USER_POOL_ID,
    "aws_user_pools_web_client_id": process.env.REACT_APP_CLIENT_ID,
    "oauth": {},
    Auth: {
        // REQUIRED - Amazon Cognito Identity Pool ID
        identityPoolId: process.env.REACT_APP_IDENTITY_POOL_ID,
        // REQUIRED - Amazon Cognito Region
        region: process.env.REACT_APP_REGION, 
        // OPTIONAL - Amazon Cognito User Pool ID
        userPoolId: process.env.REACT_APP_USER_POOL_ID, 
        // OPTIONAL - Amazon Cognito Web Client ID (26-char alphanumeric string)
        userPoolWebClientId: process.env.REACT_APP_CLIENT_ID,
    }
});

您不必使用 amplify push 进行部署。您可以手动部署和配置您正在使用的任何功能。

我发现这种方法可以让您完全控制使用 Amplify 库,而无需 CLI 和部署过程的开销。

嗯,这应该没什么问题。在官方文档中提到您可以在没有 CLI 的情况下使用 Amplify 库:

If you are not using the Amplify CLI or need to override these settings, this documentation shows the available configuration properties for each category.

所以你看这没问题。

在下面的 link 中,您可能会找到不同服务的配置(通常会生成):

https://docs.amplify.aws/lib/client-configuration/configuring-amplify-categories/q/platform/js#general-configuration

是的,这是可能的。正如 Mohammed 所指出的,这是 covered in the documentation,尽管他们绝对不急于通知您此选项可用。无论如何,React 应用程序的设置是:

npm install aws-amplify @aws-amplify/ui-react

然后你所要做的就是:

import Amplify from 'aws-amplify';
import { withAuthenticator } from '@aws-amplify/ui-react';

Amplify.configure({
  Auth: {
    region: 'XX-XXXX-X',
    userPoolId: 'XX-XXXX-X_abcd1234',
    userPoolWebClientId: 'a1b2c3d4e5f6g7h8i9j0k1l2m3',
    mandatorySignIn: true,
  }
});

export default withAuthenticator(function Home() {
  return (<h1>Protected Content</h1>)
})

请注意:自 2022 年 2 月起,用于身份验证的 Amplify UI 组件最近对其 API 进行了重大更改。您会发现很多关于 AmplifyAuthenticator 及其子组件的旧帖子和文档,例如 this page for example. If you try to use these examples with the current version of Amplify UI, it will fail without explaining why! You need to be using the Authenticator component. Instead, use the examples in the Amplify UI docs.