Skip to content

Commit

Permalink
Added constructor & extension method to create Blob Storage from conn…
Browse files Browse the repository at this point in the history
…ection string. Fixes #491
  • Loading branch information
tidyui committed Jan 26, 2019
1 parent eb0b43f commit 1222c1b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
31 changes: 20 additions & 11 deletions core/Piranha.Azure.BlobStorage/BlobStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*
*
* https://github.com/piranhacms/piranha.core
*
*
*/

using Microsoft.WindowsAzure.Storage;
Expand Down Expand Up @@ -34,27 +34,36 @@ public class BlobStorage : IStorage
private string _containerUrl;

/// <summary>
/// Default constructor.
/// Creates a new Blog Storage service from the given credentials.
/// </summary>
public BlobStorage(StorageCredentials credentials, string containerName = "uploads")
public BlobStorage(StorageCredentials credentials, string containerName = "uploads")
{
_storage = new CloudStorageAccount(credentials, true);
_containerName = containerName;
}

/// <summary>
/// Creates a new Blob Storage service from the given connection string.
/// </summary>
public BlobStorage(string connectionString, string containerName = "uploads")
{
_storage = CloudStorageAccount.Parse(connectionString);
_containerName = containerName;
}

/// <summary>
/// Opens a new storage session.
/// </summary>
/// <returns>A new open session</returns>
public async Task<IStorageSession> OpenAsync()
public async Task<IStorageSession> OpenAsync()
{
var session = _storage.CreateCloudBlobClient();
var container = session.GetContainerReference(_containerName);

if (!await container.ExistsAsync())
if (!await container.ExistsAsync())
{
await container.CreateAsync();
await container.SetPermissionsAsync(new BlobContainerPermissions()
await container.SetPermissionsAsync(new BlobContainerPermissions()
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
Expand All @@ -69,16 +78,16 @@ await container.SetPermissionsAsync(new BlobContainerPermissions()
/// </summary>
/// <param name="id">The media resource id</param>
/// <returns>The public url</returns>
public string GetPublicUrl(string id)
public string GetPublicUrl(string id)
{
if (!string.IsNullOrWhiteSpace(id))
if (!string.IsNullOrWhiteSpace(id))
{
if (string.IsNullOrEmpty(_containerUrl))
if (string.IsNullOrEmpty(_containerUrl))
{
var session = _storage.CreateCloudBlobClient();
var container = session.GetContainerReference(_containerName);

_containerUrl = container.Uri.AbsoluteUri;
_containerUrl = container.Uri.AbsoluteUri;
}
return _containerUrl + "/" + id;
}
Expand Down
20 changes: 18 additions & 2 deletions core/Piranha.Azure.BlobStorage/BlobStorageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*
*
* https://github.com/piranhacms/piranha.core
*
*
*/

using Microsoft.Extensions.DependencyInjection;
Expand All @@ -30,4 +30,20 @@ public static IServiceCollection AddPiranhaBlobStorage(this IServiceCollection s

return services;
}

/// <summary>
/// Adds the services for the Azure BlobStorage service.
/// </summary>
/// <param name="services">The current service collection</param>
/// <param name="connectionString">The connection string</param>
/// <param name="containerName">The optional container name</param>
/// <param name="scope">The optional service scope. Default is singleton</param>
/// <returns>The service collection</returns>
public static IServiceCollection AddPiranhaBlobStorage(this IServiceCollection services,
string connectionString, string containerName = "uploads", ServiceLifetime scope = ServiceLifetime.Singleton)
{
services.Add(new ServiceDescriptor(typeof(IStorage), sp => new BlobStorage(connectionString, containerName), scope));

return services;
}
}

0 comments on commit 1222c1b

Please sign in to comment.