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.
You can upload files to a specific bucket and include custom metadata:
- JavaScript
- HTTP
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. Files are served via CDN for optimal performance.- JavaScript
- HTTP
- Conditional requests using
If-Modified-Since
,If-None-Match
headers for caching - Range requests for partial file downloads using the
Range
header - Image transformations via query parameters (see Image Transformation section)
Pre-signed URL
Pre-signed URLs work differently from public URLs and are ideal for temporary, secure access. How they work:- Permission check happens only when requesting the pre-signed URL
- Once generated, anyone with the URL can download the file (no additional auth required)
- URLs expire after a configurable time period
- Default bucket: 30 seconds expiration
- Configurable per bucket via the
download_expiration
field (in seconds)
- JavaScript
- HTTP
Delete File
Delete a file and the file metadata in the database. This permanently removes both the file content from storage and its associated metadata.- JavaScript
- HTTP
File deletion is permanent and cannot be undone. Always delete files via the Nhost Storage API to ensure both the file content and metadata are properly removed.
Replace File
Replace an existing file with new content while preserving the file ID. This is useful when you need to update a file without changing references to it.- JavaScript
- HTTP
- Sets
isUploaded
flag tofalse
during the update process - Replaces the file content in storage
- Updates file metadata (size, mime-type, etc.)
- Sets
isUploaded
flag back totrue
Buckets
Buckets are containers used to organize files and group permissions. They provide a way to apply different configurations and access controls to different types of files. Buckets are stored in thestorage.buckets
table in your database and are accessible via the buckets
field in your GraphQL API.
Bucket Configuration
Each bucket can be configured with the following properties:File Restrictions
- Minimum size - Minimum file size in bytes
- Maximum size - Maximum file size in bytes to prevent large uploads
Access Control
- Cache control - HTTP cache headers for files in this bucket
- Allow pre-signed URLs - Whether files can be accessed via pre-signed URLs
- Download expiration - Expiration time in seconds for pre-signed URLs
Default Bucket
- Every Nhost project includes a
default
bucket - Used automatically when no bucket is specified during upload
- Cannot be deleted (system requirement)
- Has standard configuration suitable for most use cases
Working with Buckets
Upload to Specific Bucket
GraphQL Bucket Management
You can query and manage buckets through your GraphQL API:Common Bucket Patterns
User Uploads: Separate public and private user contentPermissions
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.
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 to file URLs. This feature works with both public URLs and pre-signed URLs, allowing you to resize, optimize, and convert images without pre-processing.Available Transformations
Resize Parameters
w
- Maximum width to resize image to while maintaining aspect ratioh
- Maximum height to resize image to while maintaining aspect ratio
Quality and Format
q
- Image quality (1-100). Applies to JPEG, WebP, and PNG filesf
- Output format. Options:auto
,same
,jpeg
,webp
,png
,avif
auto
- Uses content negotiation based on Accept headersame
- Keeps original format (default)
Effects
b
- Blur the image using sigma value (0 or higher)
Examples
- JavaScript
- HTTP
Use Cases
Responsive Images: Generate different sizes for various screen resolutionsImage transformations are cached at the CDN level for optimal performance. The first request might be slower as the transformed image is generated and cached.