-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
storage(swift): update swift storage with storage path #7142
Changes from 4 commits
c0f4838
dc6cee8
a88152a
12ccdc1
b0e2526
3856ec0
d1598db
8abfd81
2d58249
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
## Customization | ||
## Customization (Deprecated) | ||
|
||
### Customize Object Key Path | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,58 @@ | ||
import native_common from '/src/fragments/lib/storage/native_common/gen2_callout.mdx'; | ||
|
||
<Fragments fragments={{ all: native_common }} /> | ||
|
||
There are three ways of getting data that was previously uploaded: | ||
|
||
## Download data | ||
|
||
You can download to in-memory buffer [Data](https://developer.apple.com/documentation/foundation/data) object with `Amplify.Storage.downloadData`: | ||
|
||
#### With StoragePath | ||
<BlockSwitcher> | ||
|
||
<Block name="Async/Await"> | ||
|
||
```swift | ||
let downloadTask = Amplify.Storage.downloadData(path: .fromString("example/path")) | ||
Task { | ||
for await progress in await downloadTask.progress { | ||
print("Progress: \(progress)") | ||
} | ||
} | ||
let data = try await downloadTask.value | ||
print("Completed: \(data)") | ||
``` | ||
|
||
</Block> | ||
|
||
<Block name="Combine"> | ||
|
||
```swift | ||
let downloadTask = Amplify.Storage.downloadData(path: .fromString("example/path")) | ||
let progressSink = downloadTask | ||
.inProcessPublisher | ||
.sink { progress in | ||
print("Progress: \(progress)") | ||
} | ||
|
||
let resultSink = downloadTask | ||
.resultPublisher | ||
.sink { | ||
if case let .failure(storageError) = $0 { | ||
print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)") | ||
} | ||
} | ||
receiveValue: { data in | ||
print("Completed: \(data)") | ||
} | ||
``` | ||
|
||
</Block> | ||
|
||
</BlockSwitcher> | ||
|
||
#### With Key (Deprecated) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add context that this has been deprecated from the latest version and we recommend using StoragePath above instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be a callout at the top of the download page now. |
||
<BlockSwitcher> | ||
|
||
<Block name="Async/Await"> | ||
|
@@ -51,6 +100,69 @@ let resultSink = downloadTask | |
|
||
You can download to a file [URL](https://developer.apple.com/documentation/foundation/url) with `Amplify.Storage.downloadFile`: | ||
|
||
#### With StoragePath | ||
<BlockSwitcher> | ||
|
||
<Block name="Async/Await"> | ||
|
||
```swift | ||
let downloadToFileName = FileManager.default.urls( | ||
for: .documentDirectory, | ||
in: .userDomainMask | ||
)[0].appendingPathComponent("myFile.txt") | ||
|
||
let downloadTask = Amplify.Storage.downloadFile( | ||
path: .fromString("example/path"), | ||
local: downloadToFileName, | ||
options: nil | ||
) | ||
Task { | ||
for await progress in await downloadTask.progress { | ||
print("Progress: \(progress)") | ||
} | ||
} | ||
try await downloadTask.value | ||
print("Completed") | ||
``` | ||
|
||
</Block> | ||
|
||
<Block name="Combine"> | ||
|
||
```swift | ||
let downloadToFileName = FileManager.default.urls( | ||
for: .documentDirectory, | ||
in: .userDomainMask | ||
)[0].appendingPathComponent("myFile.txt") | ||
|
||
let downloadTask = Amplify.Storage.downloadFile( | ||
path: .fromString("example/path"), | ||
local: downloadToFileName, | ||
options: nil | ||
) | ||
let progressSink = downloadTask | ||
.inProcessPublisher | ||
.sink { progress in | ||
print("Progress: \(progress)") | ||
} | ||
|
||
let resultSink = downloadTask | ||
.resultPublisher | ||
.sink { | ||
if case let .failure(storageError) = $0 { | ||
print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)") | ||
} | ||
} | ||
receiveValue: { | ||
print("Completed") | ||
} | ||
``` | ||
|
||
</Block> | ||
|
||
</BlockSwitcher> | ||
|
||
#### With Key (Deprecated) | ||
<BlockSwitcher> | ||
|
||
<Block name="Async/Await"> | ||
|
@@ -115,17 +227,42 @@ let resultSink = downloadTask | |
## Generate a download URL | ||
|
||
You can also retrieve a URL for the object in storage: | ||
<BlockSwitcher> | ||
<Block name="With StoragePath"> | ||
```swift | ||
let url = try await Amplify.Storage.getURL(path: .fromString("example/path")) | ||
print("Completed: \(url)") | ||
``` | ||
</Block> | ||
|
||
<Block name="With Key (Deprecated)"> | ||
```swift | ||
let url = try await Amplify.Storage.getURL(key: "ExampleKey") | ||
print("Completed: \(url)") | ||
``` | ||
</Block> | ||
</BlockSwitcher> | ||
|
||
When creating a downloadable URL, you can choose to check if the file exists by setting `validateObjectExistence` to | ||
`true` in `AWSStorageGetURLOptions`. If the file is inaccessible or does not exist, a `StorageError` is thrown. | ||
This allows you to check if an object exists during generating the presigned URL, which you can then use to download | ||
that object. | ||
|
||
<BlockSwitcher> | ||
<Block name="With StoragePath"> | ||
```swift | ||
let url = try await Amplify.Storage.getURL( | ||
path: .fromString("example/path"), | ||
options: .init( | ||
pluginOptions: AWSStorageGetURLOptions( | ||
validateObjectExistence: true | ||
) | ||
) | ||
) | ||
``` | ||
</Block> | ||
|
||
<Block name="With Key (Deprecated)"> | ||
```swift | ||
let url = try await Amplify.Storage.getURL( | ||
key: "ExampleKey", | ||
|
@@ -136,7 +273,9 @@ let url = try await Amplify.Storage.getURL( | |
) | ||
) | ||
``` | ||
</Block> | ||
|
||
</BlockSwitcher> | ||
## Cancel, Pause, Resume | ||
|
||
Calls to `downloadData` or `downloadFile` return a reference to the task that is actually performing the download. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a short blurb about how this is new in the latest version and we recommend customers to use this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a callout for each of the new API. I didn't specify a specific Amplify version, as this callout can be common and used across all native platforms.