Storing Sitecore Media Library Assets in File System VS Storing in Database





5.00/5 (2 votes)
We will see how to easily store Sitecore media library assets in the file system and also compare this approach with the default (storing in database).
Introduction
All the assets that we keep inside Media Library folder of the content tree are stored inside the database by Sitecore (default behavior). We can change it and store assets in the file system by making some easy configuration changes.
We will see how this can be achieved. We will compare both the approaches (storing in file system and database) by checking their advantages and disadvantages and try to figure out which approach suits which scenario.
Background
Let us first see where Sitecore stores media assets in the database. Login to the database server where Sitecore DBs are present. Open Master DB, there is a Blobs table. This table will store all media library assets.
This table has 6 columns out of which 2 are important to note, namely BlobId & Data. BlobId
column contains a GUID
to uniquely identify a blob. Data column contains the binary data for the blob.
So, when we upload an asset in the media library, Sitecore creates a media item with a field named media
. Sitecore stores the actual asset in blob
table and takes BlobId
field value from this table and puts in the media field of the media item. This way, Sitecore is separating blob storage and content storage. Sitecore leverages this concept to store media in the file system instead of storing it in the DB, if required by user.
How to Store Media Assets in the File System?
Sitecore stores all media library assets in the database. This is because of the following configurable setting present in the web.config file of the Sitecore website.
<!-- UPLOAD AS FILES
Controls whether Sitecore stores media as files or as database records by default.
This setting is ignored if the Media.DisableFileMedia setting is true.
Default: false
-->
<setting name="Media.UploadAsFiles" value="false"/>
To make Sitecore store media assets in the file system instead of in the DB, we need to update the value of the above setting to true
. See below.
<setting name="Media.UploadAsFiles" value="true"/>
Now, Sitecore will start storing assets in the file system. By default, it stores them at path, "/App_Data/MediaFiles". Sitecore has a configurable setting for changing this as well. See below.
<!-- MEDIA - FILE FOLDER
The folder under which media files are stored by the system.
Default value: /App_Data/MediaFiles
It should be different from MediaFolder setting
-->
<setting name="Media.FileFolder" value="/App_Data/MediaFiles"/>
We can change the value of the above setting to store blobs anywhere in the file system.
Storing Media Assets in Database VS Storing in the File System
So, when should we use file system storage of media assets instead of default database storage? Let us compare both of them to figure it out.
Database storage |
File system storage |
Pros
|
Pros
|
Cons
|
Cons
|
Learning
Taking into account all the pros & cons mentioned above of both the approaches, it can be said that the tradeoff of losing CMS provided features (security, workflow of items, publishing) by storing in DB can be neglected due to the improved performance by storing assets on the file system.
Therefore, if the overall number of images is less and the number of large images (larger than 1 MB) is less, then storing assets in database should be considered for convenience, otherwise we should consider the file system route.
History
- 15th February, 2016: First/draft version of the tip