Skip to content

feat(android): Add new page for S3 acceleration endpoint #5214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/directory/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ const directory = {
{
title: 'Use Transfer Acceleration',
route: '/lib/storage/transfer-acceleration',
filters: ['js', 'react-native']
filters: ['android', 'js', 'react-native']
},
{
title: 'Lambda triggers',
Expand Down
108 changes: 108 additions & 0 deletions src/fragments/lib/storage/android/transfer-acceleration.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<Callout warning>

When you use Transfer Acceleration, additional data transfer charges might apply. For more information about pricing, see [Amazon S3 pricing](https://aws.amazon.com/s3/pricing/).

</Callout>

You can enable [Transfer Acceleration](https://docs.aws.amazon.com/AmazonS3/latest/userguide/transfer-acceleration.html) 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.

```sh
$ 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) · yes
Edit 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.

```typescript
// amplify/backend/storage/accelerated-bucket/override.ts
import { AmplifyS3ResourceTemplate } from '@aws-amplify/cli-extensibility-helper';

export function override(resources: AmplifyS3ResourceTemplate) {
resources.s3Bucket.accelerateConfiguration = {
accelerationStatus: 'Enabled'
}
}
```

Next, deploy this storage resource:

```sh
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`.
<BlockSwitcher>

<Block name="Java">

```java
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)
);
```
</Block>
<Block name="Kotlin - Callbacks">

```kotlin
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) }
)
```
</Block>
<Block name="Kotlin - Coroutines">

```kotlin
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)
}
)
```
</Block>
<Block name="RxJava">

```java
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)
);
);
```
</Block>
</BlockSwitcher>
<Callout>Note: useAccelerateEndpoint flag is available in AWSS3StorageUploadFileOptions, AWSS3StorageUploadInputStreamOptions, AWSS3StorageDownloadFileOptions and AWSS3StorageGetPresignedUrlOptions</Callout>
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ export const meta = {

import js from "/src/fragments/lib/storage/js/transfer-acceleration.mdx";

<Fragments fragments={{js: js, 'react-native': js}} />
<Fragments fragments={{js: js, 'react-native': js}} />

import android2 from '/src/fragments/lib/storage/android/transfer-acceleration.mdx';

<Fragments fragments={{ android: android2 }} />