Skip to content

Commit fe9c40c

Browse files
committed
feat(Storage)[Swift]: Adding documentation for multi-bucket support
1 parent 49a9d66 commit fe9c40c

File tree

5 files changed

+253
-11
lines changed

5 files changed

+253
-11
lines changed

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

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ let url = try await Amplify.Storage.getURL(
276276
Option | Type | Description |
277277
| -- | -- | ----------- |
278278
| expires | Integer | Number of seconds before the URL expires |
279+
| bucket | StorageBucket | The bucket in which the file is stored |
279280

280281
</InlineFilter>
281282

@@ -392,14 +393,14 @@ You can download to a file [URL](https://developer.apple.com/documentation/found
392393

393394
<Block name="Async/Await">
394395
```swift
395-
let downloadToFileName = FileManager.default.urls(
396+
let downloadToFileUrl = FileManager.default.urls(
396397
for: .documentDirectory,
397398
in: .userDomainMask
398399
)[0].appendingPathComponent("myFile.txt")
399400

400401
let downloadTask = Amplify.Storage.downloadFile(
401402
path: .fromString("public/example/path"),
402-
local: downloadToFileName,
403+
local: downloadToFileUrl,
403404
options: nil
404405
)
405406
Task {
@@ -413,14 +414,14 @@ print("Completed")
413414
</Block>
414415
<Block name="Combine">
415416
```swift
416-
let downloadToFileName = FileManager.default.urls(
417+
let downloadToFileUrl = FileManager.default.urls(
417418
for: .documentDirectory,
418419
in: .userDomainMask
419420
)[0].appendingPathComponent("myFile.txt")
420421

421422
let downloadTask = Amplify.Storage.downloadFile(
422423
path: .fromString("public/example/path"),
423-
local: downloadToFileName,
424+
local: downloadToFileUrl,
424425
options: nil
425426
)
426427
let progressSink = downloadTask
@@ -442,6 +443,44 @@ let resultSink = downloadTask
442443
```
443444
</Block>
444445
</BlockSwitcher>
446+
447+
### Download from a specified bucket
448+
449+
You can also perform a download operation from a specific bucket by providing the `bucket` option
450+
451+
<BlockSwitcher>
452+
<Block name="From Outputs">
453+
You can use `.fromOutputs(name:)` to provide a string representing the target bucket's assigned name in the Amplify Backend.
454+
455+
```swift
456+
let downloadTask = Amplify.Storage.downloadFile(
457+
path: .fromString("public/example/path"),
458+
local: downloadToFileUrl,
459+
options: .init(
460+
bucket: .fromOutputs(name: "secondBucket")
461+
)
462+
)
463+
```
464+
</Block>
465+
466+
<Block name="From Bucket Info">
467+
You can also use `.fromBucketInfo(_:)` to provide a bucket name and region directly.
468+
469+
```swift
470+
let downloadTask = Amplify.Storage.downloadFile(
471+
path: .fromString("public/example/path"),
472+
local: downloadToFileUrl,
473+
options: .init(
474+
bucket: .fromBucketInfo(.init(
475+
bucketName: "another-bucket-name",
476+
region: "another-bucket-region")
477+
)
478+
)
479+
)
480+
```
481+
</Block>
482+
</BlockSwitcher>
483+
445484
</InlineFilter>
446485

447486
<InlineFilter filters={["flutter"]}>
@@ -1073,6 +1112,41 @@ final operation = Amplify.Storage.downloadData(
10731112

10741113
<InlineFilter filters={["swift"]}>
10751114

1115+
### Download from a specified bucket
1116+
1117+
You can also perform a download operation from a specific bucket by providing the `bucket` option
1118+
1119+
<BlockSwitcher>
1120+
<Block name="From Outputs">
1121+
You can use `.fromOutputs(name:)` to provide a string representing the target bucket's assigned name in the Amplify Backend.
1122+
1123+
```swift
1124+
let downloadTask = Amplify.Storage.downloadData(
1125+
path: .fromString("public/example/path"),
1126+
options: .init(
1127+
bucket: .fromOutputs(name: "secondBucket")
1128+
)
1129+
)
1130+
```
1131+
</Block>
1132+
1133+
<Block name="From Bucket Info">
1134+
You can also use `.fromBucketInfo(_:)` to provide a bucket name and region directly.
1135+
1136+
```swift
1137+
let downloadTask = Amplify.Storage.downloadData(
1138+
path: .fromString("public/example/path"),
1139+
options: .init(
1140+
bucket: .fromBucketInfo(.init(
1141+
bucketName: "another-bucket-name",
1142+
region: "another-bucket-region")
1143+
)
1144+
)
1145+
)
1146+
```
1147+
</Block>
1148+
</BlockSwitcher>
1149+
10761150
### Pause, resume, and cancel downloads
10771151

10781152
Calls to `downloadData` or `downloadFile` return a reference to the task that is actually performing the download.

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,43 @@ Note the trailing slash `/` in the given path.
788788
If you had used `public/photos` as path, it would also match against files like `public/photos01.jpg`.
789789
</Callout>
790790

791+
### List files from a specified bucket
792+
793+
You can perform a list operation from a specific bucket by providing the `bucket` option.
794+
795+
<BlockSwitcher>
796+
<Block name="From Outputs">
797+
You can use `.fromOutputs(name:)` to provide a string representing the target bucket's assigned name in the Amplify Backend.
798+
799+
```swift
800+
let listResult = try await Amplify.Storage.list(
801+
path: .fromString("public/example/path/myFile.txt"),
802+
local: fileUrl,
803+
options: .init(
804+
bucket: .fromOutputs(name: "secondBucket")
805+
)
806+
)
807+
```
808+
</Block>
809+
810+
<Block name="From Bucket Info">
811+
You can also use `.fromBucketInfo(_:)` to provide a bucket name and region directly.
812+
813+
```swift
814+
let listResult = try await Amplify.Storage.list(
815+
path: .fromString("public/example/path/myFile.txt"),
816+
local: fileUrl,
817+
options: .init(
818+
bucket: .fromBucketInfo(.init(
819+
bucketName: "another-bucket-name",
820+
region: "another-bucket-region")
821+
)
822+
)
823+
)
824+
```
825+
</Block>
826+
</BlockSwitcher>
827+
791828
### Exclude results from nested subpaths
792829
793830
By default, the `list` API will return all objects contained within the given path, including objects inside nested subpaths.

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

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ export function getStaticProps(context) {
3131

3232
Files can be removed from a storage bucket using the 'remove' API. If a file is protected by an identity Id, only the user who owns the file will be able to remove it.
3333

34-
You can also perform an upload operation to a specific bucket by providing the target bucket's assigned name from Amplify Backend in `bucket` option.
35-
3634
<InlineFilter filters={["react", "angular", "javascript", "vue", "nextjs", "react-native"]}>
3735

36+
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.
37+
3838
```javascript
3939
import { remove } from 'aws-amplify/storage';
4040

@@ -50,9 +50,8 @@ try {
5050
```
5151
</InlineFilter>
5252

53-
Alternatively, you can also pass in an object by specifying the bucket name and region from the console.
54-
5553
<InlineFilter filters={["react", "angular", "javascript", "vue", "nextjs", "react-native"]}>
54+
Alternatively, you can also pass in an object by specifying the bucket name and region from the console.
5655

5756
```javascript
5857
import { remove } from 'aws-amplify/storage';
@@ -276,7 +275,9 @@ RxAmplify.Storage.remove(StoragePath.fromString("public/myUploadedFileName.txt")
276275
<Block name="Async/Await">
277276

278277
```swift
279-
let removedObject = try await Amplify.Storage.remove(path: .fromString("public/example/path"))
278+
let removedObject = try await Amplify.Storage.remove(
279+
path: .fromString("public/example/path")
280+
)
280281
print("Deleted \(removedObject)")
281282
```
282283

@@ -286,7 +287,9 @@ print("Deleted \(removedObject)")
286287

287288
```swift
288289
let sink = Amplify.Publisher.create {
289-
try await Amplify.Storage.remove(path: .fromString("public/example/path"))
290+
try await Amplify.Storage.remove(
291+
path: .fromString("public/example/path")
292+
)
290293
}.sink {
291294
if case let .failure(error) = $0 {
292295
print("Failed: \(error)")
@@ -301,6 +304,41 @@ receiveValue: { removedObject in
301304

302305
</BlockSwitcher>
303306

307+
### Remove files from a specified bucket
308+
309+
You can perform a remove operation from a specific bucket by providing the `bucket` option.
310+
311+
<BlockSwitcher>
312+
<Block name="From Outputs">
313+
You can use `.fromOutputs(name:)` to provide a string representing the target bucket's assigned name in the Amplify Backend.
314+
315+
```swift
316+
let removedObject = try await Amplify.Storage.remove(
317+
path: .fromString("public/example/path/myFile.txt"),
318+
options: .init(
319+
bucket: .fromOutputs(name: "secondBucket")
320+
)
321+
)
322+
```
323+
</Block>
324+
325+
<Block name="From Bucket Info">
326+
You can also use `.fromBucketInfo(_:)` to provide a bucket name and region directly.
327+
328+
```swift
329+
let removedObject = try await Amplify.Storage.remove(
330+
path: .fromString("public/example/path/myFile.txt"),
331+
options: .init(
332+
bucket: .fromBucketInfo(.init(
333+
bucketName: "another-bucket-name",
334+
region: "another-bucket-region")
335+
)
336+
)
337+
)
338+
```
339+
</Block>
340+
</BlockSwitcher>
341+
304342
</InlineFilter>
305343

306344
<InlineFilter filters={["flutter"]}>

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,35 @@ download
334334
</BlockSwitcher>
335335
</InlineFilter>
336336

337+
<InlineFilter filters={["swift"]}>
338+
### Storage bucket client usage
339+
340+
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.
341+
342+
```swift
343+
let downloadTask = Amplify.Storage.downloadData(
344+
path: .fromString("public/example/path"),
345+
options: .init(
346+
bucket: .fromOutputs(name: "secondBucket")
347+
)
348+
)
349+
```
350+
351+
Alternatively, you can also directly specify the bucket name and region from the console. See each Amplify Storage API page for additional usage examples.
352+
353+
```swift
354+
let downloadTask = Amplify.Storage.downloadData(
355+
path: .fromString("public/example/path"),
356+
options: .init(
357+
bucket: .fromBucketInfo(.init(
358+
bucketName: "another-bucket-name",
359+
region: "another-bucket-region")
360+
)
361+
)
362+
)
363+
```
364+
</InlineFilter>
365+
337366
## Connect your app code to the storage backend
338367

339368
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: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ try dataString.write(
357357

358358
let uploadTask = Amplify.Storage.uploadFile(
359359
path: .fromString("public/example/path/myFile.txt"),
360-
local: filename
360+
local: fileUrl
361361
)
362362

363363
```
@@ -943,6 +943,70 @@ private void uploadFile() {
943943
</BlockSwitcher>
944944
</InlineFilter>
945945

946+
<InlineFilter filters={["swift"]}>
947+
948+
### Upload to a specified bucket
949+
950+
You can perform an upload operation to a specific bucket by providing the `bucket` option.
951+
952+
<BlockSwitcher>
953+
<Block name="From Outputs">
954+
You can use `.fromOutputs(name:)` to provide a string representing the target bucket's assigned name in the Amplify Backend.
955+
956+
```swift
957+
// Upload from file
958+
let uploadTask = Amplify.Storage.uploadFile(
959+
path: .fromString("public/example/path/myFile.txt"),
960+
local: fileUrl,
961+
options: .init(
962+
bucket: .fromOutputs(name: "secondBucket")
963+
)
964+
)
965+
966+
// Upload from data
967+
let uploadTask = Amplify.Storage.uploadData(
968+
path: .fromString("public/example/path/myFile.txt"),
969+
data: data,
970+
options: .init(
971+
bucket: .fromOutputs(name: "secondBucket")
972+
)
973+
)
974+
```
975+
</Block>
976+
977+
<Block name="From Bucket Info">
978+
You can also use `.fromBucketInfo(_:)` to provide a bucket name and region directly.
979+
980+
```swift
981+
// Upload from File
982+
let uploadTask = Amplify.Storage.uploadFile(
983+
path: .fromString("public/example/path/myFile.txt"),
984+
local: fileUrl,
985+
options: .init(
986+
bucket: .fromBucketInfo(.init(
987+
bucketName: "another-bucket-name",
988+
region: "another-bucket-region")
989+
)
990+
)
991+
)
992+
993+
// Upload from Data
994+
let uploadTask = Amplify.Storage.uploadData(
995+
path: .fromString("public/example/path/myFile.txt"),
996+
data: data,
997+
options: .init(
998+
bucket: .fromBucketInfo(.init(
999+
bucketName: "another-bucket-name",
1000+
region: "another-bucket-region")
1001+
)
1002+
)
1003+
)
1004+
```
1005+
</Block>
1006+
</BlockSwitcher>
1007+
1008+
</InlineFilter>
1009+
9461010
<InlineFilter filters={["angular","javascript","nextjs","react","vue","react-native"]}>
9471011

9481012
### Monitor upload progress

0 commit comments

Comments
 (0)