I need to run migration script for more than thousands records.
This is my code so far:
const { MOVIES } = require("../movies");
const axios = require("axios");
module.exports = {
async up(knex) {
try {
await knex.transaction(async (trx) => {
const uploadImageFromUrl = async (url, hash, name) => {
const file = await axios.get(url, {
responseType: "stream",
});
const entity = {
name,
hash,
ext: "png",
mime: "image/png",
size: file.headers["content-length"],
provider: "aws-s3",
getStream: () => file.data,
};
const image = await strapi
.plugin("upload")
.service("provider")
.upload(entity);
console.log(image); // this is undefined
return image;
};
for (const movie of MOVIES) {
try {
const uploadedLogo = await uploadImageFromUrl(movie.logo, movie.hash, movie.name);
await strapi
.service("api::movie.movie")
.createWithId({
data: {
name: movie.name,
slug: movie.slug,
logo: uploadedLogo.id,
createdAt: new Date(),
updatedAt: new Date(),
publishedAt: new Date(),
},
});
console.log(`Successfully created movie: ${movie.name}`);
} catch (error) {
console.error(
`Failed to create movie: ${movie.name}`,
error
);
}
}
});
} catch (error) {
console.error("DATABASE MIGRATION ERROR:", error);
throw error;
}
},
};
Upload to S3 works as I can see image in bucket. This is the only method that worked in migration script. Now as I don’t get URL from:
const image = await strapi
.plugin("upload")
.service("provider")
.upload(entity);
I don’t know how proceed. I suspect that I need to insert file row into file table. I can potentially generate S3 URL based on BASE_URL + file name but how should I proceed with adding relation between image and movie?
Does anyone know?
thanks!