Skip to content

Commit 1b54cf6

Browse files
authored
amplify-libraries Flutter multibucket support documentation update (#8115)
* updated storage section for multibucket support * updated api explanations for consistency * removed section on passwordless since flutter doesnt support it yet
1 parent 19b6501 commit 1b54cf6

File tree

7 files changed

+285
-18
lines changed

7 files changed

+285
-18
lines changed

src/pages/[platform]/build-a-backend/auth/modify-resources-with-cdk/index.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ cfnUserPool.enabledMfas = [...(cfnUserPool.enabledMfas || []), "EMAIL_OTP"]
117117
{/* BYO custom idp construct */}
118118
{/* extend auth/unauth roles */}
119119

120+
<InlineFilter filters={["android","angular", "javascript", "nextjs", "react", "react-native", "swift", "vue"]}>
121+
120122
### Override Cognito UserPool to enable passwordless sign-in methods
121123

122124
You can modify the underlying Cognito user pool resource to enable sign in with passwordless methods. [Learn more about passwordless sign-in methods](/[platform]/build-a-backend/auth/concepts/passwordless/).
@@ -146,3 +148,4 @@ cfnUserPoolClient.explicitAuthFlows = [
146148
cfnUserPool.addPropertyOverride('WebAuthnRelyingPartyID', '<RELYING_PARTY>');
147149
cfnUserPool.addPropertyOverride('WebAuthnUserVerification', 'preferred');
148150
```
151+
</InlineFilter>

src/pages/[platform]/build-a-backend/storage/copy-files/index.mdx

+40-1
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,48 @@ Future<void> copy() async {
139139
}
140140
}
141141
```
142+
## Specify a bucket or copy across buckets / regions
142143

143-
## More `copy` options
144+
You can also perform a `copy` operation to a specific bucket by providing the `CopyBuckets` option.
145+
This option is an object that takes two `StorageBucket` parameters, which can be constructed by the specified name in the Amplify Backend, or the bucket name and region from the console.
146+
147+
```dart
148+
final mainBucket = StorageBucket.fromOutputs(
149+
'mainBucket',
150+
);
151+
final bucket2 = StorageBucket.fromBucketInfo(
152+
BucketInfo(
153+
bucketName: 'second-bucket-name-from-console',
154+
region: 'us-east-2',
155+
),
156+
),
157+
try {
158+
final result = await Amplify.Storage.copy(
159+
source: const StoragePath.fromString('album/2024/1.jpg'),
160+
destination: const StoragePath.fromString('shared/2024/1.jpg'),
161+
options: StorageCopyOptions(
162+
buckets: CopyBuckets(
163+
source: bucket1,
164+
destination: bucket2,
165+
),
166+
),
167+
).result;
168+
safePrint('Copied file: ${result.copiedItem.path}');
169+
} on StorageException catch (e) {
170+
print('Error: $e');
171+
}
172+
```
173+
174+
<Callout>
175+
In order to copy to or from a bucket other than your default, the source and/or destination paths must exist in that bucket
176+
</Callout>
177+
178+
## `copy` options
144179

145180
Option | Type | Description |
146181
| -- | -- | ----------- |
147182
| getProperties | boolean | Whether to retrieve properties for the copied object using theAmplify.Storage.getProperties() after the operation completes. When set to true the returned item will contain additional info such as metadata and content type. |
183+
| buckets | CopyBuckets | An object that accepts two `StorageBucket` parameters. To copy to and from the same bucket, use the `CopyBuckets.sameBucket(targetBucket)` method, where `targetBucket` is a `StorageBucket`. Read more at [Configure additional storage buckets](/[platform]/build-a-backend/storage/set-up-storage/#configure-additional-storage-buckets)|
148184

149185
Example of `copy` with options:
150186

@@ -156,6 +192,9 @@ final result = Amplify.Storage.copy(
156192
pluginOptions: S3CopyPluginOptions(
157193
getProperties: true,
158194
),
195+
buckets: CopyBuckets.sameBucket(
196+
StorageBucket.fromOutputs('secondBucket'),
197+
),
159198
),
160199
);
161200
```

src/pages/[platform]/build-a-backend/storage/download-files/index.mdx

+47-3
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,21 @@ Option | Type | Description |
291291
When creating a downloadable URL, you can choose to check if the file exists by setting `validateObjectExistence` to
292292
`true` in `S3GetUrlPluginOptions`. If the file is inaccessible or does not exist, a `StorageException` is thrown.
293293
This allows you to check if an object exists during generating the presigned URL, which you can then use to download
294-
that object.
294+
that object. You may also pass in a bucket to target into `StorageGetUrlOptions` from either the chosen name in the
295+
backend or the console name and region. If no bucket is provided, the default bucket defined in the backend will be used.
296+
Read more at [Configure additional storage buckets](/[platform]/build-a-backend/storage/set-up-storage/#configure-additional-storage-buckets)
295297

296298
```dart
297299
Future<void> getDownloadUrl() async {
298300
try {
299301
final result = await Amplify.Storage.getUrl(
300302
path: const StoragePath.fromString('public/example.txt'),
303+
/*
304+
// targeting a specific bucket by the name defined in the backend
305+
options: StorageGetUrlOptions(
306+
bucket: StorageBucket.fromOutputs('secondBucket'),
307+
),
308+
*/
301309
).result;
302310
safePrint('url: ${result.url}');
303311
} on StorageException catch (e) {
@@ -681,7 +689,6 @@ const result = await downloadData({
681689
}).result;
682690

683691
```
684-
685692
### Monitor download progress
686693

687694
```javascript
@@ -717,7 +724,6 @@ try {
717724
```
718725

719726
</InlineFilter>
720-
721727
<InlineFilter filters={["android"]}>
722728

723729
### Download from a specified bucket
@@ -1104,6 +1110,7 @@ Future<void> download() async {
11041110

11051111
Option | Type | Description |
11061112
| -- | -- | ----------- |
1113+
| bucket | StorageBucket | The target bucket from the assigned name in the Amplify Backend or from the bucket name and region in the console<br/><br/>Defaults to the default bucket and region from the Amplify configuration if this option is not provided.<br/><br/>Read more at [Configure additional storage buckets](/[platform]/build-a-backend/storage/set-up-storage/#configure-additional-storage-buckets) |
11071114
| getProperties | boolean | Whether to retrieve properties for the downloaded object using theAmplify.Storage.getProperties() after the operation completes. When set to true the returned item will contain additional info such as metadata and content type. |
11081115
| useAccelerateEndpoint | boolean | Whether to use accelerate endpoint. <br/><br/> Read more at [Transfer Acceleration](/[platform]/build-a-backend/storage/upload-files/#transfer-acceleration) |
11091116
| bytesRange | S3DataBytesRange | The byte range to download from the object |
@@ -1119,6 +1126,7 @@ final operation = Amplify.Storage.downloadFile(
11191126
getProperties: true,
11201127
useAccelerateEndpoint: true,
11211128
),
1129+
bucket: StorageBucket.fromOutputs('secondBucket'),
11221130
),
11231131
);
11241132
```
@@ -1138,6 +1146,42 @@ final operation = Amplify.Storage.downloadData(
11381146
);
11391147
```
11401148

1149+
You can also perform a `downloadData` or `downloadFile` operation to a specific bucket by providing the `bucket` option. You can pass in a `StorageBucket` object representing the target bucket from the name defined in the Amplify Backend.
1150+
1151+
```dart
1152+
final operation = Amplify.Storage.downloadFile(
1153+
path: const StoragePath.fromString('public/example.txt'),
1154+
localFile: AWSFile.fromPath('/path/to/local/file.txt'),
1155+
options: const StorageDownloadFileOptions(
1156+
pluginOptions: S3DownloadFilePluginOptions(
1157+
getProperties: true,
1158+
useAccelerateEndpoint: true,
1159+
),
1160+
bucket: StorageBucket.fromOutputs('secondBucket'),
1161+
),
1162+
);
1163+
```
1164+
1165+
Alternatively, you can also pass in an object by specifying the bucket name and region from the console.
1166+
1167+
```dart
1168+
final operation = Amplify.Storage.downloadData(
1169+
path: const StoragePath.fromString('public/example.txt'),
1170+
options: StorageDownloadDataOptions(
1171+
pluginOptions: S3DownloadDataPluginOptions(
1172+
getProperties: true,
1173+
useAccelerateEndpoint: true,
1174+
bytesRange: S3DataBytesRange(start: 0, end: 100),
1175+
),
1176+
bucket: StorageBucket.fromBucketInfo(
1177+
BucketInfo(
1178+
bucketName: 'second-bucket-name-from-console',
1179+
region: 'us-east-2',
1180+
),
1181+
),
1182+
),
1183+
);
1184+
```
11411185
</InlineFilter>
11421186

11431187
<InlineFilter filters={["swift"]}>

src/pages/[platform]/build-a-backend/storage/list-files/index.mdx

+65
Original file line numberDiff line numberDiff line change
@@ -1012,11 +1012,47 @@ Future<void> listAllUnderPublicPath() async {
10121012
}
10131013

10141014
```
1015+
<InlineFilter filters={["flutter"]}>
1016+
1017+
### List files from a specified bucket
1018+
You can also perform a `list` operation to a specific bucket by providing the `bucket` option. You can pass in a `StorageBucket` object representing the target bucket from the name defined in the Amplify Backend.
1019+
1020+
```dart
1021+
final result = await Amplify.Storage.list(
1022+
path: const StoragePath.fromString('path/to/dir'),
1023+
options: StorageListOptions(
1024+
// highlight-start
1025+
// Specify a target bucket using name assigned in Amplify Backend
1026+
bucket: StorageBucket.fromOutputs('secondBucket'),
1027+
// highlight-end
1028+
),
1029+
).result;
1030+
```
1031+
Alternatively, you can also pass in an object by specifying the bucket name and region from the console.
1032+
1033+
```dart
1034+
final result = await Amplify.Storage.list(
1035+
path: const StoragePath.fromString('path/to/dir'),
1036+
options: StorageListOptions(
1037+
// highlight-start
1038+
// Alternatively, provide bucket name from console and associated region
1039+
bucket: StorageBucket.fromBucketInfo(
1040+
BucketInfo(
1041+
bucketName: 'second-bucket-name-from-console',
1042+
region: 'us-east-2',
1043+
),
1044+
),
1045+
// highlight-end
1046+
),
1047+
).result;
1048+
```
1049+
</InlineFilter>
10151050

10161051
### More `list` options
10171052

10181053
| Option | Type | Description |
10191054
| --- | --- | --- |
1055+
| bucket | StorageBucket | The target bucket from the assigned name in the Amplify Backend or from the bucket name and region in the console<br/><br/>Defaults to the default bucket and region from the Amplify configuration if this option is not provided.<br/><br/>Read more at [Configure additional storage buckets](/[platform]/build-a-backend/storage/set-up-storage/#configure-additional-storage-buckets) |
10201056
| excludeSubPaths | boolean | Whether to exclude objects under the sub paths of the path to list. Defaults to false. |
10211057
| delimiter | String | The delimiter to use when evaluating sub paths. If excludeSubPaths is false, this value has no impact on behavior. |
10221058

@@ -1095,6 +1131,35 @@ Future<void> getFileProperties() async {
10951131
}
10961132
```
10971133

1134+
As well as specify a bucket to target to view properties of a file
1135+
1136+
```dart
1137+
Future<void> getFileProperties() async {
1138+
try {
1139+
final result = await Amplify.Storage.getProperties(
1140+
path: const StoragePath.fromString('example.txt'),
1141+
options: StorageGetPropertiesOptions(
1142+
StorageBucket.fromOutputs('secondBucket'),
1143+
),
1144+
// Alternatively, provide bucket name from console and associated region
1145+
/*
1146+
options: StorageGetPropertiesOptions(
1147+
bucket: StorageBucket.fromBucketInfo(
1148+
BucketInfo(
1149+
bucketName: 'second-bucket-name-from-console',
1150+
region: 'us-east-2',
1151+
),
1152+
),
1153+
),
1154+
*/
1155+
).result;
1156+
safePrint('File size: ${result.storageItem.size}');
1157+
} on StorageException catch (e) {
1158+
safePrint(e.message);
1159+
}
1160+
}
1161+
```
1162+
10981163
<Callout>
10991164

11001165
To get the metadata in result for all APIs when building against a web target, you have to configure user defined metadata in CORS.

src/pages/[platform]/build-a-backend/storage/remove-files/index.mdx

+40-14
Original file line numberDiff line numberDiff line change
@@ -341,28 +341,42 @@ let removedObject = try await Amplify.Storage.remove(
341341

342342
<InlineFilter filters={["flutter"]}>
343343

344-
## Remove single file
345-
346-
You can remove a single file using `Amplify.Storage.remove` with the `key` and its associated access level:
344+
You can also perform a `remove` operation to a specific bucket by providing the `bucket` option. You can pass in a `StorageBucket` object representing the target bucket from the name defined in the Amplify Backend.
347345

348346
```dart
347+
final result = await Amplify.Storage.remove(
348+
path: const StoragePath.fromString('path/to/file.txt'),
349+
options: StorageRemoveOptions(
350+
// highlight-start
351+
// Specify a target bucket using name assigned in Amplify Backend
352+
bucket: StorageBucket.fromOutputs('secondBucket'),
353+
// highlight-end
354+
),
355+
).result;
356+
```
349357

350-
Future<void> removeFile() async {
351-
try {
352-
final result = await Amplify.Storage.remove(
353-
path: const StoragePath.fromString('public/file.txt'),
354-
).result;
355-
safePrint('Removed file: ${result.removedItem.path}');
356-
} on StorageException catch (e) {
357-
safePrint(e.message);
358-
}
359-
}
358+
Alternatively, you can also pass in an object by specifying the bucket name and region from the console.
360359

360+
```dart
361+
final result = await Amplify.Storage.remove(
362+
path: const StoragePath.fromString('path/to/file.txt'),
363+
options: StorageRemoveOption(
364+
// highlight-start
365+
// Alternatively, provide bucket name from console and associated region
366+
bucket: StorageBucket.fromBucketInfo(
367+
BucketInfo(
368+
bucketName: 'second-bucket-name-from-console',
369+
region: 'us-east-2',
370+
),
371+
),
372+
// highlight-end
373+
),
374+
).result;
361375
```
362376

363377
## Remove multiple Files
364378

365-
You can remove multiple files using `Amplify.Storage.removeMany` with the `keys`, the files to be removed in a batch should have the same access level:
379+
You can remove multiple files using `Amplify.Storage.removeMany`, as well as specify a bucket to target, the files to be removed in a batch should have the same access level:
366380

367381
```dart
368382
Future<void> remove() async {
@@ -372,6 +386,18 @@ Future<void> remove() async {
372386
const StoragePath.fromString('public/file-1.txt'),
373387
const StoragePath.fromString('public/file-2.txt'),
374388
],
389+
// if this option is not provided, the default bucket in the Amplify Backend will be used
390+
options: StorageRemoveManyOptions(
391+
bucket: StorageBucket.fromOutputs('secondBucket'),
392+
/* Alternatively, provide bucket name from console and associated region
393+
bucket: StorageBucket.fromBucketInfo(
394+
BucketInfo(
395+
bucketName: 'second-bucket-name-from-console',
396+
region: 'us-east-2',
397+
),
398+
),
399+
*/
400+
),
375401
).result;
376402
safePrint('Removed files: ${result.removedItems}');
377403
} on StorageException catch (e) {

src/pages/[platform]/build-a-backend/storage/set-up-storage/index.mdx

+48
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,54 @@ let downloadTask = Amplify.Storage.downloadData(
363363
```
364364
</InlineFilter>
365365

366+
<InlineFilter filters={["flutter"]}>
367+
### Storage bucket client usage
368+
369+
Additional storage buckets can be referenced from application code by passing the `bucket` option to Amplify Storage APIs. You can provide a target bucket's name assigned in Amplify Backend.
370+
371+
```dart
372+
import 'package:amplify_flutter/amplify_flutter.dart';
373+
374+
try {
375+
final result = await Amplify.Storage.downloadData(
376+
path: const StoragePath.fromString('album/2024/1.jpg'),
377+
options: StorageDownloadDataOptions(
378+
// highlight-start
379+
// Specify a target bucket using name assigned in Amplify Backend
380+
bucket: StorageBucket.fromOutputs('secondBucket'),
381+
// highlight-end
382+
),
383+
).result;
384+
} on Exception catch (e) {
385+
print('Error: $e');
386+
}
387+
```
388+
Alternatively, you can also pass in an object by specifying the bucket name and region from the console. See each Amplify Storage API page for additional usage examples.
389+
390+
```dart
391+
import 'package:amplify_flutter/amplify_flutter.dart';
392+
393+
try {
394+
final result = await Amplify.Storage.downloadData(
395+
path: const StoragePath.fromString('album/2024/1.jpg'),
396+
options: const StorageDownloadDataOptions(
397+
// highlight-start
398+
// Alternatively, provide bucket name from console and associated region
399+
bucket: StorageBucket.fromBucketInfo(
400+
BucketInfo(
401+
bucketName: 'second-bucket-name-from-console',
402+
region: 'us-east-2',
403+
),
404+
),
405+
// highlight-end
406+
),
407+
).result;
408+
} on Exception catch (e) {
409+
print('Error: $e');
410+
}
411+
```
412+
</InlineFilter>
413+
366414
## Connect your app code to the storage backend
367415

368416
The Amplify Storage library provides client APIs that connect to the backend resources you defined.

0 commit comments

Comments
 (0)