Skip to content

Commit 5bc028f

Browse files
docs: Document how to reference a stored file from a model (serverpod#866)
1 parent bc49e98 commit 5bc028f

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

‎docs/06-concepts/02-endpoints-and-apis/07-file-uploads.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,24 @@ await session.storage.storeFile(
190190

191191
To delete a stored file, use `deleteFile` with the same `storageId` and `path`.
192192

193+
## Save a file reference in a model
194+
195+
To link a stored file to your data, such as a document a user uploaded, save where the file lives rather than the file itself. Add `storageId` and `path` fields to the [model](../data-and-the-database/models):
196+
197+
```yaml title="document.spy.yaml"
198+
class: Document
199+
table: document
200+
fields:
201+
title: String
202+
storageId: String
203+
path: String
204+
```
205+
206+
The storage methods above identify a file by its `storageId` and `path`. These two fields are enough to read, delete, or create a URL for the file later.
207+
208+
- **Private storage:** Don't save a URL. Signed URLs expire, so call `temporaryDownloadUrl` each time an authorized user asks for the file.
209+
- **Public storage:** You can also save the URL from `publicDownloadUrl` in a `Uri` field, so the app gets the link along with the rest of the data. Keep `storageId` and `path` next to it, because a URL alone isn't enough to delete or replace the file. A saved URL also keeps the domain it was created with, so create new URLs from those two fields if you move the files or change their domain.
210+
193211
## Configure a storage provider
194212

195213
Each storage is identified by a `storageId`. Serverpod comes with two default storages, `public` and `private`. Replace these with a cloud-backed implementation, or add additional storages with custom IDs. Calling `pod.addCloudStorage` with `public` or `private` replaces that default. Call it before `pod.start()`. For local disk or a NAS, see [Custom cloud storage](./custom-cloud-storage).

‎versioned_docs/version-4.0.0/06-concepts/02-endpoints-and-apis/07-file-uploads.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,24 @@ await session.storage.storeFile(
190190

191191
To delete a stored file, use `deleteFile` with the same `storageId` and `path`.
192192

193+
## Save a file reference in a model
194+
195+
To link a stored file to your data, such as a document a user uploaded, save where the file lives rather than the file itself. Add `storageId` and `path` fields to the [model](../data-and-the-database/models):
196+
197+
```yaml title="document.spy.yaml"
198+
class: Document
199+
table: document
200+
fields:
201+
title: String
202+
storageId: String
203+
path: String
204+
```
205+
206+
The storage methods above identify a file by its `storageId` and `path`. These two fields are enough to read, delete, or create a URL for the file later.
207+
208+
- **Private storage:** Don't save a URL. Signed URLs expire, so call `temporaryDownloadUrl` each time an authorized user asks for the file.
209+
- **Public storage:** You can also save the URL from `publicDownloadUrl` in a `Uri` field, so the app gets the link along with the rest of the data. Keep `storageId` and `path` next to it, because a URL alone isn't enough to delete or replace the file. A saved URL also keeps the domain it was created with, so create new URLs from those two fields if you move the files or change their domain.
210+
193211
## Configure a storage provider
194212

195213
Each storage is identified by a `storageId`. Serverpod comes with two default storages, `public` and `private`. Replace these with a cloud-backed implementation, or add additional storages with custom IDs. Calling `pod.addCloudStorage` with `public` or `private` replaces that default. Call it before `pod.start()`. For local disk or a NAS, see [Custom cloud storage](./custom-cloud-storage).

0 commit comments

Comments
 (0)