mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Prevent Azure blob storage from breaking an instance if not yet configured
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using Microsoft.WindowsAzure;
|
|
||||||
using Orchard.Environment.Configuration;
|
using Orchard.Environment.Configuration;
|
||||||
using Orchard.Environment.Extensions;
|
using Orchard.Environment.Extensions;
|
||||||
using Orchard.FileSystems.Media;
|
using Orchard.FileSystems.Media;
|
||||||
@@ -7,11 +6,8 @@ using Orchard.FileSystems.Media;
|
|||||||
namespace Orchard.AzureBlobStorage.Services {
|
namespace Orchard.AzureBlobStorage.Services {
|
||||||
[OrchardSuppressDependency("Orchard.FileSystems.Media.FileSystemStorageProvider")]
|
[OrchardSuppressDependency("Orchard.FileSystems.Media.FileSystemStorageProvider")]
|
||||||
public class AzureBlobStorageProvider : AzureFileSystem, IStorageProvider {
|
public class AzureBlobStorageProvider : AzureFileSystem, IStorageProvider {
|
||||||
public AzureBlobStorageProvider(ShellSettings shellSettings, IMimeTypeProvider mimeTypeProvider)
|
|
||||||
: this(shellSettings, CloudConfigurationManager.GetSetting("StorageConnectionString"), mimeTypeProvider) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public AzureBlobStorageProvider(ShellSettings shellSettings, string storageConnectionString, IMimeTypeProvider mimeTypeProvider) : base("media", shellSettings.Name, false, storageConnectionString, mimeTypeProvider) { }
|
public AzureBlobStorageProvider(ShellSettings shellSettings, IMimeTypeProvider mimeTypeProvider) : base("media", shellSettings.Name, false, mimeTypeProvider) { }
|
||||||
|
|
||||||
public bool TrySaveStream(string path, Stream inputStream) {
|
public bool TrySaveStream(string path, Stream inputStream) {
|
||||||
try {
|
try {
|
||||||
|
@@ -2,42 +2,68 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Microsoft.WindowsAzure;
|
||||||
using Microsoft.WindowsAzure.Storage;
|
using Microsoft.WindowsAzure.Storage;
|
||||||
using Microsoft.WindowsAzure.Storage.Blob;
|
using Microsoft.WindowsAzure.Storage.Blob;
|
||||||
using Orchard.FileSystems.Media;
|
using Orchard.FileSystems.Media;
|
||||||
|
|
||||||
namespace Orchard.AzureBlobStorage.Services {
|
namespace Orchard.AzureBlobStorage.Services {
|
||||||
public class AzureFileSystem {
|
public class AzureFileSystem {
|
||||||
private readonly IMimeTypeProvider _mimeTypeProvider;
|
|
||||||
public const string FolderEntry = "$$$ORCHARD$$$.$$$";
|
public const string FolderEntry = "$$$ORCHARD$$$.$$$";
|
||||||
|
|
||||||
|
private readonly bool _isPrivate;
|
||||||
|
private readonly IMimeTypeProvider _mimeTypeProvider;
|
||||||
|
|
||||||
|
protected string _root;
|
||||||
|
protected string _absoluteRoot;
|
||||||
|
|
||||||
|
private CloudStorageAccount _storageAccount;
|
||||||
|
private CloudBlobClient _blobClient;
|
||||||
|
private CloudBlobContainer _container;
|
||||||
|
|
||||||
public string ContainerName { get; protected set; }
|
public string ContainerName { get; protected set; }
|
||||||
|
|
||||||
private readonly CloudStorageAccount _storageAccount;
|
public CloudBlobClient BlobClient {
|
||||||
protected readonly string _root;
|
get {
|
||||||
protected readonly string _absoluteRoot;
|
EnsureInitialized();
|
||||||
public CloudBlobClient BlobClient { get; private set; }
|
return _blobClient;
|
||||||
public CloudBlobContainer Container { get; private set; }
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public AzureFileSystem(string containerName, string root, bool isPrivate, string storageConnectionString, IMimeTypeProvider mimeTypeProvider) {
|
public CloudBlobContainer Container {
|
||||||
|
get {
|
||||||
|
EnsureInitialized();
|
||||||
|
return _container;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public AzureFileSystem(string containerName, string root, bool isPrivate, IMimeTypeProvider mimeTypeProvider) {
|
||||||
|
_isPrivate = isPrivate;
|
||||||
_mimeTypeProvider = mimeTypeProvider;
|
_mimeTypeProvider = mimeTypeProvider;
|
||||||
// Setup the connection to custom storage accountm, e.g. Development Storage
|
|
||||||
_storageAccount = CloudStorageAccount.Parse(storageConnectionString);
|
|
||||||
ContainerName = containerName;
|
ContainerName = containerName;
|
||||||
_root = String.IsNullOrEmpty(root) ? "" : root + "/";
|
_root = String.IsNullOrEmpty(root) ? "" : root + "/";
|
||||||
_absoluteRoot = Combine(Combine(_storageAccount.BlobEndpoint.AbsoluteUri, containerName), root);
|
}
|
||||||
|
|
||||||
BlobClient = _storageAccount.CreateCloudBlobClient();
|
private void EnsureInitialized() {
|
||||||
|
if (_storageAccount != null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
_storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
|
||||||
|
_absoluteRoot = Combine(Combine(_storageAccount.BlobEndpoint.AbsoluteUri, ContainerName), _root);
|
||||||
|
|
||||||
|
_blobClient = _storageAccount.CreateCloudBlobClient();
|
||||||
// Get and create the container if it does not exist
|
// Get and create the container if it does not exist
|
||||||
// The container is named with DNS naming restrictions (i.e. all lower case)
|
// The container is named with DNS naming restrictions (i.e. all lower case)
|
||||||
Container = BlobClient.GetContainerReference(ContainerName);
|
_container = _blobClient.GetContainerReference(ContainerName);
|
||||||
|
|
||||||
Container.CreateIfNotExists();
|
_container.CreateIfNotExists();
|
||||||
|
|
||||||
Container.SetPermissions(isPrivate
|
_container.SetPermissions(_isPrivate
|
||||||
? new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Off }
|
? new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Off }
|
||||||
: new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
|
: new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string ConvertToRelativeUriPath(string path) {
|
private static string ConvertToRelativeUriPath(string path) {
|
||||||
|
Reference in New Issue
Block a user