Skip to content

Commit adf6d08

Browse files
committed
Add examples for IObjectStorageProvider.ListObject (fixes #323)
1 parent 3ae981e commit adf6d08

10 files changed

Lines changed: 188 additions & 0 deletions

src/Samples/CPPCodeSamples/CPPCodeSamples.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
</ItemGroup>
8686
<ItemGroup>
8787
<ClCompile Include="AssemblyInfo.cpp" />
88+
<ClCompile Include="ObjectStorageProviderExamples.cpp" />
8889
<ClCompile Include="QueueingServiceExamples.cpp" />
8990
<ClCompile Include="Stdafx.cpp">
9091
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>

src/Samples/CPPCodeSamples/CPPCodeSamples.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
<ClCompile Include="QueueingServiceExamples.cpp">
3333
<Filter>Source Files</Filter>
3434
</ClCompile>
35+
<ClCompile Include="ObjectStorageProviderExamples.cpp">
36+
<Filter>Source Files</Filter>
37+
</ClCompile>
3538
</ItemGroup>
3639
<ItemGroup>
3740
<Text Include="ReadMe.txt" />
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "Stdafx.h"
2+
3+
using namespace net::openstack::Core;
4+
using namespace net::openstack::Core::Domain;
5+
using namespace net::openstack::Core::Providers;
6+
using namespace System;
7+
using namespace System::Collections::Generic;
8+
9+
ref class ObjectStorageProviderExamples
10+
{
11+
public:
12+
#pragma region ListObjectsInContainer
13+
void ListObjects(IObjectStorageProvider^ provider, String^ containerName)
14+
{
15+
Console::WriteLine("Objects in container {0}", containerName);
16+
for each (ContainerObject^ containerObject in ListAllObjects(provider, containerName))
17+
Console::WriteLine(" {0}", containerObject->Name);
18+
}
19+
20+
static IEnumerable<ContainerObject^>^ ListAllObjects(IObjectStorageProvider^ provider, String^ containerName)
21+
{
22+
return ListAllObjects(provider, containerName, Nullable<int>(), nullptr, nullptr, false, nullptr);
23+
}
24+
25+
static IEnumerable<ContainerObject^>^ ListAllObjects(
26+
IObjectStorageProvider^ provider,
27+
String^ containerName,
28+
Nullable<int> blockSize,
29+
String^ prefix,
30+
String^ region,
31+
bool useInternalUrl,
32+
CloudIdentity^ identity)
33+
{
34+
if (blockSize.HasValue && blockSize.Value <= 0)
35+
throw gcnew ArgumentOutOfRangeException("blockSize");
36+
37+
List<ContainerObject^>^ result = gcnew List<ContainerObject^>();
38+
ContainerObject^ lastContainerObject = nullptr;
39+
do
40+
{
41+
String^ marker = lastContainerObject ? lastContainerObject->Name : nullptr;
42+
IEnumerable<ContainerObject^>^ containerObjects =
43+
provider->ListObjects(containerName, blockSize, marker, nullptr, prefix, region, useInternalUrl, identity);
44+
int previousCount = result->Count;
45+
result->AddRange(containerObjects);
46+
if (result->Count > previousCount)
47+
lastContainerObject = result[result->Count - 1];
48+
else
49+
lastContainerObject = nullptr;
50+
} while (lastContainerObject);
51+
52+
return result;
53+
}
54+
#pragma endregion
55+
};

src/Samples/CSharpCodeSamples/CSharpCodeSamples.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<Reference Include="System.Xml" />
4343
</ItemGroup>
4444
<ItemGroup>
45+
<Compile Include="ObjectStorageProviderExamples.cs" />
4546
<Compile Include="Properties\AssemblyInfo.cs" />
4647
<Compile Include="QueueingServiceExamples.cs" />
4748
</ItemGroup>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace CSharpCodeSamples
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using net.openstack.Core.Domain;
6+
using net.openstack.Core.Providers;
7+
8+
public class ObjectStorageProviderExamples
9+
{
10+
#region ListObjectsInContainer
11+
public void ListObjects(IObjectStorageProvider provider, string containerName)
12+
{
13+
Console.WriteLine("Objects in container {0}", containerName);
14+
foreach (ContainerObject containerObject in ListAllObjects(provider, containerName))
15+
Console.WriteLine(" {0}", containerObject.Name);
16+
}
17+
18+
private static IEnumerable<ContainerObject> ListAllObjects(
19+
IObjectStorageProvider provider,
20+
string containerName,
21+
int? blockSize = null,
22+
string prefix = null,
23+
string region = null,
24+
bool useInternalUrl = false,
25+
CloudIdentity identity = null)
26+
{
27+
if (blockSize <= 0)
28+
throw new ArgumentOutOfRangeException("blockSize");
29+
30+
ContainerObject lastContainerObject = null;
31+
32+
do
33+
{
34+
string marker = lastContainerObject != null ? lastContainerObject.Name : null;
35+
IEnumerable<ContainerObject> containerObjects =
36+
provider.ListObjects(containerName, blockSize, marker, null, prefix, region, useInternalUrl, identity);
37+
lastContainerObject = null;
38+
foreach (ContainerObject containerObject in containerObjects)
39+
{
40+
lastContainerObject = containerObject;
41+
yield return containerObject;
42+
}
43+
} while (lastContainerObject != null);
44+
}
45+
#endregion ListObjectsInContainer
46+
}
47+
}

