How can I post an image from local directory to the Strapi Media Library?

For anyone wondering how I spent my last 10 hours, here’s a working solution:

        const mime = require('mime-types'); //used to detect file's mime type
        const fileName = 'poster.jpg';
        const filePath = `./${fileName}`
        const stats = fs.statSync(filePath)
        await strapi.plugins.upload.services.upload.upload({
          data:{}, //mandatory declare the data(can be empty), otherwise it will give you an undefined error.
          files: {
          path: './poster.jpg',
          name: 'poster.jpg',
          type: mime.lookup(filePath), // mime type of the file
          size: stats.size,
        },
      });
1 Like