Skip to content

Commit ab08e64

Browse files
sdhukaRachel Lee Nabors
authored andcommitted
feat(android): Add new page for S3 acceleration endpoint (#5214)
* feat(android): Add new page for S3 acceleration endpoint * address comments * remove semi-colon from kotlin snippet
1 parent 76d6f90 commit ab08e64

File tree

3 files changed

+114
-2
lines changed

3 files changed

+114
-2
lines changed

src/directory/directory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ const directory = {
781781
{
782782
title: 'Use Transfer Acceleration',
783783
route: '/lib/storage/transfer-acceleration',
784-
filters: ['js', 'react-native']
784+
filters: ['android', 'js', 'react-native']
785785
},
786786
{
787787
title: 'Lambda triggers',
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<Callout warning>
2+
3+
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/).
4+
5+
</Callout>
6+
7+
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.
8+
9+
## Override storage resource
10+
11+
Start by overriding your storage resources to enable Transfer Acceleration on your S3 bucket.
12+
13+
```sh
14+
$ amplify override storage
15+
✅ Successfully generated "override.ts" folder at <project>/amplify/backend/storage/accelerated-bucket
16+
✔ Do you want to edit override.ts file now? (Y/n) · yes
17+
Edit the file in your editor: <project>/amplify/backend/storage/accelerated-bucket/override.ts
18+
```
19+
20+
In the generated `override.ts` file use the following CDK snippet to enable transfer acceleration.
21+
22+
```typescript
23+
// amplify/backend/storage/accelerated-bucket/override.ts
24+
import { AmplifyS3ResourceTemplate } from '@aws-amplify/cli-extensibility-helper';
25+
26+
export function override(resources: AmplifyS3ResourceTemplate) {
27+
resources.s3Bucket.accelerateConfiguration = {
28+
accelerationStatus: 'Enabled'
29+
}
30+
}
31+
```
32+
33+
Next, deploy this storage resource:
34+
35+
```sh
36+
amplify push
37+
```
38+
39+
40+
41+
## Upload files using the accelerated S3 endpoint
42+
43+
We switch to the accelerated S3 endpoint by using the `useAccelerateEndpoint` parameter set to `true` in the `AWSS3StorageUploadFileOptions`.
44+
<BlockSwitcher>
45+
46+
<Block name="Java">
47+
48+
```java
49+
AWSS3StorageUploadFileOptions awsS3StorageUploadFileOptions =
50+
AWSS3StorageUploadFileOptions.builder().setUseAccelerateEndpoint(true).build();
51+
Amplify.Storage.uploadFile(
52+
"KEY",
53+
file
54+
awsS3StorageUploadFileOptions,
55+
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),
56+
storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
57+
);
58+
```
59+
</Block>
60+
<Block name="Kotlin - Callbacks">
61+
62+
```kotlin
63+
val awsS3StorageUploadFileOptions = AWSS3StorageUploadFileOptions.builder().
64+
setUseAccelerateEndpoint(true).
65+
build()
66+
Amplify.Storage.uploadFile(
67+
"KEY",
68+
file
69+
awsS3StorageUploadFileOptions,
70+
{ Log.i("MyAmplifyApp", "Successfully uploaded: " + it.getKey()) },
71+
{ Log.e("MyAmplifyApp", "Upload failed", it) }
72+
)
73+
```
74+
</Block>
75+
<Block name="Kotlin - Coroutines">
76+
77+
```kotlin
78+
val awsS3StorageUploadFileOptions = AWSS3StorageUploadFileOptions.builder().
79+
setUseAccelerateEndpoint(true).
80+
build()
81+
val upload = Amplify.Storage.uploadFile("KEY", file, awsS3StorageUploadFileOptions)
82+
try {
83+
val result = upload.result()
84+
Log.i("MyAmplifyApp", "Successfully uploaded: ${result.key}")
85+
} catch (error: StorageException) {
86+
Log.e("MyAmplifyApp", "Upload failed", error)
87+
}
88+
)
89+
```
90+
</Block>
91+
<Block name="RxJava">
92+
93+
```java
94+
AWSS3StorageUploadFileOptions awsS3StorageUploadFileOptions =
95+
AWSS3StorageUploadFileOptions.builder().setUseAccelerateEndpoint(true).build();
96+
RxProgressAwareSingleOperation<StorageUploadFileResult> rxUploadOperation =
97+
RxAmplify.Storage.uploadFile("KEY", file, awsS3StorageUploadFileOptions);
98+
rxUploadOperation
99+
.observeResult()
100+
.subscribe(
101+
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),
102+
error -> Log.e("MyAmplifyApp", "Upload failed", error)
103+
);
104+
);
105+
```
106+
</Block>
107+
</BlockSwitcher>
108+
<Callout>Note: useAccelerateEndpoint flag is available in AWSS3StorageUploadFileOptions, AWSS3StorageUploadInputStreamOptions, AWSS3StorageDownloadFileOptions and AWSS3StorageGetPresignedUrlOptions</Callout>

src/pages/lib/storage/transfer-acceleration/q/platform/[platform].mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ export const meta = {
55

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

8-
<Fragments fragments={{js: js, 'react-native': js}} />
8+
<Fragments fragments={{js: js, 'react-native': js}} />
9+
10+
import android2 from '/src/fragments/lib/storage/android/transfer-acceleration.mdx';
11+
12+
<Fragments fragments={{ android: android2 }} />

0 commit comments

Comments
 (0)