Strapi Upload Plugin: How to upload a remote file in a cron job

I am using the strapi upload plugin with s3 as a provider. It is working great when hitting the upload api endpoint on my strapi instance (/upload). However, I have a cron job in our repo that checks images in an s3 bucket and uploads them. Is there any way to call the upload plugin using the global strapi object, without having to make an http request in the cron job? The latter seems a little strange, since the cron job is running on the same server as strapi.

In my config/functions/cron.js file, I currently have this:

        const imageBuffer = await fetch(imageURL).then((response) => response.buffer());
        const formData = new FormData();
        formData.append('files', imageBuffer, { filename: imageURL.split('/').pop() });
        const uploadResult = await fetch('xxxxx/upload', {
            method: 'POST',
            body: formData
        }).then((response) => response.json());

I would prefer to do something simple like:

      const imageBuffer = await fetch(imageURL).then((response) => response.buffer());
      await strapi.plugins.upload(imageBuffer)

I have been trying to reverse engineer the what the plugin does in its controller file, but that doesn’t seem ideal either.

Any help from Strapi experts would be appreciated!

90 thoughts on “Strapi Upload Plugin: How to upload a remote file in a cron job”