Migrate Kentico EMS to Kentico Kontent - Part 2: Assets
Tags: kentico, migration, dotnet, assets
Published: 2019-10-24 13:18:35

##Introduction
In the last post I shared code examples on how to migrate your Kentico EMS Categories into Kentico Kontent Taxonomies. This post builds on top of those examples and illustrate how you can migrate Kentico EMS Media Library files to Assets in Kentico Kontent. A difference compared to importing Taxonomies is that this post uses the Kentico Cloud Content Management .NET SDK: {% github https://github.com/Kentico/content-management-sdk-net %}

##Work breakdown
Before we dig into the code, it is good to have a understanding of what kind of asset data Kentico Kontent requires. Adding assets consists out of the following two steps:

 * Upload the binary file as a file reference
 * Create the asset and pass in the file reference

So let's start with the first step by grabbing the media file, uploading it to Kentico Kontent and retrieving the file reference.

##Download the media file
In Kentico EMS media files are typically referenced using a permanent link (e.g. to prevent broken links when renaming or moving files). This can make it complex to find the actual file that we want to migrate. You could use the API to retrieve it from the database, write some code to locate the file in the media library folder or download the file from the live website. In my case I went with the last option and downloaded the media file using the following code:
```csharp
using (WebClient webClient = new WebClient())
{
   byte[] data = webClient.DownloadData(imageUrl);
}
```
In order to create a FileReference we need two more things: the name of the file and the content type. 

##Grab the filename
The filename should be relatively easy. Either take it from the Media File using the Kentico API or by executing a SQL query or use code like the following:
```csharp
Uri uri;
if (!Uri.TryCreate(imageUrl, UriKind.Absolute, out uri))
   uri = new Uri(domainName, imageUrl);

var fileName = Path.GetFileName(uri.LocalPath);
```
##Get the content type
As for the content type we could use the FileExtensionContentTypeProvider taken from the **Microsoft.AspNetCore.StaticFiles** NuGet package:
```csharp
new FileExtensionContentTypeProvider().TryGetContentType(source, out contentType);
```
This should be enough info to get the FileReference via the Content Management .NET SDK:
```csharp
FileReference fileReference = await client.UploadFileAsync(
   new FileContentSource(data, fileName, contentType));
```
##Upsert the asset
The final step of the exercise is to create the asset, pass in the FileReference and a Title (e.g. the Filename) and upserting it using a external ID (remember the previous post?). By passing in the external ID we will be able to insert and/or update (upsert) our Asset without having to worry if it exists or not :+1:
```csharp
AssetUpsertModel asset = new AssetUpsertModel
{
   FileReference = fileReference,
   Title = fileName
};

AssetModel assetResponse = await client.UpsertAssetByExternalIdAsync(assetExternalId, asset);
```

And that's all to it! In this post you have seen a different approach to migrating Kentico EMS data to Kentico Kontent using the Kentico Cloud Content Management .NET SDK.