6

What's the best approach to migrate disk-based media into the database? We inherited a large enterprise site that's getting forklifted to Sitecore 8.2, and it currently uses file-based media. We want to move these into the database, as that's our standard, and makes the solution more scalable.

I found a reference somewhere to a shared source component, but the link is dead: http://trac.sitecore.net/MediaConversionTool/ (DEAD LINK)

Is there anything particular for Sitecore 8.x that others are using?

8

You have a few good options.

Module:

You were probably looking for this.

https://marketplace.sitecore.net/en/Modules/Media_Conversion_Tool.aspx

Custom Code

I would convert the code in this article to a script in Sitecore PowerShell Extensions since you can both run the existing report for finding them plus run the conversion. Actually sounds like a nice addition to the Media Library Maintenance module bundled with SPE.

https://adesev.wordpress.com/2014/08/06/convert-file-based-media-to-database-storage/

Update

Couldn't help myself so I decided to try this myself.

I first ran the SPE report to confirm that there were items that were file based.

Database or File based media

There is currently one on my system.

File based item

Little rough and needs some testing but this should do it for you in SPE.

function Get-MediaItemFileBased {
    Get-ChildItem -Path "master:\sitecore\media library" -Recurse | 
        Where-Object { $_.TemplateID -ne [Sitecore.TemplateIDs]::MediaFolder } |
        ForEach-Object {
            $mediaItem = New-Object Sitecore.Data.Items.MediaItem $PSItem
            if($mediaItem.FileBased) {
                $mediaItem
            }
        }
}

function ConvertTo-MediaItemDatabaseBased {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [ValidateNotNullOrEmpty()]
        [Sitecore.Data.Items.MediaItem]$MediaItem
    )

    if($MediaItem -eq $null) { continue }

    $filePath = [Sitecore.IO.FileUtil]::MapPath($MediaItem.FilePath)
    $fileInfo = Get-Item -Path $filePath
    if($fileInfo.Exists) {
        $mediaCreator = New-Object Sitecore.Resources.Media.MediaCreator
        $mediaItemFullPath = $MediaItem.InnerItem.Paths.Path
        $mediaCreatorOptions = New-Object Sitecore.Resources.Media.MediaCreatorOptions
        $mediaCreatorOptions.FileBased = $false
        $mediaCreatorOptions.Destination = $mediaItemFullPath
        $mediaCreatorOptions.KeepExisting = $false

        $filename = $mediaItem.Name + "." + $mediaItem.Extension
        $mediaCreator.AttachStreamToMediaItem($fileInfo.Open([System.IO.FileMode]::Open), $mediaItemFullPath, $filename, $mediaCreatorOptions)
    }
}

Get-MediaItemFileBased | ConvertTo-MediaItemDatabaseBased
2

You could just copy them to the upload folder in your web root. Sitecore watches that folder for changes and adds any files that show up there to the media library. You will want to make sure you have a decent folder structure set up ahead of time as it uploads to the root of the media library unless you add subfolders.

  • We tried this, but it kept creating duplicate items rather than updating the existing one – Tony Kiernan Feb 21 at 12:36
  • The original question was about a onetime import, not updating media that already exists. I don’t think most people would want this to overwrite existing items by default. – Ben Golden Feb 21 at 17:59
  • The original question was about migrating from disk based media to database. So, the items already exist. They just need the blobs created (I assume) – Tony Kiernan Feb 21 at 21:30
  • I suppose you might be right there. The way I read it (2+ years ago), I thought they were moving to Sitecore from some other system that used file-based media and just needed to do a bulk import. That may have been my misunderstanding. – Ben Golden Feb 22 at 3:27
  • My comment is not a criticism but purely for anyone that comes by this (as I did) trying to find out how to make that migration – Tony Kiernan Feb 22 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.