Refactored AzureBlobStorage to remove isPrivate

Added test on reading/writing content

--HG--
branch : dev
This commit is contained in:
Sebastien Ros
2010-04-26 11:02:10 -07:00
parent 9efcf65058
commit 97f8ed294e
2 changed files with 26 additions and 15 deletions

View File

@@ -7,6 +7,7 @@ using Orchard.Azure.Storage;
using Microsoft.WindowsAzure; using Microsoft.WindowsAzure;
using System.Linq; using System.Linq;
using Microsoft.WindowsAzure.StorageClient; using Microsoft.WindowsAzure.StorageClient;
using System.Text;
namespace Orchard.Azure.Tests.Storage { namespace Orchard.Azure.Tests.Storage {
[TestFixture] [TestFixture]
@@ -36,7 +37,7 @@ namespace Orchard.Azure.Tests.Storage {
CloudStorageAccount devAccount; CloudStorageAccount devAccount;
CloudStorageAccount.TryParse("UseDevelopmentStorage=true", out devAccount); CloudStorageAccount.TryParse("UseDevelopmentStorage=true", out devAccount);
_azureBlobStorageProvider = new AzureBlobStorageProvider("default", false, devAccount); _azureBlobStorageProvider = new AzureBlobStorageProvider("default", devAccount);
} }
[TestFixtureTearDown] [TestFixtureTearDown]
@@ -186,5 +187,25 @@ namespace Orchard.Azure.Tests.Storage {
Assert.AreEqual(1, _azureBlobStorageProvider.ListFiles("folder1/folder4/folder3").Count()); 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);
}
} }
} }

View File

@@ -16,34 +16,24 @@ namespace Orchard.Azure.Storage
public CloudBlobClient BlobClient { get; private set; } public CloudBlobClient BlobClient { get; private set; }
public CloudBlobContainer Container { 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 // Setup the connection to custom storage accountm, e.g. Development Storage
_storageAccount = storageAccount; _storageAccount = storageAccount;
InitBlobClient(containerName, isPrivate);
}
private void InitBlobClient(string containerName, bool isPrivate)
{
BlobClient = _storageAccount.CreateCloudBlobClient(); 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);
// Setup the permissions if the container is new Container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
if (Container.CreateIfNotExist()) {
if (isPrivate)
Container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Off });
else
Container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
}
} }
private void EnsurePathIsRelative(string path) { private static void EnsurePathIsRelative(string path) {
if(path.StartsWith("/")) if(path.StartsWith("/"))
throw new ArgumentException("Path must be relative"); throw new ArgumentException("Path must be relative");
} }