AWS Amplify

一个用于使用云服务开发应用程序的声明式JavaScript库。「A declarative JavaScript library for application development using cloud services.」

Github stars Tracking Chart

AWS Amplify

AWS Amplify 是面向前端和移动开发人员构建支持云的应用程序的 JavaScript 库。

AWS Amplify提供跨不同类别云操作的声明性且易于使用的界面。 AWS Amplify适用于任何基于JavaScript的前端工作流程,以及适用于移动开发人员的React Native。

我们的默认实施适用于Amazon Web Services(AWS),但AWS Amplify旨在为任何自定义后端或服务开放和插拔。

注意:

Amplify@3.x.x 有突破性的变化。请看下面的突破性变化:

  • AWS.credentialsAWS.config 不再存在于Amplify JS的任何地方。
  • 这两个选项在第3版中将无法使用。您将无法使用和设置自己的凭证。关于 "如何迁移到使用Amplify提供的凭证" 的迁移计划将在GA推出后的未来几周内跟进。

如果你不能迁移到 aws-sdk-js-v3 或依赖 aws-sdk@2.x, ,你将需要单独导入它。

  • 如果您在Amplify JS应用程序中使用导出的路径,(例如:import from "@aws-amplify/analytics/lib/Analytics"),现在这将打破,不再支持。你将需要改用命名导入。:
    import { Analytics } from 'aws-amplify';        
  • 如果您使用类别作为 Amplify.<Category>,这将不再工作,我们建议导入您需要使用的类别:
    import { Auth } from 'aws-amplify';        
    • 唯一的例外是使用 aws-amplify-angular 包,我们仍然提供所有实例化的category。这在以后的版本中可能会发生变化。
  • 对于 aws-amplify-reactAuthenticator 组件,你需要在你的应用程序中导入样式:
    import `@aws-amplify/ui/dist/style.css`;        

特性 / APIs

  • 身份验证 :面向想要创建的开发人员的API和构建块用户认证体验。
  • Analytics :轻松收集应用的分析数据。 Google Analytics 数据包括您要在应用中跟踪的用户会话和其他自定义事件。
  • API :提出HTTP请求时提供简单的解决方案。它提供符合AWS Signature Version 4的自动轻量级签名过程。
  • GraphQL客户端 :互动使用您的GraphQL服务器或AWS AppSync API,易于使用和使用配置GraphQL客户端。
  • 存储 :提供一种简单的机制来管理您的用户内容应用程序在公共,受保护或私人存储桶中。
  • 推送通知 :允许您在应用中集成推送通知使用Amazon Pinpoint定位和广告系列管理支持。
  • 互动 :使用深度学习技术创建对话机器人。
  • PubSub :提供与基于云的面向消息的中间件的连接。
  • 国际化 :轻量级国际化解决方案。
  • 缓存 :为JavaScript开发人员提供通用LRU缓存以存储具有优先级和到期设置的数据。

访问我们的网站,了解有关AWS Amplify的更多信息。

安装

AWS Amplify可在 npm 上以 aws-amplify 包的形式提供

Web
  $ npm install aws-amplify --save    

或者您可以单独安装要使用的模块:

  $ npm install @aws-amplify/auth --save    
React

如果您正在开发 React 应用,则可以安装其他软件包 aws-amplify-react 包含高阶组件

$ npm install aws-amplify --save
$ npm install aws-amplify-react --save    
Angular

如果您正在开发 Angular 应用,则可以安装其他软件包 aws-amplify-angular 。此软件包包含 Angular模块,其中包含提供商和组件

$ npm install aws-amplify --save
$ npm install aws-amplify-angular --save    

访问我们的Web安装指南,开始构建您的网络应用。

Vue

如果您正在开发 Vue 应用,则可以安装其他软件包 aws-amplify-vue 。此软件包包含Amplify库的 Vue插件以及Vue 组件

$ npm install aws-amplify --save
$ npm install aws-amplify-vue --save    

访问我们的网络安装指南,开始构建您的Vue应用。

React Native

对于React Native开发,请安装 aws-amplify

$ npm install aws-amplify --save    

如果您正在开发 React Native 应用,则可以安装其他软件包 aws-amplify-react -native 包含高阶组件

$ npm install aws-amplify-react-native --save    

访问我们的 React Native安装指南,开始构建您的Web应用。

配置

在您的应用中的某个位置,最好是在根级别,使用您的资源配置Amplify。

