The only way I have been able to use the in-built strapi upload service is if you first save the file to a folder. I’m not sure what the implications will be for different hosting providers but you can probably programmatically delete this stored file.
const fetch = require("node-fetch"); // is already a strapi dependency
const mime = require("mime-types"); // is already a strapi dependency
const path = require("path");
const fs = require("fs");
const url = "YOUR_URL";
const filepath = path.basename(url);
const filename = path.basename(filepath, path.extname(filepath));
let dest = fs.createWriteStream(filepath);
await new Promise((resolve, reject) =>
fetch(url)
.then((res) => res.body.pipe(dest).on("finish", () => resolve(true))) // you can add more error handling
.catch((e) => reject(e))
);
const fileStat = fs.statSync(filepath);
try {
await strapi.plugins.upload.services.upload.upload({
data: {},
files: {
path: filepath,
name: filename,
type: mime.lookup(filepath),
size: fileStat.size,
},
});
} catch (e) {
console.log("Error", e);
}