Importing assets and media files to Optimizely through API

Vote:
 

We have around 50000 images that need to be imported into Optimizely CMS 12. Is there any way we can do this through an API like Content Management API? if not what are the other possible options?

#267039
Nov 19, 2021 12:22
Vote:
 

Hi Dejan Caric, Service API documentation mentions it is for importing assets into Optimizely commerce. But we don't have commerce in our project 

#267042
Nov 19, 2021 13:31
Vote:
 

Hi,

You don't need Commerce to use Service API. It works with CMS too.

#267045
Nov 19, 2021 15:10
Vote:
 

I've tested this with Alloy (CMS 11). Should be the same for CMS 12.

Test app:

internal class Program
{
    private static void Main(string[] args)
    {
        string token = GetToken();

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(@"https://localhost:44381/");
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

            var content = new MultipartFormDataContent();
            var filestream = new FileStream(@"c:\temp\MediaImport.zip", FileMode.Open);
            content.Add(new StreamContent(filestream), "file", "MediaImport.zip");
            var response = client.PostAsync("/episerverapi/commerce/import/assets", content).Result;
            Console.WriteLine(response.StatusCode.ToString()); }
    }


    private static string GetToken()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(@"https://localhost:44381/");
            var fields = new Dictionary<string, string>
            {
                { "grant_type", "password" },
                { "username", "admin" },
                { "password", "Administrator123!" }
            };
            var response = client.PostAsync("/episerverapi/token", new FormUrlEncodedContent(fields)).Result;
            if (response.StatusCode == HttpStatusCode.OK)
            {
                var content = response.Content.ReadAsStringAsync().Result;
                var token = JObject.Parse(content).GetValue("access_token");

                return token.ToString();
            }

            return string.Empty;
        }
    }
}

C:\temp\MediaImport.zip contains MediaImport.xml that looks like this:

<MediaImport>
  <ContentFolderUri></ContentFolderUri>
  <MediaType name="ImageFile">
    <ContentType>AlloyFind.Models.Media.ImageFile, AlloyFind</ContentType>
  </MediaType>
  <MediaGroup mediaTypeName="ImageFile">
    <Media>
      <IntegrationId>TEST_IMAGE</IntegrationId>
      <Name>Test Image.png</Name>
      <BlobContent>
       <ExternalUri>https://dummyimage.com/600x400/85c2f5/000000.png</ExternalUri>
      </BlobContent>
    </Media>
  </MediaGroup>
</MediaImport>

Here I want to import an external image to For All Sites folder.

And here's how it looks in action:

Hope this helps!

#267047
Nov 19, 2021 15:29
Vote:
 

Hi Dejan, Thanks for helping. Can you clarify one more doubt? Does Service API migrate assets to BLOB providers like Azure when they were added to the application? if so does that require any additional configuration other than adding a default provider on app settings?

#267181
Nov 22, 2021 15:36
Vote:
 

The Service API uses IContentRepository under the hood.

As long as your Optimizely instance is properly configured for Azure/DXP, you don't need to configure any additional/service api-specific app settings.

#267183
Nov 22, 2021 16:02
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.