It is great that Kentico Kontent Management REST API allows uploading the assets, but how to upload an asset under a specific folder and how to achieve it via Javascript Management SDK?
Originally this question was asked on StackOverflow.
The answer is pretty simple in the end. The only hard thing is to find the right part of the REST Management API documentation and then find this API provided via Javascript/Typescript Management SDK wrapper.
JS SDK should wrap the capabilities of the Kontent REST Management API v2, in this case, the crucial is folder property of the Asset model.
So the insert script when uploading an asset and then submitting it under specific folder would look like that:
const folders = await mClient.listAssetFolders()
.toPromise(); // get folders
const desiredFolder = folders.items
.find(folder => folder.name = "Pets"); // set desired folder
const assetData = await getAssetDataDataFromUrl(article.image.url)
.toPromise(); // load binary how you want it
const assetObject = await mClient
.uploadBinaryFile()
.withData(assetData)
.toPromise(); // upload binary
const asset = await mClient.addAsset()
.withData({
descriptions: [{
language: {
codename: argv.language
},
description: `Image for article ${article.title}`
}],
external_id: article.image.id,
file_reference: {
...assetObject.data
},
folder: {
id: desiredFolder.id,
// also possible to use external ID:
// external_id: desiredFolder.externalId
}
})
.toPromise();
If you already have the asset in Kontent, you could also just assign an aseet under your desired folder:
const updatedAsset = await mClient.upsertAsset().withData({
// not sure if you need to add whole asset object like:
// `...asset`, but I think it is not necessary
folder: {
id: desiredFolder.id,
// also possible to use external ID:
// external_id: desiredFolder.externalId
}
}).toPromise()
Original answer on StackOverflow.