How to Use File Module
In this document, you’ll learn about the different methods in the File Module's service and how to use them.
Resolve File Module's Service#
In your workflow's step, you can resolve the File Module's service from the Medusa container:
1 import { Modules } from "@medusajs/framework/utils" 2 import { createStep } from "@medusajs/framework/workflows-sdk" 3
4 const step1 = createStep ( 5 "step-1" , 6 async ( { } , { container } ) => { 7 const fileModuleService = container . resolve ( 8 Modules . FILE 9 ) 10 11 12 } 13 )
You can then use the File Module's service's methods in the step. The rest of this guide details these methods.
createFiles#
createFiles(data, sharedContext?): Promise<FileDTO []>#
This method uploads files to the designated file storage system.
Example
1 const [ file ] = await fileModuleService . createFiles ( [ { 2 filename : "product.png" , 3 mimeType : "image/png" , 4 content : "somecontent" 5 } ] )
Parameters
The files to be created.
A context used to share resources, such as transaction manager, between the application and the module.
Returns
createFiles(data, sharedContext?): Promise<FileDTO >#
This method uploads a file to the designated file storage system.
Example
1 const file = await fileModuleService . createFiles ( { 2 filename : "product.png" , 3 mimeType : "image/png" , 4 content : "somecontent" 5 } )
Parameters
The file to be created.
A context used to share resources, such as transaction manager, between the application and the module.
Returns
deleteFiles#
deleteFiles(ids, sharedContext?): Promise<void>#
This method deletes files by their IDs.
Example
await fileModuleService . deleteFiles ( [ "file_123" ] )
Parameters
The IDs of the files.
A context used to share resources, such as transaction manager, between the application and the module.
Returns
Resolves when the files are deleted successfully.
deleteFiles(id, sharedContext?): Promise<void>#
This method deletes a file by its ID.
Example
await fileModuleService . deleteFiles ( "file_123" )
Parameters
The ID of the file.
A context used to share resources, such as transaction manager, between the application and the module.
Returns
Resolves when the file is deleted successfully.
getAsBuffer#
This method retrieves a file by its ID and returns the file contents as a buffer. Under the hood, it will use the
file provider that was used to upload the file to retrieve the buffer.
Note: This is available starting from Medusa v2.8.0
.
Example#
1 const contents = await fileModuleService . getAsBuffer ( "file_123" ) 2 contents . toString ( 'utf-8' )
Parameters#
The ID of the file.
A context used to share resources, such as transaction manager, between the application and the module.
Returns#
A buffer of the file contents.
getDownloadStream#
This method retrieves a file by its ID and returns a stream to download the file. Under the hood, it will use the
file provider that was used to upload the file to retrievethe stream.
Note: This is available starting from Medusa v2.8.0
.
Example#
1 const stream = await fileModuleService . getDownloadStream ( "file_123" ) 2 writeable . pipe ( stream )
Parameters#
The ID of the file.
A context used to share resources, such as transaction manager, between the application and the module.
Returns#
A readable stream of the file contents.
getProvider#
This method returns the service of the configured File Module Provider in medusa-config.ts
. This is useful
if you want to execute custom methods defined in the provider's service or you need direct access to it.
Example#
1 const s3ProviderService = fileModuleService . getProvider ( ) 2
Returns#
getUploadFileUrls#
getUploadFileUrls(data, sharedContext?): Promise<UploadFileUrlDTO >#
This method gets the upload URL for a file.
Example
1 const uploadInfo = await fileModuleService . getUploadFileUrls ( { 2 filename : "product.png" , 3 mimeType : "image/png" , 4 } )
Parameters
The file information to get the upload URL for.
A context used to share resources, such as transaction manager, between the application and the module.
Returns
The upload URL for the file.
getUploadFileUrls(data, sharedContext?): Promise<UploadFileUrlDTO []>#
This method uploads files to the designated file storage system.
Example
1 const [ uploadInfo ] = await fileModuleService . getUploadFileUrls ( [ { 2 filename : "product.png" , 3 mimeType : "image/png" , 4 } ] )
Parameters
The file information to get the upload URL for.
A context used to share resources, such as transaction manager, between the application and the module.
Returns
The upload URLs for the files.
listAndCountFiles#
This method is used to list files and their count. It only supports filtering by ID.
Example#
const [ files ] = await fileModuleService . listAndCountFiles ( { id : "file_123" } )
Parameters#
The filters to apply on the retrieved files.
The configurations determining how the files are retrieved. Its properties, such as select
or relations
, accept the
attributes or relations associated with a file.
A context used to share resources, such as transaction manager, between the application and the module.
Returns#
Promise
Promise<[FileDTO [], number]> The list of files and their count.
listFiles#
This method is used to list files. It only supports filtering by ID.
Example#
const files = await fileModuleService . listFiles ( { id : [ "file_123" , "file_456" ] } )
Parameters#
The filters to apply on the retrieved files.
The configurations determining how the files are retrieved. Its properties, such as select
or relations
, accept the
attributes or relations associated with a file.
A context used to share resources, such as transaction manager, between the application and the module.
Returns#
retrieveFile#
This method retrieves a file with a downloadable URL by its ID.
Example#
const file = await fileModuleService . retrieveFile ( "file_123" )
Parameters#
The ID of the file.
The configurations determining how the file is retrieved. Its properties, such as select
or relations
, accept the
attributes or relations associated with a file.
A context used to share resources, such as transaction manager, between the application and the module.
Returns#