本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 AWS Amplify 程式庫將瀏覽器型上傳到 Amazon S3
本節說明如何使用 AWS Amplify JavaScript 程式庫將檔案上傳至 Amazon S3。
如需設定 AWS Amplify 程式庫的詳細資訊,請參閱 AWS Amplify 安裝和組態
使用 AWS Amplify JavaScript 程式庫將檔案上傳至 Amazon S3
AWS Amplify 程式庫Storage模組提供簡單的瀏覽器型上傳機制,用於管理公有或私有 Amazon S3 儲存體中的使用者內容。
範例: AWS Amplify 手動設定
下列範例顯示使用 AWS Amplify Storage 模組的手動設定。Storage 模組的預設實作使用 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));
如需使用 AWS Amplify Storage 模組的詳細資訊,請參閱 AWS Amplify Storage