Interface: StorageActionWriter | Convex Developer Hub
server.StorageActionWriter
An interface to read and write files to storage within Convex actions and HTTP actions.
In actions, ctx.storage has additional methods not available in mutations:
get() to download a file as a Blob, and store() to upload a Blob directly.
Example
// In an action, download and re-upload a file:
const blob = await ctx.storage.get(storageId);
if (blob) {
const newStorageId = await ctx.storage.store(blob);
}
-
↳
StorageActionWriter
getUrl
▸ getUrl(storageId): Promise<null | string>
Get the URL for a file in storage by its Id<"_storage">.
The GET response includes a standard HTTP Digest header with a sha256 checksum.
Example
const url = await ctx.storage.getUrl(storageId);
Parameters
| Name | Type | Description |
|---|---|---|
storageId | GenericId<"_storage"> | The Id<"_storage"> of the file to fetch from Convex storage. |
Returns
Promise<null | string>
- A URL which fetches the file via an HTTP GET, or
nullif the file no longer exists.
Inherited from
Defined in
▸ getUrl<T>(storageId): Promise<null | string>
Deprecated
Passing a string is deprecated, use storage.getUrl(Id<"_storage">) instead.
Get the URL for a file in storage by its StorageId.
The GET response includes a standard HTTP Digest header with a sha256 checksum.
Type parameters
| Name | Type |
|---|---|
T | extends string |
Parameters
| Name | Type | Description |
|---|---|---|
storageId | T extends { __tableName: any } ? never : T | The StorageId of the file to fetch from Convex storage. |
Returns
Promise<null | string>
- A url which fetches the file via an HTTP GET, or
nullif it no longer exists.
Inherited from
Defined in
getMetadata
▸ getMetadata(storageId): Promise<null | FileMetadata>
Deprecated
Use ctx.db.system.get("_storage", storageId) instead, which returns
equivalent metadata from the _storage system table (with a slightly different shape):
const metadata = await ctx.db.system.get("_storage", storageId);
// { _id, _creationTime, sha256, size, contentType? }
Get metadata for a file.
Parameters
| Name | Type | Description |
|---|---|---|
storageId | GenericId<"_storage"> | The Id<"_storage"> of the file. |
Returns
Promise<null | FileMetadata>
- A FileMetadata object if found or
nullif not found.
Inherited from
Defined in
▸ getMetadata<T>(storageId): Promise<null | FileMetadata>
Deprecated
Use ctx.db.system.get("_storage", storageId) instead.
Get metadata for a file.
Type parameters
| Name | Type |
|---|---|
T | extends string |
Parameters
| Name | Type | Description |
|---|---|---|
storageId | T extends { __tableName: any } ? never : T | The StorageId of the file. |
Returns
Promise<null | FileMetadata>
- A FileMetadata object if found or
nullif not found.
Inherited from
Defined in
generateUploadUrl
▸ generateUploadUrl(): Promise<string>
Generate a short-lived URL for uploading a file into storage.
The client should make a POST request to this URL with the file as the
body. The response will be a JSON object containing a newly allocated
Id<"_storage"> ({ storageId: "..." }).
Example
// In a mutation, generate the upload URL:
export const generateUploadUrl = mutation({
args: {},
returns: v.string(),
handler: async (ctx) => {
return await ctx.storage.generateUploadUrl();
},
});
// On the client, upload the file:
// const uploadUrl = await generateUploadUrl();
// const result = await fetch(uploadUrl, { method: "POST", body: file });
// const { storageId } = await result.json();
Returns
Promise<string>
- A short-lived URL for uploading a file via HTTP POST.
Inherited from
StorageWriter.generateUploadUrl
Defined in
delete
▸ delete(storageId): Promise<void>
Delete a file from Convex storage.
Once a file is deleted, any URLs previously generated by getUrl will return 404s.
Example
await ctx.storage.delete(storageId);
Parameters
| Name | Type | Description |
|---|---|---|
storageId | GenericId<"_storage"> | The Id<"_storage"> of the file to delete from Convex storage. |
Returns
Promise<void>
Inherited from
Defined in
▸ delete<T>(storageId): Promise<void>
Deprecated
Passing a string is deprecated, use storage.delete(Id<"_storage">) instead.
Delete a file from Convex storage.
Once a file is deleted, any URLs previously generated by getUrl will return 404s.
Type parameters
| Name | Type |
|---|---|
T | extends string |
Parameters
| Name | Type | Description |
|---|---|---|
storageId | T extends { __tableName: any } ? never : T | The StorageId of the file to delete from Convex storage. |
Returns
Promise<void>
Inherited from
Defined in
get
▸ get(storageId): Promise<null | Blob>
Download a file from storage as a Blob.
Only available in actions and HTTP actions (not in mutations or queries).
Parameters
| Name | Type | Description |
|---|---|---|
storageId | GenericId<"_storage"> | The Id<"_storage"> of the file. |
Returns
Promise<null | Blob>
A Blob containing the file contents, or null if the file doesn't exist.
Defined in
▸ get<T>(storageId): Promise<null | Blob>
Deprecated
Passing a string is deprecated, use storage.get(Id<"_storage">) instead.
Get a Blob containing the file associated with the provided StorageId, or null if there is no file.
Type parameters
| Name | Type |
|---|---|
T | extends string |
Parameters
| Name | Type |
|---|---|
storageId | T extends { __tableName: any } ? never : T |
Returns
Promise<null | Blob>
Defined in
store
▸ store(blob, options?): Promise<GenericId<"_storage">>
Upload a Blob directly to storage.
Only available in actions and HTTP actions. For client-side uploads from
mutations, use generateUploadUrl() instead.
Example
const storageId = await ctx.storage.store(blob);
// Save storageId to the database via ctx.runMutation
Parameters
| Name | Type | Description |
|---|---|---|
blob | Blob | The Blob to store. |
options? | Object | Optional settings. Pass sha256 to verify the file integrity. |
options.sha256? | string | - |
Returns
Promise<GenericId<"_storage">>
The Id<"_storage"> of the newly stored file.