Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 237 additions & 10 deletions src/pages/[platform]/build-a-backend/storage/download-files/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,17 @@ RxAmplify.Storage.getUrl(StoragePath.fromString("public/example"), options).subs

Option | Type | Description |
| -- | -- | ----------- |
| 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) |
| expires | Integer | Number of seconds before the URL expires |
| useAccelerateEndpoint | Boolean | Flag to configure use of acceleration mode |

</InlineFilter>

<InlineFilter filters={["swift"]}>
```swift
let url = try await Amplify.Storage.getURL(path: .fromString("public/example/path"))
let url = try await Amplify.Storage.getURL(
path: .fromString("public/example/path")
)
print("Completed: \(url)")
```

Expand All @@ -275,6 +278,7 @@ let url = try await Amplify.Storage.getURL(
Option | Type | Description |
| -- | -- | ----------- |
| expires | Integer | Number of seconds before the URL expires |
| bucket | StorageBucket | The bucket in which the file is stored |

</InlineFilter>

Expand Down Expand Up @@ -366,7 +370,7 @@ try {
RxProgressAwareSingleOperation<StorageDownloadFileResult> download =
RxAmplify.Storage.downloadFile(
StoragePath.fromString("public/example"),
new File(getApplicationContext().getFilesDir() + "/download.txt"
new File(getApplicationContext().getFilesDir() + "/download.txt")
);

download
Expand All @@ -391,14 +395,14 @@ You can download to a file [URL](https://developer.apple.com/documentation/found

<Block name="Async/Await">
```swift
let downloadToFileName = FileManager.default.urls(
let downloadToFileUrl = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
)[0].appendingPathComponent("myFile.txt")

let downloadTask = Amplify.Storage.downloadFile(
path: .fromString("public/example/path"),
local: downloadToFileName,
local: downloadToFileUrl,
options: nil
)
Task {
Expand All @@ -412,14 +416,14 @@ print("Completed")
</Block>
<Block name="Combine">
```swift
let downloadToFileName = FileManager.default.urls(
let downloadToFileUrl = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
)[0].appendingPathComponent("myFile.txt")

let downloadTask = Amplify.Storage.downloadFile(
path: .fromString("public/example/path"),
local: downloadToFileName,
local: downloadToFileUrl,
options: nil
)
let progressSink = downloadTask
Expand All @@ -441,6 +445,44 @@ let resultSink = downloadTask
```
</Block>
</BlockSwitcher>

### Download from a specified bucket

You can also perform a download operation from a specific bucket by providing the `bucket` option

<BlockSwitcher>
<Block name="From Outputs">
You can use `.fromOutputs(name:)` to provide a string representing the target bucket's assigned name in the Amplify Backend.

```swift
let downloadTask = Amplify.Storage.downloadFile(
path: .fromString("public/example/path"),
local: downloadToFileUrl,
options: .init(
bucket: .fromOutputs(name: "secondBucket")
)
)
```
</Block>

<Block name="From Bucket Info">
You can also use `.fromBucketInfo(_:)` to provide a bucket name and region directly.

```swift
let downloadTask = Amplify.Storage.downloadFile(
path: .fromString("public/example/path"),
local: downloadToFileUrl,
options: .init(
bucket: .fromBucketInfo(.init(
bucketName: "another-bucket-name",
region: "another-bucket-region")
)
)
)
```
</Block>
</BlockSwitcher>

</InlineFilter>

<InlineFilter filters={["flutter"]}>
Expand Down Expand Up @@ -523,7 +565,7 @@ try {

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

## API to download data in memory
## Download to data in memory

You can download to in-memory buffer [Data](https://developer.apple.com/documentation/foundation/data) object with `Amplify.Storage.downloadData`:

Expand All @@ -532,7 +574,9 @@ You can download to in-memory buffer [Data](https://developer.apple.com/document
<Block name="Async/Await">

```swift
let downloadTask = Amplify.Storage.downloadData(path: .fromString("public/example/path"))
let downloadTask = Amplify.Storage.downloadData(
path: .fromString("public/example/path")
)
Task {
for await progress in await downloadTask.progress {
print("Progress: \(progress)")
Expand All @@ -547,7 +591,9 @@ print("Completed: \(data)")
<Block name="Combine">

```swift
let downloadTask = Amplify.Storage.downloadData(path: .fromString("public/example/path"))
let downloadTask = Amplify.Storage.downloadData(
path: .fromString("public/example/path")
)
let progressSink = downloadTask
.inProcessPublisher
.sink { progress in
Expand Down Expand Up @@ -650,6 +696,152 @@ try {

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

### Download from a specified bucket

You can also perform a 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.

<BlockSwitcher>
<Block name="Java">

```java
StorageBucket secondBucket = StorageBucket.fromOutputs("secondBucket");
StorageDownloadFileOptions options = StorageDownloadFileOptions.builder().bucket(secondBucket).build();
Amplify.Storage.downloadFile(
StoragePath.fromString("public/example"),
new File(getApplicationContext().getFilesDir() + "/download.txt"),
options,
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
error -> Log.e("MyAmplifyApp", "Download Failure", error)
);
```

</Block>
<Block name="Kotlin - Callbacks">

```kotlin
val secondBucket = StorageBucket.fromOutputs("secondBucket")
val options = StorageDownloadFileOptions.builder().bucket(secondBucket).build()
val file = File("${applicationContext.filesDir}/download.txt")
Amplify.Storage.downloadFile(StoragePath.fromString("public/example"), file, option,
{ Log.i("MyAmplifyApp", "Successfully downloaded: ${it.file.name}") },
{ Log.e("MyAmplifyApp", "Download Failure", it) }
)
```

</Block>
<Block name="Kotlin - Coroutines">

```kotlin
val secondBucket = StorageBucket.fromOutputs("secondBucket")
val options = StorageDownloadFileOptions.builder().bucket(secondBucket).build()
val file = File("${applicationContext.filesDir}/download.txt")
val download = Amplify.Storage.downloadFile(StoragePath.fromString("public/example"), file, options)
try {
val fileName = download.result().file.name
Log.i("MyAmplifyApp", "Successfully downloaded: $fileName")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Download Failure", error)
}
```

</Block>
<Block name="RxJava">

```java
StorageBucket secondBucket = StorageBucket.fromOutputs("secondBucket");
StorageDownloadFileOptions options = StorageDownloadFileOptions.builder().bucket(secondBucket).build();
RxProgressAwareSingleOperation<StorageDownloadFileResult> download =
RxAmplify.Storage.downloadFile(
StoragePath.fromString("public/example"),
new File(getApplicationContext().getFilesDir() + "/download.txt"),
options
);

download
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
error -> Log.e("MyAmplifyApp", "Download Failure", error)
);
```

</Block>
</BlockSwitcher>

Alternatively, you can also pass in an object by specifying the bucket name and region from the console.

<BlockSwitcher>
<Block name="Java">

```java
BucketInfo bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
StorageBucket secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
StorageDownloadFileOptions options = StorageDownloadFileOptions.builder().bucket(secondBucket).build();
Amplify.Storage.downloadFile(
StoragePath.fromString("public/example"),
new File(getApplicationContext().getFilesDir() + "/download.txt"),
options,
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
error -> Log.e("MyAmplifyApp", "Download Failure", error)
);
```

</Block>
<Block name="Kotlin - Callbacks">

```kotlin
val bucketInfo = BucketInfo("second-bucket-name-from-console", "us-east-2")
val secondBucket = StorageBucket.fromBucketInfo(bucketInfo)
val options = StorageDownloadFileOptions.builder().bucket(secondBucket).build()
val file = File("${applicationContext.filesDir}/download.txt")
Amplify.Storage.downloadFile(StoragePath.fromString("public/example"), file, options,
{ Log.i("MyAmplifyApp", "Successfully downloaded: ${it.file.name}") },
{ Log.e("MyAmplifyApp", "Download Failure", it) }
)
```

</Block>
<Block name="Kotlin - Coroutines">

```kotlin
val bucketInfo = BucketInfo("second-bucket-name-from-console", "us-east-2")
val secondBucket = StorageBucket.fromBucketInfo(bucketInfo)
val options = StorageDownloadFileOptions.builder().bucket(secondBucket).build()
val file = File("${applicationContext.filesDir}/download.txt")
val download = Amplify.Storage.downloadFile(StoragePath.fromString("public/example"), file, options)
try {
val fileName = download.result().file.name
Log.i("MyAmplifyApp", "Successfully downloaded: $fileName")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Download Failure", error)
}
```

</Block>
<Block name="RxJava">

```java
BucketInfo bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
StorageBucket secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
StorageDownloadFileOptions options = StorageDownloadFileOptions.builder().bucket(secondBucket).build();
RxProgressAwareSingleOperation<StorageDownloadFileResult> download =
RxAmplify.Storage.downloadFile(
StoragePath.fromString("public/example"),
new File(getApplicationContext().getFilesDir() + "/download.txt"),
options,
);

download
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
error -> Log.e("MyAmplifyApp", "Download Failure", error)
);
```

</Block>
</BlockSwitcher>

### Monitor download progress

<BlockSwitcher>
Expand Down Expand Up @@ -926,7 +1118,42 @@ final operation = Amplify.Storage.downloadData(

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

### Pause, resume, and cancel downloads
### Download from a specified bucket

You can also perform a download operation from a specific bucket by providing the `bucket` option

<BlockSwitcher>
<Block name="From Outputs">
You can use `.fromOutputs(name:)` to provide a string representing the target bucket's assigned name in the Amplify Backend.

```swift
let downloadTask = Amplify.Storage.downloadData(
path: .fromString("public/example/path"),
options: .init(
bucket: .fromOutputs(name: "secondBucket")
)
)
```
</Block>

<Block name="From Bucket Info">
You can also use `.fromBucketInfo(_:)` to provide a bucket name and region directly.

```swift
let downloadTask = Amplify.Storage.downloadData(
path: .fromString("public/example/path"),
options: .init(
bucket: .fromBucketInfo(.init(
bucketName: "another-bucket-name",
region: "another-bucket-region")
)
)
)
```
</Block>
</BlockSwitcher>

## Pause, resume, and cancel downloads

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

Expand Down
Loading