Skip to content

Commit 93f1b14

Browse files
committed
updated storage section for multibucket support
1 parent 6c55788 commit 93f1b14

File tree

6 files changed

+252
-18
lines changed

6 files changed

+252
-18
lines changed

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,48 @@ Future<void> copy() async {
128128
}
129129
}
130130
```
131+
## Specify a bucket or copy across buckets / regions
131132

132-
## More `copy` options
133+
You can also perform an `copy` operation to a specific bucket by providing the `CopyBuckets` option.
134+
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.
135+
136+
```dart
137+
final mainBucket = StorageBucket.fromOutputs(
138+
'mainBucket',
139+
);
140+
final bucket2 = StorageBucket.fromBucketInfo(
141+
BucketInfo(
142+
bucketName: 'second-bucket-name-from-console',
143+
region: 'us-east-2',
144+
),
145+
),
146+
try {
147+
final result = await Amplify.Storage.copy(
148+
source: const StoragePath.fromString('album/2024/1.jpg'),
149+
destination: const StoragePath.fromString('shared/2024/1.jpg'),
150+
options: StorageCopyOptions(
151+
buckets: CopyBuckets(
152+
source: bucket1,
153+
destination: bucket2,
154+
),
155+
),
156+
).result;
157+
safePrint('Copied file: ${result.copiedItem.path}');
158+
} on StorageException catch (e) {
159+
print('Error: $e');
160+
}
161+
```
162+
163+
<Callout>
164+
In order to copy to or from a bucket other than your default, the source and/or destination paths must exist in that bucket
165+
</Callout>
166+
167+
## `copy` options
133168

134169
Option | Type | Description |
135170
| -- | -- | ----------- |
136171
| 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. |
172+
| 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)|
137173

138174
Example of `copy` with options:
139175

@@ -145,6 +181,9 @@ final result = Amplify.Storage.copy(
145181
pluginOptions: S3CopyPluginOptions(
146182
getProperties: true,
147183
),
184+
buckets: CopyBuckets.sameBucket(
185+
StorageBucket.fromOutputs('secondBucket'),
186+
),
148187
),
149188
);
150189
```

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

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

293295
```dart
294296
Future<void> getDownloadUrl() async {
295297
try {
296298
final result = await Amplify.Storage.getUrl(
297299
path: const StoragePath.fromString('public/example.txt'),
300+
/*
301+
// targeting a specific bucket by the name defined in the backend
302+
options: StorageGetUrlOptions(
303+
bucket: StorageBucket.fromOutputs('secondBucket'),
304+
),
305+
*/
298306
).result;
299307
safePrint('url: ${result.url}');
300308
} on StorageException catch (e) {
@@ -678,7 +686,6 @@ const result = await downloadData({
678686
}).result;
679687

680688
```
681-
682689
### Monitor download progress
683690

684691
```javascript
@@ -714,7 +721,6 @@ try {
714721
```
715722

716723
</InlineFilter>
717-
718724
<InlineFilter filters={["android"]}>
719725

720726
### Download from a specified bucket
@@ -1101,6 +1107,7 @@ Future<void> download() async {
11011107

11021108
Option | Type | Description |
11031109
| -- | -- | ----------- |
1110+
| 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) |
11041111
| 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. |
11051112
| useAccelerateEndpoint | boolean | Whether to use accelerate endpoint. <br/><br/> Read more at [Transfer Acceleration](/[platform]/build-a-backend/storage/upload-files/#transfer-acceleration) |
11061113
| bytesRange | S3DataBytesRange | The byte range to download from the object |
@@ -1116,6 +1123,7 @@ final operation = Amplify.Storage.downloadFile(
11161123
getProperties: true,
11171124
useAccelerateEndpoint: true,
11181125
),
1126+
bucket: StorageBucket.fromOutputs('secondBucket'),
11191127
),
11201128
);
11211129
```
@@ -1131,6 +1139,12 @@ final operation = Amplify.Storage.downloadData(
11311139
useAccelerateEndpoint: true,
11321140
bytesRange: S3DataBytesRange(start: 0, end: 100),
11331141
),
1142+
bucket: StorageBucket.fromBucketInfo(
1143+
BucketInfo(
1144+
bucketName: 'second-bucket-name-from-console',
1145+
region: 'us-east-2',
1146+
),
1147+
),
11341148
),
11351149
);
11361150
```

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,11 +1010,47 @@ Future<void> listAllUnderPublicPath() async {
10101010
}
10111011

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

10141049
### More `list` options
10151050

10161051
| Option | Type | Description |
10171052
| --- | --- | --- |
1053+
| 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) |
10181054
| excludeSubPaths | boolean | Whether to exclude objects under the sub paths of the path to list. Defaults to false. |
10191055
| delimiter | String | The delimiter to use when evaluating sub paths. If excludeSubPaths is false, this value has no impact on behavior. |
10201056

@@ -1093,6 +1129,35 @@ Future<void> getFileProperties() async {
10931129
}
10941130
```
10951131

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

10981163
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

Lines changed: 40 additions & 14 deletions
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 from a specific bucket by providing the target bucket's assigned name from Amplify Backend in `bucket` option.
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

Lines changed: 48 additions & 0 deletions
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.

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,10 +647,52 @@ Future<void> upload() async {
647647

648648
<InlineFilter filters={["flutter"]}>
649649

650+
### Upload to a specified bucket
651+
652+
You can also perform an `upload` operation to a specific bucket by providing the `bucket` option. You can pass in an object representing the target bucket in Amplify Backend.
653+
654+
```dart
655+
final data = 'multi bucket upload data byte'.codeUnits;
656+
final result = await Amplify.Storage.uploadData(
657+
data: StorageDataPayload.bytes(data),
658+
path: const StoragePath.fromString('path/to/file.txt'),
659+
options: StorageUploadDataOptions(
660+
// highlight-start
661+
// Specify a target bucket using name assigned in Amplify Backend
662+
bucket: StorageBucket.fromOutputs('secondBucket'),
663+
// highlight-end
664+
),
665+
).result;
666+
```
667+
Alternatively, you can also pass in an object by specifying the bucket name and region from the console.
668+
669+
```dart
670+
final data = 'multi bucket upload data byte'.codeUnits;
671+
final result = await Amplify.Storage.uploadData(
672+
data: StorageDataPayload.bytes(data),
673+
path: const StoragePath.fromString('path/to/file.txt'),
674+
options: StorageUploadDataOptions(
675+
// highlight-start
676+
// Alternatively, provide bucket name from console and associated region
677+
bucket: StorageBucket.fromBucketInfo(
678+
BucketInfo(
679+
bucketName: 'second-bucket-name-from-console',
680+
region: 'us-east-2',
681+
),
682+
),
683+
// highlight-end
684+
),
685+
).result;
686+
```
687+
</InlineFilter>
688+
689+
<InlineFilter filters={["flutter"]}>
690+
650691
### More upload options
651692

652693
Option | Type | Description |
653694
| -- | -- | ----------- |
695+
| 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 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) |
654696
| getProperties | boolean | Whether to retrieve properties for the uploaded 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. |
655697
| useAccelerateEndpoint | boolean | Whether to use accelerate endpoint. <br/><br/> Read more at [Transfer Acceleration](/[platform]/build-a-backend/storage/upload-files/#transfer-acceleration) |
656698

0 commit comments

Comments
 (0)