I am following: Asset API Technical Reference - Documentation - Prismic
const uploadFile = async (filePath: fs.PathLike, token: string) => {
const formData = new FormData();
formData.append('file', fs.createReadStream(filePath));
const response = await axios.post(assetUrl, formData, {
headers: {
Authorization: `Bearer ${token}`,
'x-api-key': apiKey,
'Content-Type': 'multipart/form-data',
'repository': instanceRepository,
'Accept': "application/json"
},
});
await delay(2000);
return response
};
I need to migrate assets so that the asset ids match when I use the migration API, however, they are always changed when I use the Prismic Asset API. I cannot seem to force it to have the same ID. It makes sense but I am not sure how to deal with it.
const uploadFile = async (filePath, instanceRepository, assetId, assetUrl) => {
const formData = new FormData();
formData.append('file', await fileFromPath(filePath));
formData.append('assetId', assetId);
formData.append('id', assetId);
formData.append('url', assetUrl);
console.log(formData, 'formData');
const ResponseUrl = new URL('https://asset-api.prismic.io/assets');
const authResponse = await fetch('https://auth.prismic.io/login', {
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify({
email,
password,
}),
});
const token = await authResponse.text();
const response = await axios.post(ResponseUrl, formData, {
headers: {
Authorization: `Bearer ${token}`,
'x-api-key': destApiKey,
'Content-Type': 'multipart/form-data',
'repository': instanceRepository,
'Accept': "application/json"
},
});
if (!response.status === 200) {
return console.log('Error:', response.statusText);
}
await delay(200);
console.log('Initial assetId:', assetId);
console.log('Initial response.data.id:', response.data.id);
response.data.id = assetId;
response.data.url = assetUrl;
console.log('Updated response.data.id:', response.data.id);
console.log('response data:', response.data);
return response;
};
async function fetchAssets({ assetType = 'image', limit = 100, repository } = {}) {
const url = new URL('https://asset-api.prismic.io/assets');
const authResponse = await fetch('https://auth.prismic.io/login', {
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify({
email,
password,
}),
});
const token = await authResponse.text();
const params = {
assetType,
limit,
};
Object.keys(params).forEach(key => {
if (params[key]) {
url.searchParams.append(key, params[key]);
}
});
if (!token) {
console.error('PRISMIC_API_TOKEN is not set');
return;
}
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
'repository': repository,
'x-api-key': apiKey,
},
method: 'GET'
});
if (!response.ok) {
const responseBody = await response.text();
try {
const formattedResponse = JSON.parse(responseBody);
console.error('Response body (JSON):', JSON.stringify(formattedResponse, null, 2));
} catch (e) {
console.error('Response body (String):', responseBody);
}
console.error('Error:', response.statusText);
console.log('Doc uid stopped at:', response);
console.log('===========================================');
return;
}
const data = await response.json();
const assets = Object.entries(data.items);
let uploadCount = 0;
for (const [key, asset] of assets) {
const { url, id, filename } = asset;
if (!url) {
console.error('Invalid URL:', url);
continue;
}
const assetResponse = await fetch(url);
const filePath = path.join(__dirname, asset.filename);
const buffer = await assetResponse.arrayBuffer();
fs.writeFileSync(filePath, Buffer.from(buffer));
migratedAssetIds.push({ uploadCount, id, filename, url });
try {
await uploadFile(filePath, process.env.TARGET_API_KEY, destRepository, id, url);
console.log('Uploaded asset:', asset.filename, asset.id, "to", destRepository);
uploadCount++;
console.log('number uploaded', uploadCount)
} catch (e) {
console.log(e);
} finally {
console.log('Deleting/unlinking file:', filePath);
fs.unlinkSync(filePath);
}
}
fs.writeFileSync('migrated_asset_ids.json', JSON.stringify(migratedAssetIds, null, 2));
console.log('Migrated asset ids:', migratedAssetIds, 'number of assets:', migratedAssetIds.length);
return
}
Any help?