使用AWS资源
import Amplify from 'aws-amplify';
import aws_exports from './aws-exports';
Amplify.configure(aws_exports);
// 或者您不想安装所有类别
import Amplify from '@aws-amplify/core';
import Auth from '@aws-amplify/auth';
import aws_exports from './aws-exports';
// 以这种方式,您只导入Auth并进行配置。
Amplify.configure(aws_exports);    
没有AWS
 Amplify.configure({
    API: {
        graphql_endpoint: 'https://www.example.com/my-graphql-endpoint',
    },
});    

例子

AWS Amplify支持许多类别方案,例如开发人员指南。下面是几个样本:

1.收集用户会话指标

默认情况下,AWS Amplify可以使用几行代码收集用户会话跟踪数据:

import Analytics from '@aws-amplify/analytics';
Analytics.record('myCustomEvent');    

有关详细信息,请参阅我们的 Analytics开发人员指南

2.将身份验证添加到您的应用

添加用户注册并使用 Auth class 可用的众多方法中的两种方法登录:

import Auth from '@aws-amplify/auth';
Auth.signUp({
    username: 'AmandaB',
    password: 'MyCoolPassword1!',
    attributes: {
        email: 'someemail@example.com',
    },
});
Auth.signIn(username, password)
    .then(success => console.log('successful sign in'))
    .catch(err => console.log(err));    
有关详细信息,请参阅我们的身份验证开发人员指南 React / React Native 向React或React Native应用程序添加身份验证就像使用我们的 withAuthenticator 更高阶组件包装应用程序的主要组件一样简单。 AWS Amplify将为用户注册和登录等常见用例提供可自定义的UI。
//对于React
import { withAuthenticator } from 'aws-amplify-react';
// 对于 React Native
import { withAuthenticator } from 'aws-amplify-react-native';
export default withAuthenticator(App);    
Angular 要向Angular应用添加身份验证,您还可以使用内置服务提供商和组件:
// app.component.ts
import { AmplifyService }  from 'aws-amplify-angular';
...
constructor( public amplify:AmplifyService ) {
  // handle auth state changes
  this.amplify.authStateChange$
    .subscribe(authState => {
      this.authenticated = authState.state === 'signedIn';
      if (!authState.user) {
        this.user = null;
      } else {
        this.user = authState.user;
      }
  });
}
// app.component.html
<amplify-authenticator></amplify-authenticator>    

有关Angular设置和使用的更多详细信息,请参阅我们的 Angular Guide

3.签署HTTP请求

当使用API模块时,AWS Amplify使用 AWS签名版本4 自动签署您的REST请求:

import API from '@aws-amplify/api';
let apiName = 'MyApiName';
let path = '/path';
let options = {
  headers: {...} // OPTIONAL
}
API.get(apiName, path, options).then(response => {
  // Add your code here
});    

有关详细信息,请参阅我们的 API开发人员指南

4.GraphQL API操作

要使用您的应用访问GraphQL API,您需要确保在应用配置中配置端点URL。

// configure a custom GraphQL endpoint
Amplify.configure({
    API: {
        graphql_endpoint: 'https://www.example.com/my-graphql-endpoint',
    },
});
// Or configure an AWS AppSync endpoint.
let myAppConfig = {
    // ...
    aws_appsync_graphqlEndpoint:
        'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql',
    aws_appsync_region: 'us-east-1',
    aws_appsync_authenticationType: 'API_KEY',
    aws_appsync_apiKey: 'da2-xxxxxxxxxxxxxxxxxxxxxxxxxx',
    // ...
};
Amplify.configure(myAppConfig);    
查询
import API, { graphqlOperation } from '@aws-amplify/api';
const ListEvents = `query ListEvents {
  listEvents {
    items {
      id
      where
      description
    }
  }
}`;
const allEvents = await API.graphql(graphqlOperation(ListEvents));    
突变
import API, { graphqlOperation } from '@aws-amplify/api';
const CreateEvent = `mutation CreateEvent($name: String!, $when: String!, $where: String!, $description: String!) {
  createEvent(name: $name, when: $when, where: $where, description: $description) {
    id
    name
    where
    when
    description
  }
}`;
const eventDetails = {
    name: 'Party tonight!',
    when: '8:00pm',
    where: 'Ballroom',
    description: 'Coming together as a team!',
};
const newEvent = await API.graphql(graphqlOperation(CreateEvent, eventDetails));
    
订阅
import API, { graphqlOperation } from '@aws-amplify/api';
const SubscribeToEventComments = `subscription subscribeToComments {
  subscribeToComments {
    commentId
    content
  }
}`;
const subscription = API.graphql(
    graphqlOperation(SubscribeToEventComments)
).subscribe({
    next: eventData => console.log(eventData),
});    

有关详细信息,请参阅我们的 GraphQL API开发人员指南

5.上传和下载公共或私人内容

AWS Amplify提供了一个易于使用的API,用于存储和从公共或私人存储文件夹中获取内容:

Storage.put(key, fileObj, { level: 'private' })
    .then(result => console.log(result))
    .catch(err => console.log(err));
// Store data with specifying its MIME type
Storage.put(key, fileObj, {
    level: 'private',
    contentType: 'text/plain',
})
    .then(result => console.log(result))
    .catch(err => console.log(err));    

有关详细信息,请参阅我们的存储开发人员指南

Main metrics

Overview
Name With Owneraws-amplify/amplify-js
Primary LanguageTypeScript
Program languageTypeScript (Language Count: 10)
PlatformAmazon AWS, Linux, Mac, Windows
License:Apache License 2.0
所有者活动
Created At2017-10-02 22:17:14
Pushed At2025-04-22 18:04:16
Last Commit At
Release Count8419
Last Release Nametsc-compliance-test@0.1.84 (Posted on 2025-04-21 19:48:45)
First Release Name0.1.30 (Posted on )
用户参与
Stargazers Count9.5k
Watchers Count209
Fork Count2.1k
Commits Count10k
Has Issues Enabled
Issues Count6699
Issue Open Count460
Pull Requests Count4276
Pull Requests Open Count50
Pull Requests Close Count1022
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

AWS Amplify is a JavaScript library for frontend and mobile developers building cloud-enabled applications.

AWS Amplify provides a declarative and easy-to-use interface across different categories of cloud operations. AWS Amplify goes well with any JavaScript based frontend workflow, and React Native for mobile developers.

Our default implementation works with Amazon Web Services (AWS), but AWS Amplify is designed to be open and pluggable for any custom backend or service.

Notice:

Amplify@1.x.x has structural changes. For details please check Amplify Modularization.

Features / APIs

  • Authentication: APIs and building blocks for developers who want to create user authentication experiences.
  • Analytics: Easily collect analytics data for your app. Analytics data includes user sessions and other custom events that you want to track in your app.
  • API: Provides a simple solution when making HTTP requests. It provides an automatic, lightweight signing process which complies with AWS Signature Version 4.
  • GraphQL Client: Interact with your GraphQL server or AWS AppSync API with an easy-to-use & configured GraphQL client.
  • Storage: Provides a simple mechanism for managing user content for your app in public, protected or private storage buckets.
  • Push Notifications: Allows you to integrate push notifications in your app with Amazon Pinpoint targeting and campaign management support.
  • Interactions: Create conversational bots powered by deep learning technologies.
  • PubSub: Provides connectivity with cloud-based message-oriented middleware.
  • Internationalization: A lightweight internationalization solution.
  • Cache: Provides a generic LRU cache for JavaScript developers to store data with priority and expiration settings.
  • Predictions: Provides a solution for using AI and ML cloud services to enhance your application.

Visit our Web Site to learn more about AWS Amplify.

Installation

AWS Amplify is available as aws-amplify package on npm.

Web

$ npm install aws-amplify --save

or you could install the module you want to use individually:

$ npm install @aws-amplify/auth --save

React

If you are developing a React app, you can install an additional package aws-amplify-react containing Higher Order Components:

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

Angular

If you are developing an Angular app, you can install an additional package aws-amplify-angular. This package contains an Angular module with a provider and components:

$ npm install aws-amplify --save
$ npm install aws-amplify-angular --save

Visit our Installation Guide for Web to start building your web app.

Vue

If you are developing a Vue app, you can install an additional package aws-amplify-vue. This package contains a Vue plugin for the Amplify library along with Vue components:

$ npm install aws-amplify --save
$ npm install aws-amplify-vue --save

Visit our Installation Guide for Web to start building your Vue app.

React Native

For React Native development, install aws-amplify:

$ npm install aws-amplify --save

If you are developing a React Native app, you can install an additional package aws-amplify-react-native containing Higher Order Components:

$ npm install aws-amplify-react-native --save

Visit our Installation Guide for React Native to start building your web app.

Configuration

Somewhere in your app, preferably at the root level, configure Amplify with your resources.

Using AWS Resources

import Amplify from 'aws-amplify';
import aws_exports from './aws-exports';

Amplify.configure(aws_exports);

// or if you don't want to install all the categories
import Amplify from '@aws-amplify/core';
import Auth from '@aws-amplify/auth';
import aws_exports from './aws-exports';

// in this way you are only importing Auth and configuring it.
Amplify.configure(aws_exports);

Without AWS

Amplify.configure({
	API: {
		graphql_endpoint: 'https://www.example.com/my-graphql-endpoint',
	},
});

