Import files into media library using REST

Nicolas Zawada asked on April 28, 2016 16:44

Hello

I am importing data from a non-Kentico website into Kentico via the REST API. This went alright, but now I'm stuck with media (pictures). I know this wasn't supported in K7 & K8. So my question is, is this already supported? If not, what is another recommended way to import media?

Thanks in advance.

Recent Answers


Trevor Fayas answered on April 28, 2016 17:22

I can't find anything in documentation either. However, there are other options other than REST API to get media files in.

1: Since you can send Attachment Files VIA rest, you could send files via an attachment REST create then use either a custom hook to translate those attachments to the media library

2: Since the Media library is files (with a database tag), you could setup an FTP to the existing site and pull the files down, then via API go through the files and if it hasn't been imported into the database, import it.

3: Create a task that takes either a single URL or a list of URLs for the file and both grabs the file and enters it into the database.

the code to import is something like this (may need to modify). It should be noted that at least for 8, when you download a file, then import it into the database, it tends to re-create the file, so i would download the physical file to a temp drive, then import it into the right spot.

CMS.IO.DirectoryHelper.EnsureDiskPath(fullPath, rootDocPath);


            int assetLibraryID = CMS.MediaLibrary.MediaLibraryInfoProvider.GetMediaLibraryInfo("Assets", "GlobalContent").LibraryID;
            // Download to a temporary path, when we insert into database it re-creates the doc.
            System.Net.WebClient webClient = new System.Net.WebClient();


            if (System.IO.File.Exists(fullPath))
            {
                // Replace file and update the Media Library Info
                System.IO.File.Delete(fullPath);
                webClient.DownloadFile(fileUrl, fullPath);
                var UpdatedFileInfo = CMS.MediaLibrary.MediaFileInfoProvider.GetMediaFileInfo(assetLibraryID, combinedPath.Replace("\\", "/") + "/" + FileName);
                var newFile = new System.IO.FileInfo(fullPath);
                UpdatedFileInfo.FileSize = newFile.Length;
                UpdatedFileInfo.Update();

            }
            else
            {
                // Upload to temporary folder, then create database Media File and set proper path.
                webClient.DownloadFile(fileUrl, uploadPath);
                var newFileInfo = new CMS.MediaLibrary.MediaFileInfo(uploadPath, assetLibraryID);
                newFileInfo.FilePath = combinedPath.Replace("\\", "/") + "/" + FileName;
                newFileInfo.Insert();

                // Delete temporary file
                System.IO.File.Delete(uploadPath);
            }
0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on April 28, 2016 17:43

Hi,

You may use a DAM connector by PucturePark on Kentico market place. It's approved by Kentico.

https://devnet.kentico.com/marketplace/integration/dam-connector-for-kentico

Check this and see if this helps you.

Cheers Chetan

1 votesVote for this answer Mark as a Correct answer

Nicolas Zawada answered on April 29, 2016 00:08

Hey Trevor Fayas, I forgot to mention that I use Azure Blop storage for media. So I can't really work with physical file transfer, I think.

Chetan, this DAM connector only links to the old media library, and doesn't transfer it to the Azure Blop storage?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.