src/Samples/FSharpCodeSamples/FSharpCodeSamples.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
</PropertyGroup>
5454
<ItemGroup>
5555
<Compile Include="QueueingServiceExamples.fs" />
56+
<Compile Include="ObjectStorageProviderExamples.fs" />
5657
</ItemGroup>
5758
<Choose>
5859
<When Condition="'$(VisualStudioVersion)' == '11.0'">
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module ObjectStorageProviderExamples
2+
3+
open System;
4+
open System.Collections.Generic;
5+
open net.openstack.Core.Domain;
6+
open net.openstack.Core.Providers;
7+
8+
//#region ListObjectsInContainer
9+
let listAllObjects(provider : IObjectStorageProvider, containerName : string) =
10+
seq {
11+
let lastContainerObject : ContainerObject ref = { contents = null }
12+
let finished : bool ref = { contents = false }
13+
while not !finished do
14+
let marker = if !lastContainerObject <> null then (!lastContainerObject).Name else null
15+
let containerObjects = provider.ListObjects(containerName, marker= marker)
16+
lastContainerObject := null
17+
for containerObject in containerObjects do
18+
lastContainerObject := containerObject
19+
yield containerObject
20+
if !lastContainerObject = null then
21+
finished := true
22+
}
23+
24+
let listObjects(provider : IObjectStorageProvider, containerName : string) =
25+
Console.WriteLine("Objects in container {0}", containerName)
26+
for containerObject in listAllObjects(provider, containerName) do
27+
Console.WriteLine(" {0}", containerObject.Name)
28+
()
29+
//#endregion
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Imports net.openstack.Core.Providers
2+
Imports net.openstack.Core.Domain
3+
4+
Public Class ObjectStorageProviderExamples
5+
6+
#Region "ListObjectsInContainer"
7+
Public Sub ListObjects(provider As IObjectStorageProvider, containerName As String)
8+
Console.WriteLine("Objects in container {0}", containerName)
9+
For Each containerObject In ListAllObjects(provider, containerName)
10+
Console.WriteLine(" {0}", containerObject.Name)
11+
Next
12+
End Sub
13+
14+
Private Shared Iterator Function ListAllObjects(
15+
provider As IObjectStorageProvider,
16+
containerName As String,
17+
Optional blockSize As Nullable(Of Integer) = Nothing,
18+
Optional prefix As String = Nothing,
19+
Optional region As String = Nothing,
20+
Optional useInternalUrl As Boolean = False,
21+
Optional identity As CloudIdentity = Nothing) As IEnumerable(Of ContainerObject)
22+
23+
If blockSize <= 0 Then
24+
Throw New ArgumentOutOfRangeException("blockSize")
25+
End If
26+
27+
Dim lastContainerObject As ContainerObject = Nothing
28+
Do
29+
Dim marker = If(lastContainerObject IsNot Nothing, lastContainerObject.Name, Nothing)
30+
Dim containerObjects = provider.ListObjects(containerName, blockSize, marker, Nothing, prefix, region, useInternalUrl, identity)
31+
lastContainerObject = Nothing
32+
For Each containerObject In containerObjects
33+
lastContainerObject = containerObject
34+
Yield containerObject
35+
Next
36+
Loop While lastContainerObject IsNot Nothing
37+
End Function
38+
#End Region
39+
40+
41+
End Class

src/Samples/VBCodeSamples/VBCodeSamples.vbproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<DependentUpon>Settings.settings</DependentUpon>
8080
<DesignTimeSharedInput>True</DesignTimeSharedInput>
8181
</Compile>
82+
<Compile Include="ObjectStorageProviderExamples.vb" />
8283
<Compile Include="QueueingServiceExamples.vb" />
8384
</ItemGroup>
8485
<ItemGroup>

src/corelib/Core/Providers/IObjectStorageProvider.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,15 @@ public interface IObjectStorageProvider
10381038
/// <para>If <paramref name="region"/> is <see langword="null"/> and no default region is available for the provider.</para>
10391039
/// </exception>
10401040
/// <exception cref="ResponseException">If the REST API request failed.</exception>
1041+
/// <example>
1042+
/// <para>The following example demonstrates the use of this method to output the names of all objects in a container
1043+
/// using <see cref="Console.WriteLine(string, object[])"/>. In the example, the pagination details of this method are handled by the
1044+
/// helper method <c>ListAllObjects</c>.</para>
1045+
/// <code source="..\Samples\CSharpCodeSamples\ObjectStorageProviderExamples.cs" region="ListObjectsInContainer" language="cs"/>
1046+
/// <code source="..\Samples\VBCodeSamples\ObjectStorageProviderExamples.vb" region="ListObjectsInContainer" language="vbnet"/>
1047+
/// <code source="..\Samples\CPPCodeSamples\ObjectStorageProviderExamples.cpp" region="ListObjectsInContainer" language="cpp"/>
1048+
/// <code source="..\Samples\FSharpCodeSamples\ObjectStorageProviderExamples.fs" region="ListObjectsInContainer" language="fs"/>
1049+
/// </example>
10411050
/// <seealso href="http://docs.openstack.org/api/openstack-object-storage/1.0/content/list-objects.html">List Objects (OpenStack Object Storage API v1 Reference)</seealso>
10421051
IEnumerable<ContainerObject> ListObjects(string container, int? limit = null, string marker = null, string markerEnd = null, string prefix = null, string region = null, bool useInternalUrl = false, CloudIdentity identity = null);
10431052

0 commit comments

Comments
 (0)