トランスファーアクセラレーションを使用する
トランスファーアクセラレーションを有効にして、エンドユーザーデバイスとS3バケット間で長距離にわたるファイルの高速かつ安全な転送を行うことができます。このこのこの構成のストレージリソースをオーバーライドしてから、useAccelerateEndpointパラメータを使用して、加速されたS3エンドポイントを活用できます。
ストレージリソースをオーバーライドする
S3バケットでトランスファーアクセラレーションを有効にするために、ストレージリソースをオーバーライドすることから始めます。
$ amplify override storage✅ Successfully generated "override.ts" folder at <project>/amplify/backend/storage/accelerated-bucket✔ Do you want to edit override.ts file now? (Y/n) · yesEdit the file in your editor: <project>/amplify/backend/storage/accelerated-bucket/override.ts生成されたoverride.tsファイルで、次のCDKスニペットを使用してトランスファーアクセラレーションを有効にします。
// amplify/backend/storage/accelerated-bucket/override.tsimport { AmplifyS3ResourceTemplate } from '@aws-amplify/cli-extensibility-helper';
export function override(resources: AmplifyS3ResourceTemplate) { resources.s3Bucket.accelerateConfiguration = { accelerationStatus: 'Enabled' }}次に、このストレージリソースをデプロイします。
amplify pushストレージ操作でトランスファーアクセラレーションを使用する
次のAPIを呼び出す際にトランスファーアクセラレーションを使用できます:
getUrldownloadDatadownloadFileuploadDatauploadFile
対応するStorage S3プラグインオプションでuseAccelerateEndpointをtrueに設定して、加速されたS3エンドポイントを操作に適用します。たとえば、トランスファーアクセラレーションを使用してファイルをアップロードします:
import 'package:amplify_storage_s3/amplify_storage_s3.dart';
Future<void> uploadFileUsingAcceleration(String filePath, String path) async { final localFile = AWSFile.fromPath(filePath); try { final uploadFileOperation = Amplify.Storage.uploadFile( localFile: localFile, path: const StoragePath.fromString(path), options: const StorageUploadFileOptions( pluginOptions: S3UploadFilePluginOptions( useAccelerateEndpoint: true, ), ), );
final result = await uploadFileOperation.result; safePrint('Uploaded file: ${result.uploadedItem.path}'); } on StorageException catch (error) { safePrint('Something went wrong uploading file: ${error.message}'); }}