File Module
In this document, you'll learn about the File Module and its providers.
What is the File Module?#
The File Module exposes the functionalities to upload assets, such as product images, to the Medusa application. Medusa uses the File Module in its core commerce features for all file operations, and you can use it in your custom features as well.
How to Use the File Module?#
You can use the File Module as part of the workflows you build for your custom features. A workflow is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism.
In a step of your workflow, you can resolve the File Module's service and use its methods to upload files, retrieve files, or delete files.
For example:
1import { Modules } from "@medusajs/framework/utils"2import { 3 createStep,4 createWorkflow,5 StepResponse,6 WorkflowResponse,7} from "@medusajs/framework/workflows-sdk"8 9const step1 = createStep(10 "step-1",11 async ({}, { container }) => {12 const fileModuleService = container.resolve(13 Modules.FILE14 )15 16 const { url } = await fileModuleService.retrieveFile("image.png")17 18 return new StepResponse(url)19 } 20)21 22export const workflow = createWorkflow(23 "workflow-1",24 () => {25 const url = step1()26 27 return new WorkflowResponse(url)28 }29)
In the example above, you create a workflow that has a step. In the step, you resolve the service of the File Module from the Medusa container.
Then, you use the retrieveFile
method of the File Module to retrieve the URL of the file with the name "image.png"
. The URL is then returned as a response from the step and the workflow.
What is a File Module Provider?#
A File Module Provider implements the underlying logic of handling uploads and downloads of assets, such as integrating third-party services. The File Module then uses the registered File Module Provider to handle file operations.
By default, Medusa uses the Local File Module. This module uploads files to the uploads
directory of your Medusa application.
This is useful for development. However, for production, it’s highly recommended to use other File Module Providers, such as the S3 File Module Provider. You can also Create a File Provider.