Skip to content

Commit 49a9d66

Browse files
committed
update android storage with multi-bucket support
1 parent 3a21b61 commit 49a9d66

File tree

5 files changed

+846
-1
lines changed

5 files changed

+846
-1
lines changed

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

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ RxAmplify.Storage.getUrl(StoragePath.fromString("public/example"), options).subs
241241

242242
Option | Type | Description |
243243
| -- | -- | ----------- |
244+
| bucket | StorageBucket | A string representing the target bucket's assigned name in Amplify Backend or an object specifying the bucket name and region from the console.<br/><br/>Read more at [Configure additional storage buckets](/[platform]/build-a-backend/storage/set-up-storage/#configure-additional-storage-buckets) |
244245
| expires | Integer | Number of seconds before the URL expires |
245246
| useAccelerateEndpoint | Boolean | Flag to configure use of acceleration mode |
246247

@@ -366,7 +367,7 @@ try {
366367
RxProgressAwareSingleOperation<StorageDownloadFileResult> download =
367368
RxAmplify.Storage.downloadFile(
368369
StoragePath.fromString("public/example"),
369-
new File(getApplicationContext().getFilesDir() + "/download.txt"
370+
new File(getApplicationContext().getFilesDir() + "/download.txt")
370371
);
371372

372373
download
@@ -650,6 +651,152 @@ try {
650651

651652
<InlineFilter filters={["android"]}>
652653

654+
### Download from a specified bucket
655+
656+
You can also perform an download operation to a specific bucket by providing the `bucket` option. You can pass in a string representing the target bucket's assigned name in Amplify Backend.
657+
658+
<BlockSwitcher>
659+
<Block name="Java">
660+
661+
```java
662+
StorageBucket secondBucket = StorageBucket.fromOutputs("secondBucket");
663+
StorageDownloadFileOptions options = StorageDownloadFileOptions.builder().bucket(secondBucket).build();
664+
Amplify.Storage.downloadFile(
665+
StoragePath.fromString("public/example"),
666+
new File(getApplicationContext().getFilesDir() + "/download.txt"),
667+
options,
668+
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
669+
error -> Log.e("MyAmplifyApp", "Download Failure", error)
670+
);
671+
```
672+
673+
</Block>
674+
<Block name="Kotlin - Callbacks">
675+
676+
```kotlin
677+
val secondBucket = StorageBucket.fromOutputs("secondBucket")
678+
val options = StorageDownloadFileOptions.builder().bucket(secondBucket).build()
679+
val file = File("${applicationContext.filesDir}/download.txt")
680+
Amplify.Storage.downloadFile(StoragePath.fromString("public/example"), file, option,
681+
{ Log.i("MyAmplifyApp", "Successfully downloaded: ${it.file.name}") },
682+
{ Log.e("MyAmplifyApp", "Download Failure", it) }
683+
)
684+
```
685+
686+
</Block>
687+
<Block name="Kotlin - Coroutines">
688+
689+
```kotlin
690+
val secondBucket = StorageBucket.fromOutputs("secondBucket")
691+
val options = StorageDownloadFileOptions.builder().bucket(secondBucket).build()
692+
val file = File("${applicationContext.filesDir}/download.txt")
693+
val download = Amplify.Storage.downloadFile(StoragePath.fromString("public/example"), file, options)
694+
try {
695+
val fileName = download.result().file.name
696+
Log.i("MyAmplifyApp", "Successfully downloaded: $fileName")
697+
} catch (error: StorageException) {
698+
Log.e("MyAmplifyApp", "Download Failure", error)
699+
}
700+
```
701+
702+
</Block>
703+
<Block name="RxJava">
704+
705+
```java
706+
StorageBucket secondBucket = StorageBucket.fromOutputs("secondBucket");
707+
StorageDownloadFileOptions options = StorageDownloadFileOptions.builder().bucket(secondBucket).build();
708+
RxProgressAwareSingleOperation<StorageDownloadFileResult> download =
709+
RxAmplify.Storage.downloadFile(
710+
StoragePath.fromString("public/example"),
711+
new File(getApplicationContext().getFilesDir() + "/download.txt"),
712+
options
713+
);
714+
715+
download
716+
.observeResult()
717+
.subscribe(
718+
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
719+
error -> Log.e("MyAmplifyApp", "Download Failure", error)
720+
);
721+
```
722+
723+
</Block>
724+
</BlockSwitcher>
725+
726+
Alternatively, you can also pass in an object by specifying the bucket name and region from the console.
727+
728+
<BlockSwitcher>
729+
<Block name="Java">
730+
731+
```java
732+
BucketInfo bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
733+
StorageBucket secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
734+
StorageDownloadFileOptions options = StorageDownloadFileOptions.builder().bucket(secondBucket).build();
735+
Amplify.Storage.downloadFile(
736+
StoragePath.fromString("public/example"),
737+
new File(getApplicationContext().getFilesDir() + "/download.txt"),
738+
options,
739+
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
740+
error -> Log.e("MyAmplifyApp", "Download Failure", error)
741+
);
742+
```
743+
744+
</Block>
745+
<Block name="Kotlin - Callbacks">
746+
747+
```kotlin
748+
val bucketInfo = BucketInfo("second-bucket-name-from-console", "us-east-2")
749+
val secondBucket = StorageBucket.fromBucketInfo(bucketInfo)
750+
val options = StorageDownloadFileOptions.builder().bucket(secondBucket).build()
751+
val file = File("${applicationContext.filesDir}/download.txt")
752+
Amplify.Storage.downloadFile(StoragePath.fromString("public/example"), file, options,
753+
{ Log.i("MyAmplifyApp", "Successfully downloaded: ${it.file.name}") },
754+
{ Log.e("MyAmplifyApp", "Download Failure", it) }
755+
)
756+
```
757+
758+
</Block>
759+
<Block name="Kotlin - Coroutines">
760+
761+
```kotlin
762+
val bucketInfo = BucketInfo("second-bucket-name-from-console", "us-east-2")
763+
val secondBucket = StorageBucket.fromBucketInfo(bucketInfo)
764+
val options = StorageDownloadFileOptions.builder().bucket(secondBucket).build()
765+
val file = File("${applicationContext.filesDir}/download.txt")
766+
val download = Amplify.Storage.downloadFile(StoragePath.fromString("public/example"), file, options)
767+
try {
768+
val fileName = download.result().file.name
769+
Log.i("MyAmplifyApp", "Successfully downloaded: $fileName")
770+
} catch (error: StorageException) {
771+
Log.e("MyAmplifyApp", "Download Failure", error)
772+
}
773+
```
774+
775+
</Block>
776+
<Block name="RxJava">
777+
778+
```java
779+
BucketInfo bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
780+
StorageBucket secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
781+
StorageDownloadFileOptions options = StorageDownloadFileOptions.builder().bucket(secondBucket).build();
782+
RxProgressAwareSingleOperation<StorageDownloadFileResult> download =
783+
RxAmplify.Storage.downloadFile(
784+
StoragePath.fromString("public/example"),
785+
new File(getApplicationContext().getFilesDir() + "/download.txt"),
786+
options,
787+
);
788+
789+
download
790+
.observeResult()
791+
.subscribe(
792+
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
793+
error -> Log.e("MyAmplifyApp", "Download Failure", error)
794+
);
795+
```
796+
797+
</Block>
798+
</BlockSwitcher>
799+
653800
### Monitor download progress
654801

655802
<BlockSwitcher>

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

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,196 @@ Note the trailing slash `/` in the given path.
334334
If you had used `public/photos` as path, it would also match against files like `public/photos01.jpg`.
335335
</Callout>
336336
337+
### List files from a specified bucket
338+
339+
You can also perform a list operation to a specific bucket by providing the `bucket` option. You can pass in a string representing the target bucket's assigned name in Amplify Backend.
340+
341+
<BlockSwitcher>
342+
<Block name="Java">
343+
344+
```java
345+
StorageBucket secondBucket = StorageBucket.fromOutputs("secondBucket");
346+
StoragePagedListOptions options = StoragePagedListOptions.builder()
347+
.setPageSize(1000)
348+
.bucket(secondBucket)
349+
.build();
350+
351+
Amplify.Storage.list(
352+
StoragePath.fromString("public/photos/"),
353+
options,
354+
result -> {
355+
for (StorageItem item : result.getItems()) {
356+
Log.i("MyAmplifyApp", "Item: " + item.getPath());
357+
}
358+
Log.i("MyAmplifyApp", "Next Token: " + result.getNextToken());
359+
},
360+
error -> Log.e("MyAmplifyApp", "List failure", error);
361+
);
362+
```
363+
364+
</Block>
365+
<Block name="Kotlin - Callbacks">
366+
367+
```kotlin
368+
val secondBucket = StorageBucket.fromOutputs("secondBucket")
369+
val options = StoragePagedListOptions.builder()
370+
.setPageSize(1000)
371+
.bucket(secondBucket)
372+
.build()
373+
374+
Amplify.Storage.list(StoragePath.fromString("public/photos/"), options,
375+
{ result ->
376+
result.items.forEach { item ->
377+
Log.i("MyAmplifyApp", "Item: ${item.path}")
378+
}
379+
Log.i("MyAmplifyApp", "Next Token: ${result.nextToken}")
380+
},
381+
{ Log.e("MyAmplifyApp", "List failure", it) }
382+
)
383+
```
384+
385+
</Block>
386+
<Block name="Kotlin - Coroutines">
387+
388+
```kotlin
389+
val secondBucket = StorageBucket.fromOutputs("secondBucket")
390+
val options = StoragePagedListOptions.builder()
391+
.setPageSize(1000)
392+
.bucket(secondBucket)
393+
.build()
394+
395+
try {
396+
val result = Amplify.Storage.list(StoragePath.fromString("public/photos/"), options)
397+
result.items.forEach {
398+
Log.i("MyAmplifyApp", "Item: $it")
399+
}
400+
Log.i("MyAmplifyApp", "next token: ${result.nextToken}")
401+
} catch (error: StorageException) {
402+
Log.e("MyAmplifyApp", "List failure", error)
403+
}
404+
```
405+
406+
</Block>
407+
<Block name="RxJava">
408+
409+
```java
410+
StorageBucket secondBucket = StorageBucket.fromOutputs("secondBucket");
411+
StoragePagedListOptions options = StoragePagedListOptions.builder()
412+
.setPageSize(1000)
413+
.bucket(secondBucket)
414+
.build();
415+
416+
RxAmplify.Storage.list(StoragePath.fromString("public/photos/"), options)
417+
.subscribe(
418+
result -> {
419+
for (StorageItem item : result.getItems()) {
420+
Log.i("MyAmplifyApp", "Item: " + item.getPath());
421+
}
422+
Log.i("MyAmplifyApp", "Next Token: " + result.getNextToken());
423+
},
424+
error -> Log.e("MyAmplifyApp", "List failure", error);
425+
);
426+
```
427+
428+
</Block>
429+
</BlockSwitcher>
430+
431+
Alternatively, you can also pass in an object by specifying the bucket name and region from the console.
432+
433+
<BlockSwitcher>
434+
<Block name="Java">
435+
436+
```java
437+
BucketInfo bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
438+
StorageBucket secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
439+
StoragePagedListOptions options = StoragePagedListOptions.builder()
440+
.setPageSize(1000)
441+
.bucket(secondBucket)
442+
.build();
443+
444+
Amplify.Storage.list(
445+
StoragePath.fromString("public/photos/"),
446+
options,
447+
result -> {
448+
for (StorageItem item : result.getItems()) {
449+
Log.i("MyAmplifyApp", "Item: " + item.getPath());
450+
}
451+
Log.i("MyAmplifyApp", "Next Token: " + result.getNextToken());
452+
},
453+
error -> Log.e("MyAmplifyApp", "List failure", error);
454+
);
455+
```
456+
457+
</Block>
458+
<Block name="Kotlin - Callbacks">
459+
460+
```kotlin
461+
val bucketInfo = BucketInfo("second-bucket-name-from-console", "us-east-2")
462+
val secondBucket = StorageBucket.fromBucketInfo(bucketInfo)
463+
val options = StoragePagedListOptions.builder()
464+
.setPageSize(1000)
465+
.bucket(secondBucket)
466+
.build()
467+
468+
Amplify.Storage.list(StoragePath.fromString("public/photos/"), options,
469+
{ result ->
470+
result.items.forEach { item ->
471+
Log.i("MyAmplifyApp", "Item: ${item.path}")
472+
}
473+
Log.i("MyAmplifyApp", "Next Token: ${result.nextToken}")
474+
},
475+
{ Log.e("MyAmplifyApp", "List failure", it) }
476+
)
477+
```
478+
479+
</Block>
480+
<Block name="Kotlin - Coroutines">
481+
482+
```kotlin
483+
val bucketInfo = BucketInfo("second-bucket-name-from-console", "us-east-2")
484+
val secondBucket = StorageBucket.fromBucketInfo(bucketInfo)
485+
val options = StoragePagedListOptions.builder()
486+
.setPageSize(1000)
487+
.bucket(secondBucket)
488+
.build()
489+
490+
try {
491+
val result = Amplify.Storage.list(StoragePath.fromString("public/photos/"), options)
492+
result.items.forEach {
493+
Log.i("MyAmplifyApp", "Item: $it")
494+
}
495+
Log.i("MyAmplifyApp", "next token: ${result.nextToken}")
496+
} catch (error: StorageException) {
497+
Log.e("MyAmplifyApp", "List failure", error)
498+
}
499+
```
500+
501+
</Block>
502+
<Block name="RxJava">
503+
504+
```java
505+
BucketInfo bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
506+
StorageBucket secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
507+
StoragePagedListOptions options = StoragePagedListOptions.builder()
508+
.setPageSize(1000)
509+
.bucket(secondBucket)
510+
.build();
511+
512+
RxAmplify.Storage.list(StoragePath.fromString("public/photos/"), options)
513+
.subscribe(
514+
result -> {
515+
for (StorageItem item : result.getItems()) {
516+
Log.i("MyAmplifyApp", "Item: " + item.getPath());
517+
}
518+
Log.i("MyAmplifyApp", "Next Token: " + result.getNextToken());
519+
},
520+
error -> Log.e("MyAmplifyApp", "List failure", error);
521+
);
522+
```
523+
524+
</Block>
525+
</BlockSwitcher>
526+
337527
### Exclude results from nested subpaths
338528

339529
By default, the `list` API will return all objects contained within the given path, including objects inside nested subpaths.

0 commit comments

Comments
 (0)