Blob Storage API

BlobStorageManager provides general blob storage facilities. When working with blobs you use LibrariesManager to get the album that you are to alter. Blob storage providers configuration is stored in the LibrariesConfig file.

The following samples demonstrate the usage of blob storage providers when working with libraries:

Creating blob storage provider

This sample creates a blob storage provider:

var providers = Config.GetManager().GetSection<LibrariesConfig>().BlobStorage.Providers;
 
providers.Add(new DataProviderSettings(providers)
{
    Name = "MyNewFileSystemProvider",
    Title = "BlobStorageFileSystemDefaultProviderTitle",
    Description = "BlobStorageFileSystemDefaultProviderDescription",
    ResourceClassId = typeof(LibrariesResources).Name,
    ProviderType = typeof(FileSystemStorageProvider),
    Parameters = new NameValueCollection() { { "applicationName", "/Blob" },
        { FileSystemStorageProvider.FolderNameKey, "~/App_Data/Storage/FileSystem" } }
});

Creating an album and uploading images in it

This sample creates an album with the specified blob storage provider and uploads there the images from the specified path. The method parameters are the root folder that contains the images and the name of the blob storage provider.

public static void CreateAlbumAndUploadImages(string rootFolderPath, string blobStorageProvider = null)
{
    DirectoryInfo rootFolder = new DirectoryInfo(rootFolderPath);
    if (!rootFolder.Exists)
        throw new ApplicationException(string.Format("A folder with path '{0}' does not exist", rootFolder.FullName));
 
    var manager = LibrariesManager.GetManager();
 
    var albumTitle = rootFolder.Name;
    var albumName = albumTitle.ToLower().Replace(' ', '_');
    if (manager.GetAlbums().Where(i => i.UrlName == albumName).Any())
        throw new ApplicationException(string.Format("An album with UrlName '{0}' already exists", albumName));
 
    if (blobStorageProvider == null)
        blobStorageProvider = Config.Get<LibrariesConfig>().BlobStorage.DefaultProvider;
 
    var album = manager.CreateAlbum();
    album.Title = albumTitle;
    album.UrlName = albumName;
    album.BlobStorageProvider = blobStorageProvider;
    manager.RecompileItemUrls<Album>(album);
 
    foreach (var file in rootFolder.GetFiles())
    {
        var image = manager.CreateImage();
 
        var extension = file.Extension;
        var title = file.Name;
        if (extension.Length > 0)
            title = title.Substring(0, title.Length - extension.Length);
        image.Parent = album;
        image.Title = title;
        image.UrlName = title.ToLower().Replace(' ', '-');
        manager.RecompileItemUrls<Telerik.Sitefinity.Libraries.Model.Image>(image);
 
        using (var fileStream = file.OpenRead())
        {
            manager.Upload(image, fileStream, file.Extension);
        }
        manager.Publish(image);
    }
    manager.SaveChanges();
}

First, you get the LibrariesManager and create the new album based on the root folder name. Then, you set the blob storage provider for the new ablum. You can find the available storage providers inConfig.Get<LibrariesConfig>().BlobStorage.Providers or BlobStorageManager.ProvidersCollection. Finally, you upload the images from the folder in the album that automatically stores all the images in the location pointed by the blob storage provider.

Changing the blob storage provider of an album

This sample changes the album blob storage provider and begins transferring album images to the new blob storage location of the album.

NOTE: This could be a long time operation. If it does not finish for some reason, it can be resumed.

The method parameters are the album name and the name of the new blob storage provider for the album.

public static void BeginChangeAlbumBlobStorage(string albumName, string newBlobStorageProvider)
{
    var manager = LibrariesManager.GetManager();
 
    var album = manager.GetAlbums().Where(i => i.UrlName == albumName).FirstOrDefault();
    album.BlobStorageProvider = newBlobStorageProvider;
 
    var imagesToTransfer = album.Images.Where(i => i.Status == ContentLifecycleStatus.Master && i.BlobStorageProvider != newBlobStorageProvider);
    foreach (var image in imagesToTransfer)
    {
        manager.TransferItemStorage(image, newBlobStorageProvider);
        var liveImage = manager.GetLive(image);
        if (liveImage != null)
            manager.TransferItemStorage(liveImage, newBlobStorageProvider);
        var tempImage = manager.GetTemp(image);
        if (tempImage != null)
            manager.TransferItemStorage(tempImage, newBlobStorageProvider);
 
        manager.SaveChanges();
    }
}

First, you get the LibrariesManager and the album based on albumName. Then, you get all images that have a blob storage provider different from the new provider and, finally, transfer them to the new location pointed by newBlobStorageProvider.

Moving all images from one album to another

This sample moves all images from one album to another. If the target album uses different blob storage, the images will be transferred to the target album storage.

NOTE: This could be a long time operation. If it does not finish for some reason, it can be resumed.

The method parameters are the names for the source and target albums.

public static void BeginMoveAlbumImagesToAnotherAlbum(string sourceAlbumName, string targetAlbumName)
{
    var manager = LibrariesManager.GetManager();
 
    var sourceAlbum = manager.GetAlbums().Where(i => i.UrlName == sourceAlbumName).FirstOrDefault();
    if (sourceAlbum != null)
    {
        var targetAlbum = manager.GetAlbums().Where(i => i.UrlName == targetAlbumName).FirstOrDefault();
        if (targetAlbum != null)
        {
            var imagesToMove = sourceAlbum.Images.Where(i => i.Status == ContentLifecycleStatus.Master).ToList();
            foreach (var image in imagesToMove)
            {
                manager.ChangeItemParent(image, targetAlbum, true);
                manager.SaveChanges();
            }
        }
    }
}

First, you get the LibrariesManager and the source and target albums based on sourceAlbumName and targetAlbumName. Then, you change the parent for every item to the target album.

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK