Extend S3 resources
For Amplify-generated S3 resources
Amplify Storage generates Amazon S3 resources to offer storage features. You can access the underlying Amazon S3 resources to further customize your backend configuration by using the AWS Cloud Developer Kit (AWS CDK).
Example - Enable Transfer Acceleration
The following is an example of how you would enable Transfer Acceleration on the bucket (CDK documentation). In order to enable Transfer Acceleration on the bucket, you will have to unwrap the L1 CDK construct from the L2 CDK construct like the following.
import * as s3 from 'aws-cdk-lib/aws-s3';import { defineBackend } from '@aws-amplify/backend';import { storage } from './storage/resource';
const backend = defineBackend({ storage});
const s3Bucket = backend.storage.resources.bucket;
const cfnBucket = s3Bucket.node.defaultChild as s3.CfnBucket;
cfnBucket.accelerateConfiguration = { accelerationStatus: "Enabled" // 'Suspended' if you want to disable transfer acceleration}
Upload files using the accelerated S3 endpoint
We switch to the accelerated S3 endpoint by using the useAccelerateEndpoint
parameter set to true
in the AWSS3StorageUploadFileOptions
.
AWSS3StorageUploadFileOptions awsS3StorageUploadFileOptions = AWSS3StorageUploadFileOptions.builder().setUseAccelerateEndpoint(true).build(); Amplify.Storage.uploadFile( StoragePath.fromString("public/example"), file awsS3StorageUploadFileOptions, result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()), storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure));
val awsS3StorageUploadFileOptions = AWSS3StorageUploadFileOptions.builder(). setUseAccelerateEndpoint(true). build() Amplify.Storage.uploadFile( StoragePath.fromString("public/example"), file awsS3StorageUploadFileOptions, { Log.i("MyAmplifyApp", "Successfully uploaded: " + it.getPath()) }, { Log.e("MyAmplifyApp", "Upload failed", it) })
val awsS3StorageUploadFileOptions = AWSS3StorageUploadFileOptions.builder(). setUseAccelerateEndpoint(true). build()val upload = Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), file, awsS3StorageUploadFileOptions)try { val result = upload.result() Log.i("MyAmplifyApp", "Successfully uploaded: ${result.path}")} catch (error: StorageException) { Log.e("MyAmplifyApp", "Upload failed", error)}
AWSS3StorageUploadFileOptions awsS3StorageUploadFileOptions = AWSS3StorageUploadFileOptions.builder().setUseAccelerateEndpoint(true).build();RxProgressAwareSingleOperation<StorageUploadFileResult> rxUploadOperation = RxAmplify.Storage.uploadFile(StoragePath.fromString("public/example"), file, awsS3StorageUploadFileOptions);rxUploadOperation .observeResult() .subscribe( result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()), error -> Log.e("MyAmplifyApp", "Upload failed", error) );
Read more about escape hatches in the CDK.