Skip to content

Commit 42e15bc

Browse files
committed
Add headers parameter to CreateContainer (fixes #167); add TestVersionedContainer
1 parent dc6384d commit 42e15bc

File tree

3 files changed

+79
-5
lines changed

3 files changed

+79
-5
lines changed

src/corelib/Core/Providers/IObjectStorageProvider.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public interface IObjectStorageProvider
4848
/// Creates a container if it does not already exist.
4949
/// </summary>
5050
/// <param name="container">The container name.</param>
51+
/// <param name="headers">A collection of custom HTTP headers to associate with the container (see <see cref="GetContainerHeader"/>).</param>
5152
/// <param name="region">The region in which to execute this action. If not specified, the user's default region will be used.</param>
5253
/// <param name="useInternalUrl"><c>true</c> to use the endpoint's <see cref="Endpoint.InternalURL"/>; otherwise <c>false</c> to use the endpoint's <see cref="Endpoint.PublicURL"/>.</param>
5354
/// <param name="identity">The cloud identity to use for this request. If not specified, the default identity for the current provider instance will be used.</param>
@@ -60,7 +61,11 @@ public interface IObjectStorageProvider
6061
/// </list>
6162
/// </returns>
6263
/// <exception cref="ArgumentNullException">If <paramref name="container"/> is <c>null</c>.</exception>
63-
/// <exception cref="ArgumentException">If <paramref name="container"/> is empty.</exception>
64+
/// <exception cref="ArgumentException">
65+
/// If <paramref name="container"/> is empty.
66+
/// <para>-or-</para>
67+
/// <para>If <paramref name="headers"/> contains two equivalent keys when compared using <see cref="StringComparer.OrdinalIgnoreCase"/>.</para>
68+
/// </exception>
6469
/// <exception cref="ContainerNameException">If <paramref name="container"/> is not a valid container name.</exception>
6570
/// <exception cref="NotSupportedException">
6671
/// If the provider does not support the given <paramref name="identity"/> type.
@@ -76,7 +81,7 @@ public interface IObjectStorageProvider
7681
/// </exception>
7782
/// <exception cref="ResponseException">If the REST API request failed.</exception>
7883
/// <seealso href="http://docs.openstack.org/api/openstack-object-storage/1.0/content/create-container.html">Create Container (OpenStack Object Storage API v1 Reference)</seealso>
79-
ObjectStore CreateContainer(string container, string region = null, bool useInternalUrl = false, CloudIdentity identity = null);
84+
ObjectStore CreateContainer(string container, Dictionary<string, string> headers = null, string region = null, bool useInternalUrl = false, CloudIdentity identity = null);
8085

8186
/// <summary>
8287
/// Deletes a container, and optionally all objects stored in the container.

src/corelib/Providers/Rackspace/CloudFilesProvider.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,10 @@ public IEnumerable<Container> ListContainers(int? limit = null, string marker =
234234
return null;
235235

236236
return response.Data;
237-
238237
}
239238

240239
/// <inheritdoc />
241-
public ObjectStore CreateContainer(string container, string region = null, bool useInternalUrl = false, CloudIdentity identity = null)
240+
public ObjectStore CreateContainer(string container, Dictionary<string, string> headers = null, string region = null, bool useInternalUrl = false, CloudIdentity identity = null)
242241
{
243242
if (container == null)
244243
throw new ArgumentNullException("container");
@@ -249,7 +248,7 @@ public ObjectStore CreateContainer(string container, string region = null, bool
249248
_cloudFilesValidator.ValidateContainerName(container);
250249
var urlPath = new Uri(string.Format("{0}/{1}", GetServiceEndpointCloudFiles(identity, region, useInternalUrl), _encodeDecodeProvider.UrlEncode(container)));
251250

252-
var response = ExecuteRESTRequest(identity, urlPath, HttpMethod.PUT);
251+
var response = ExecuteRESTRequest(identity, urlPath, HttpMethod.PUT, headers: headers);
253252

254253
switch (response.StatusCode)
255254
{

src/testing/integration/Providers/Rackspace/UserObjectStorageTests.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,76 @@ public void TestCreateContainer()
247247
provider.DeleteContainer(containerName, deleteObjects: true);
248248
}
249249

250+
[TestMethod]
251+
[TestCategory(TestCategories.User)]
252+
[TestCategory(TestCategories.ObjectStorage)]
253+
public void TestVersionedContainer()
254+
{
255+
IObjectStorageProvider provider = new CloudFilesProvider(Bootstrapper.Settings.TestIdentity);
256+
string containerName = TestContainerPrefix + Path.GetRandomFileName();
257+
string versionsContainerName = TestContainerPrefix + Path.GetRandomFileName();
258+
259+
ObjectStore result = provider.CreateContainer(versionsContainerName);
260+
Assert.AreEqual(ObjectStore.ContainerCreated, result);
261+
262+
result = provider.CreateContainer(containerName, new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) { { CloudFilesProvider.VersionsLocation, versionsContainerName } });
263+
Assert.AreEqual(ObjectStore.ContainerCreated, result);
264+
265+
Dictionary<string, string> headers = provider.GetContainerHeader(containerName);
266+
string location;
267+
Assert.IsTrue(headers.TryGetValue(CloudFilesProvider.VersionsLocation, out location));
268+
Assert.AreEqual(versionsContainerName, location);
269+
270+
string objectName = Path.GetRandomFileName();
271+
string fileData1 = "first-content";
272+
string fileData2 = "second-content";
273+
274+
using (MemoryStream uploadStream = new MemoryStream(Encoding.UTF8.GetBytes(fileData1)))
275+
{
276+
provider.CreateObject(containerName, uploadStream, objectName);
277+
}
278+
279+
using (MemoryStream downloadStream = new MemoryStream())
280+
{
281+
provider.GetObject(containerName, objectName, downloadStream);
282+
283+
downloadStream.Position = 0;
284+
StreamReader reader = new StreamReader(downloadStream, Encoding.UTF8);
285+
string actualData = reader.ReadToEnd();
286+
Assert.AreEqual(fileData1, actualData);
287+
}
288+
289+
using (MemoryStream uploadStream = new MemoryStream(Encoding.UTF8.GetBytes(fileData2)))
290+
{
291+
provider.CreateObject(containerName, uploadStream, objectName);
292+
}
293+
294+
using (MemoryStream downloadStream = new MemoryStream())
295+
{
296+
provider.GetObject(containerName, objectName, downloadStream);
297+
298+
downloadStream.Position = 0;
299+
StreamReader reader = new StreamReader(downloadStream, Encoding.UTF8);
300+
string actualData = reader.ReadToEnd();
301+
Assert.AreEqual(fileData2, actualData);
302+
}
303+
304+
provider.DeleteObject(containerName, objectName);
305+
306+
using (MemoryStream downloadStream = new MemoryStream())
307+
{
308+
provider.GetObject(containerName, objectName, downloadStream);
309+
310+
downloadStream.Position = 0;
311+
StreamReader reader = new StreamReader(downloadStream, Encoding.UTF8);
312+
string actualData = reader.ReadToEnd();
313+
Assert.AreEqual(fileData1, actualData);
314+
}
315+
316+
provider.DeleteContainer(versionsContainerName, deleteObjects: true);
317+
provider.DeleteContainer(containerName, deleteObjects: true);
318+
}
319+
250320
[TestMethod]
251321
[TestCategory(TestCategories.User)]
252322
[TestCategory(TestCategories.ObjectStorage)]

0 commit comments

Comments
 (0)