Use transfer acceleration
You can enable Transfer Acceleration for fast and secure transfer of files over long distances between your end user device and the S3 bucket. You can override the storage resource for this configuration and then leverage the useAccelerateEndpoint
parameter to use the accelerated S3 endpoint.
Override storage resource
Start by overriding your storage resources to enable Transfer Acceleration on your S3 bucket.
$ 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
In the generated override.ts
file use the following CDK snippet to enable transfer acceleration.
// amplify/backend/storage/accelerated-bucket/override.tsimport { AmplifyS3ResourceTemplate } from '@aws-amplify/cli-extensibility-helper';
export function override(resources: AmplifyS3ResourceTemplate) { resources.s3Bucket.accelerateConfiguration = { accelerationStatus: 'Enabled' }}
Next, deploy this storage resource:
amplify push
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
.
With StoragePath
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) );
With Key (Deprecated)
AWSS3StorageUploadFileOptions awsS3StorageUploadFileOptions = AWSS3StorageUploadFileOptions.builder().setUseAccelerateEndpoint(true).build(); Amplify.Storage.uploadFile( "KEY", file awsS3StorageUploadFileOptions, result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()), storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure));
val awsS3StorageUploadFileOptions = AWSS3StorageUploadFileOptions.builder(). setUseAccelerateEndpoint(true). build() Amplify.Storage.uploadFile( "KEY", file awsS3StorageUploadFileOptions, { Log.i("MyAmplifyApp", "Successfully uploaded: " + it.getKey()) }, { Log.e("MyAmplifyApp", "Upload failed", it) })
val awsS3StorageUploadFileOptions = AWSS3StorageUploadFileOptions.builder(). setUseAccelerateEndpoint(true). build()val upload = Amplify.Storage.uploadFile("KEY", file, awsS3StorageUploadFileOptions)try { val result = upload.result() Log.i("MyAmplifyApp", "Successfully uploaded: ${result.key}")} catch (error: StorageException) { Log.e("MyAmplifyApp", "Upload failed", error)}
AWSS3StorageUploadFileOptions awsS3StorageUploadFileOptions = AWSS3StorageUploadFileOptions.builder().setUseAccelerateEndpoint(true).build();RxProgressAwareSingleOperation<StorageUploadFileResult> rxUploadOperation = RxAmplify.Storage.uploadFile("KEY", file, awsS3StorageUploadFileOptions);rxUploadOperation .observeResult() .subscribe( result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()), error -> Log.e("MyAmplifyApp", "Upload failed", error) );