使用 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