Files
Files can be of any type, such as images, documents, videos, etc. File metadata is stored in your database in thefiles
table in the storage
schema. This means that file metadata is available in your GraphQL API, which makes it easy to:
- Read file metadata via GraphQL.
- Manage file permissions (in Hasura).
- Create GraphQL relationships between files and your database tables.
Don’t modify the database schema, nor GraphQL root fields in any of the tables in the
storage
schema.You’re allowed to add and modify the following:
- GraphQL Relationships
- Permissions
Upload File
When a file is uploaded, the file metadata is inserted into thestorage.files
table and a file id
is returned. The file’s id
is how you reference and access the file. It’s recommended to create your own table to store the uploaded file id
, to access the file in the future.
upload()
.Download File
There are two ways to download a file:- Public URL
- Pre-signed URL
Public URL
Public URLs are available for both unauthenticated and authenticated users. Permissions are checked for every file request. To get a public URL for a file, you would normally use thepublic
role to set select permissions.
getPublicUrl()
.Pre-signed URL
Pre-signed URLs work a bit differently from public URLs. The permission check only happens when the user requests a pre-signed URL. Once a pre-signed URL is generated, anyone with the URL can download the file. Pre-signed URLs expire after an expiration time. For files in thedefault
bucket, the expiration time is 30 seconds. You can change the expiration time for pre-signed URLs by changing the download_expiration
(in seconds) field for the bucket where the file is located.
getPresignedUrl()
.Delete File
Delete a file and the file metadata in the database.delete()
.Buckets
Buckets are used to organize files and group permissions for files. Buckets are stored in thestorage.buckets
table in your database, and accessible via buckets
in your GraphQL API.
For each Bucket, you can specify file permissions for the following properties:
- MIME type.
- Minimum size in bytes.
- Maximum size in bytes.
- Cache control.
- Allow pre-signed URLs.
- Download expiration (for pre-signed URLs).
default
) that is used if no Bucket is specified during file upload. It’s not possible to delete the default
Bucket.
Permissions
Permissions to upload, download, and delete files are managed through Hasura’s permission system on thestorage.files
table.
Upload
To upload a file, a user must have theinsert
permission to the storage.files
table. The id
column must be granted.
The following columns can be used for insert permissions:
id
bucket_id
name
size
mime_type
Download
To download a file, a user must have theselect
permission to the storage.files
table. All columns must be granted.
Delete
To delete a file, a user must have thedelete
permission to the storage.files
table.
Updating an existing file is not supported. If you need to update a file, delete the file and upload a new file.
Just deleting the file metadata in the storage.files
table does not delete the actual file. Always delete files via Nhost Storage. This way, both the file metadata and the actual file are deleted.
Image Transformation
Images can be transformed, on the fly, by adding query parameters. The following query parameters are available:w
- Width of the image in pixels.h
- Height of the image in pixels.
?w=500
):