Examples

AWS Amplify supports many category scenarios such as Auth, Analytics, APIs and Storage as outlined in the Developer Guide. A couple of samples are below:

1. Collect user session metrics

By default, AWS Amplify can collect user session tracking data with a few lines of code:

import Analytics from '@aws-amplify/analytics';

Analytics.record('myCustomEvent');

See our Analytics Developer Guide for detailed information.

2. Add Authentication to your App

Add user sign up and sign in using two of the many methods available to the Auth class:

import Auth from '@aws-amplify/auth';

Auth.signUp({
	username: 'AmandaB',
	password: 'MyCoolPassword1!',
	attributes: {
		email: 'someemail@example.com',
	},
});

Auth.signIn(username, password)
	.then(success => console.log('successful sign in'))
	.catch(err => console.log(err));

See our Authentication Developer Guide for detailed information.

React / React Native

Adding authentication to your React or React Native app is as easy as wrapping your app's main component with our withAuthenticator higher order component. AWS Amplify will provide you customizable UI for common use cases such as user registration and login.

// For React
import { withAuthenticator } from 'aws-amplify-react';

// For React Native
import { withAuthenticator } from 'aws-amplify-react-native';

export default withAuthenticator(App);

Angular

To add authentication to your Angular app you can also use the built-in service provider and components:

// app.component.ts
import { AmplifyService }  from 'aws-amplify-angular';

...

constructor( public amplify:AmplifyService ) {
  // handle auth state changes
  this.amplify.authStateChange$
    .subscribe(authState => {
      this.authenticated = authState.state === 'signedIn';
      if (!authState.user) {
        this.user = null;
      } else {
        this.user = authState.user;
      }
  });
}

// app.component.html
<amplify-authenticator></amplify-authenticator>

See our Angular Guide for more details on Angular setup and usage.

3. Sign HTTP requests

AWS Amplify automatically signs your REST requests with AWS Signature Version 4 when using the API module:

import API from '@aws-amplify/api';

let apiName = 'MyApiName';
let path = '/path';
let options = {
  headers: {...} // OPTIONAL
}
API.get(apiName, path, options).then(response => {
  // Add your code here
});

See our API Developer Guide for detailed information.

4. GraphQL API Operations

To access a GraphQL API with your app, you need to make sure to configure the endpoint URL in your app’s configuration.

// configure a custom GraphQL endpoint
Amplify.configure({
	API: {
		graphql_endpoint: 'https://www.example.com/my-graphql-endpoint',
	},
});

// Or configure an AWS AppSync endpoint.
let myAppConfig = {
	// ...
	aws_appsync_graphqlEndpoint:
		'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql',
	aws_appsync_region: 'us-east-1',
	aws_appsync_authenticationType: 'API_KEY',
	aws_appsync_apiKey: 'da2-xxxxxxxxxxxxxxxxxxxxxxxxxx',
	// ...
};

Amplify.configure(myAppConfig);

queries

import API, { graphqlOperation } from '@aws-amplify/api';

const ListEvents = `query ListEvents {
  listEvents {
    items {
      id
      where
      description
    }
  }
}`;

const allEvents = await API.graphql(graphqlOperation(ListEvents));

mutations

import API, { graphqlOperation } from '@aws-amplify/api';

const CreateEvent = `mutation CreateEvent($name: String!, $when: String!, $where: String!, $description: String!) {
  createEvent(name: $name, when: $when, where: $where, description: $description) {
    id
    name
    where
    when
    description
  }
}`;

const eventDetails = {
	name: 'Party tonight!',
	when: '8:00pm',
	where: 'Ballroom',
	description: 'Coming together as a team!',
};

const newEvent = await API.graphql(graphqlOperation(CreateEvent, eventDetails));

subscriptions

import API, { graphqlOperation } from '@aws-amplify/api';

const SubscribeToEventComments = `subscription subscribeToComments {
  subscribeToComments {
    commentId
    content
  }
}`;

const subscription = API.graphql(
	graphqlOperation(SubscribeToEventComments)
).subscribe({
	next: eventData => console.log(eventData),
});

See our GraphQL API Developer Guide for detailed information.

5. Upload and Download public or private content

AWS Amplify provides an easy-to-use API to store and get content from public or private storage folders:

Storage.put(key, fileObj, { level: 'private' })
	.then(result => console.log(result))
	.catch(err => console.log(err));

// Store data with specifying its MIME type
Storage.put(key, fileObj, {
	level: 'private',
	contentType: 'text/plain',
})
	.then(result => console.log(result))
	.catch(err => console.log(err));

See our Storage Developer Guide for detailed information.