I registered to tell you a huge Thank you!
I had a task to upload images from the external resource and create entity with it. And I had urls for images. My idea was to pass stream to the upload module without saving it to the hdd. And your example of using provider helped a lot. I debugged upload module and also found the provider but had no luck to use it in a right way… Thank you!
Kind of my result:
const uploadProvider = strapi.plugin('upload').service('provider');
const config = strapi.config.get('plugin.upload');
const { photos, ...jsonData } = record;
const media = await Promise.all(photos.map(async photo => {
const file = await axios.get(photo.url, {
responseType: 'stream',
});
const fileNameNoExt = path.basename(photo.name, path.extname(photo.name));
const entity = {
name: `${jsonData.name} ${fileNameNoExt}`,
hash: `${slugify(jsonData.name)}_${fileNameNoExt}`,
ext: path.extname(photo.name),
mime: mimeTypes.lookup(photo.name),
size: file.headers['content-length'],
provider: config.provider,
getStream: () => file.data,
};
await uploadProvider.upload(entity);
return strapi
.query('plugin::upload.file')
.create({ data: entity });
}));
try {
return await strapi.entityService.create('api::product.product', {
data: {
...jsonData,
media,
},
});
} catch (e) {
...
}