Always make sure to call Close() at the end for both the created filesystem instance and
the retrieved file readers to prevent leaking resources.
PocketBase comes with a thin abstraction between the local filesystem and S3.
To configure which one will be used you can adjust the storage settings from Dashboard > Settings > Files storage section.
The filesystem abstraction can be accessed programmatically via the
app.NewFilesystem()
method.
Below are listed some of the most common operations but you can find more details in the
filesystem
subpackage.
Always make sure to call Close() at the end for both the created filesystem instance and
the retrieved file readers to prevent leaking resources.
To retrieve the file content of a single stored file you can use
GetReader(key)
.
Note that file keys often contain a prefix (aka. the "path" to the file). For record
files the full key is
collectionId/recordId/filename.
To retrieve multiple files matching a specific prefix you can use
List(prefix)
.
The below code shows a minimal example how to retrieve a single record file and copy its content into a
bytes.Buffer.
There are several methods to save (aka. write/upload) files depending on the available file content source:
Most users rarely will have to use the above methods directly because for collection records the file
persistence is handled transparently when saving the record model (it will also perform size and MIME type
validation based on the collection file field options). For example:
Files can be deleted from the storage filesystem using
Delete(key)
.
Similar to the previous section, most users rarely will have to use the Delete file method directly
because for collection records the file deletion is handled transparently when removing the existing filename
from the record model (this also ensures that the db entry referencing the file is also removed). For example: