View a markdown version of this page

使用 Amazon Amplify 库通过基于浏览器的上传来上传到 Amazon S3 - Amazon Simple Storage Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用 Amazon Amplify 库通过基于浏览器的上传来上传到 Amazon S3

本节介绍如何使用 Amazon Amplify JavaScript 库将文件上传到 Amazon S3。

有关设置 Amazon Amplify 库的信息,请参阅 Amazon Amplify 安装和配置

使用 Amazon Amplify JavaScript 库将文件上传到 Amazon S3

Amazon Amplify 库 Storage 模块提供了一种基于浏览器的简单上传机制,用于管理公有或私有 Amazon S3 存储中的用户内容。

例:Amazon Amplify 手动设置

以下示例显示了有关使用 Amazon Amplify 存储模块的手动设置。存储模块的默认实现使用 Amazon S3。

import Amplify from 'aws-amplify'; Amplify.configure( Auth: { identityPoolId: 'XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab', //REQUIRED - Amazon Cognito Identity Pool ID region: 'XX-XXXX-X', // REQUIRED - Amazon Cognito Region userPoolId: 'XX-XXXX-X_abcd1234', //OPTIONAL - Amazon Cognito User Pool ID userPoolWebClientId: 'XX-XXXX-X_abcd1234', //OPTIONAL - Amazon Cognito Web Client ID }, Storage: { bucket: '', //REQUIRED - Amazon S3 bucket region: 'XX-XXXX-X', //OPTIONAL - Amazon service region } );
例:将数据放入 Amazon S3

以下示例显示了如何将公有数据 put 到 Amazon S3 中。

Storage.put('test.txt', 'Hello') .then (result => console.log(result)) .catch(err => console.log(err));

以下示例显示了如何将私有数据 put 到 Amazon S3 中。

Storage.put('test.txt', 'Private Content', { level: 'private', contentType: 'text/plain' }) .then (result => console.log(result)) .catch(err => console.log(err));

有关使用 Amazon Amplify 存储模块的更多信息,请参阅 Amazon Amplify Storage

更多信息

Amazon Amplify 快速入门