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

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

3 Likes