mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-04-05 21:01:35 +08:00
Refactored AzureBlobStorage to remove isPrivate
Added test on reading/writing content --HG-- branch : dev
This commit is contained in:
parent
9efcf65058
commit
97f8ed294e
@ -7,6 +7,7 @@ using Orchard.Azure.Storage;
|
||||
using Microsoft.WindowsAzure;
|
||||
using System.Linq;
|
||||
using Microsoft.WindowsAzure.StorageClient;
|
||||
using System.Text;
|
||||
|
||||
namespace Orchard.Azure.Tests.Storage {
|
||||
[TestFixture]
|
||||
@ -36,7 +37,7 @@ namespace Orchard.Azure.Tests.Storage {
|
||||
CloudStorageAccount devAccount;
|
||||
CloudStorageAccount.TryParse("UseDevelopmentStorage=true", out devAccount);
|
||||
|
||||
_azureBlobStorageProvider = new AzureBlobStorageProvider("default", false, devAccount);
|
||||
_azureBlobStorageProvider = new AzureBlobStorageProvider("default", devAccount);
|
||||
}
|
||||
|
||||
[TestFixtureTearDown]
|
||||
@ -186,5 +187,25 @@ namespace Orchard.Azure.Tests.Storage {
|
||||
Assert.AreEqual(1, _azureBlobStorageProvider.ListFiles("folder1/folder4/folder3").Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ShouldReadWriteFiles() {
|
||||
const string teststring = "This is a test string.";
|
||||
|
||||
var foo = _azureBlobStorageProvider.CreateFile("folder1/foo.txt");
|
||||
|
||||
using(var stream = foo.OpenWrite())
|
||||
using (var writer = new StreamWriter(stream))
|
||||
writer.Write(teststring);
|
||||
|
||||
Assert.AreEqual(22, foo.GetSize());
|
||||
|
||||
string content;
|
||||
using ( var stream = foo.OpenRead() )
|
||||
using ( var reader = new StreamReader(stream) ) {
|
||||
content = reader.ReadToEnd();
|
||||
}
|
||||
|
||||
Assert.AreEqual(teststring, content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,34 +16,24 @@ namespace Orchard.Azure.Storage
|
||||
public CloudBlobClient BlobClient { get; private set; }
|
||||
public CloudBlobContainer Container { get; private set; }
|
||||
|
||||
public AzureBlobStorageProvider(string containerName, bool isPrivate) : this(containerName, isPrivate, CloudStorageAccount.FromConfigurationSetting("DataConnectionString"))
|
||||
public AzureBlobStorageProvider(string containerName) : this(containerName, CloudStorageAccount.FromConfigurationSetting("DataConnectionString"))
|
||||
{
|
||||
}
|
||||
|
||||
public AzureBlobStorageProvider(string containerName, bool isPrivate, CloudStorageAccount storageAccount)
|
||||
public AzureBlobStorageProvider(string containerName, CloudStorageAccount storageAccount)
|
||||
{
|
||||
// Setup the connection to custom storage accountm, e.g. Development Storage
|
||||
_storageAccount = storageAccount;
|
||||
InitBlobClient(containerName, isPrivate);
|
||||
}
|
||||
|
||||
private void InitBlobClient(string containerName, bool isPrivate)
|
||||
{
|
||||
BlobClient = _storageAccount.CreateCloudBlobClient();
|
||||
// Get and create the container if it does not exist
|
||||
// The container is named with DNS naming restrictions (i.e. all lower case)
|
||||
Container = BlobClient.GetContainerReference(containerName);
|
||||
|
||||
// Setup the permissions if the container is new
|
||||
if (Container.CreateIfNotExist()) {
|
||||
if (isPrivate)
|
||||
Container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Off });
|
||||
else
|
||||
Container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
|
||||
}
|
||||
Container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
|
||||
}
|
||||
|
||||
private void EnsurePathIsRelative(string path) {
|
||||
private static void EnsurePathIsRelative(string path) {
|
||||
if(path.StartsWith("/"))
|
||||
throw new ArgumentException("Path must be relative");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user