We need to create a folder for each user and store their files within it. Is it possible to do it in strapi v4 using media uploads

[details=“System Information”]

  • Strapi Version: 4
  • Operating System: windows 10
  • Database: mysql
  • Node Version: v16.16.0
  • NPM Version: 8.11.0
  • Yarn Version: 1.22.15
    [/details] As the upload media folder becomes very cluttered is there a way to create folders in upload and group the media files.

1 Like

Well, folders are a thing in the media library provided by the upload plugin since I think v4.3.0:

I’m sure you can create and access folders with a custom controller.

1 Like

Thank you so much.
But how can I do this through an API.
Can I create a new folder using API and then upload the files into that folder

I solved this way, on my case, on top of the user level folder, I have a category folder, lets call it firstLevelFolder

example.file from user_123 would end up on


const uploadUserFilesService = async (fileArray, user) => {
  const uploadService = strapi.plugins.upload.services.upload;
  const folderService = strapi.plugins.upload.services.folder;

  let firstLevelFolderBase = await strapi.query('plugin::upload.folder').findOne({where: {name: 'firstLevelFolder'}});
  if (!firstLevelFolderBase) {
    await folderService.create({name: 'firstLevelFolder'})
    firstLevelFolderBase = await strapi.query('plugin::upload.folder').findOne({where: {name: 'firstLevelFolder'}});

  let userLevelFolder = await strapi.query('plugin::upload.folder').findOne({where: {name: user.username, parent: firstLevelFolderBase}});
  if (!userLevelFolder) {
    await folderService.create({name: user.username, parent: firstLevelFolderBase.id})
    userLevelFolder = await strapi.query('plugin::upload.folder').findOne({where: {name: user.username, parent: firstLevelFolderBase}});

  const uploadedFiles = await Promise.map(fileArray, async (file) => {
      return uploadService.upload({
        data: {
          path: `firstLevelFolder/${user.username}`,
          fileInfo: {folder: userLevelFolder.id},
        files: file

Thank you i will try this

I have created folders using the API(strapi.plugins.upload.services.folder) but how do i upload files to this folders

As per my understanding i can use the api/document to upload files to media library however i need to upload files to the newly created folder inside the media library.

Any updates please?

any updates??

You need to add the folder id and path on your entity, like this:

const usersFolder = await /*...*/;
const userFolder = await /*...*/;

const entity = {
    name: 'profile-picture',
    path: `${usersFolder.name}/${userFolder.name}`, // Only available on AWS S3
    folder: userFolder.id, // Parent folder ID
    folderPath: userFolder.path, // Parent folder path
    ext: path.extname(file.filename),
    mime: file.mimetype,
    provider: uploadConfig.provider,
    getStream() {
         return Readable.from(readStream);

await uploadService.upload(entity);

const res = await strapi.query('plugin::upload.file').create({
    data: entity,

I hope it helps