forked from openstacknetsdk/openstack.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectStorageProviderExamples.cs
More file actions
97 lines (89 loc) · 3.87 KB
/
Copy pathObjectStorageProviderExamples.cs
File metadata and controls
97 lines (89 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
namespace CSharpCodeSamples
{
using System;
using System.Collections.Generic;
using net.openstack.Core.Domain;
using net.openstack.Core.Providers;
using net.openstack.Providers.Rackspace;
using Stream = System.IO.Stream;
public class ObjectStorageProviderExamples
{
#region ListObjectsInContainer
public void ListObjects(IObjectStorageProvider provider, string containerName)
{
Console.WriteLine("Objects in container {0}", containerName);
foreach (ContainerObject containerObject in ListAllObjects(provider, containerName))
Console.WriteLine(" {0}", containerObject.Name);
}
private static IEnumerable<ContainerObject> ListAllObjects(
IObjectStorageProvider provider,
string containerName,
int? blockSize = null,
string prefix = null,
string region = null,
bool useInternalUrl = false,
CloudIdentity identity = null)
{
if (blockSize <= 0)
throw new ArgumentOutOfRangeException("blockSize");
ContainerObject lastContainerObject = null;
do
{
string marker = lastContainerObject != null ? lastContainerObject.Name : null;
IEnumerable<ContainerObject> containerObjects =
provider.ListObjects(containerName, blockSize, marker, null, prefix, region, useInternalUrl, identity);
lastContainerObject = null;
foreach (ContainerObject containerObject in containerObjects)
{
lastContainerObject = containerObject;
yield return containerObject;
}
} while (lastContainerObject != null);
}
#endregion ListObjectsInContainer
#region CreateObjectFromFileWithMetadata
public void CreateObjectWithMetadata(
IObjectStorageProvider provider,
string containerName,
string objectName,
string filePath)
{
// Method 1: Set metadata when the object is created
var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ CloudFilesProvider.ObjectMetaDataPrefix + "Key", "Value" }
};
provider.CreateObjectFromFile(containerName, filePath, objectName, headers: headers);
// Method 2: Set metadata in a separate call after the object is created
provider.CreateObjectFromFile(containerName, filePath, objectName);
var metadata = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "Key", "Value" }
};
provider.UpdateObjectMetadata(containerName, objectName, metadata);
}
#endregion CreateObjectWithMetadata
#region CreateObjectWithMetadata
public void CreateObjectWithMetadata(
IObjectStorageProvider provider,
string containerName,
string objectName,
Stream data)
{
// Method 1: Set metadata when the object is created
var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ CloudFilesProvider.ObjectMetaDataPrefix + "Key", "Value" }
};
provider.CreateObject(containerName, data, objectName, headers: headers);
// Method 2: Set metadata in a separate call after the object is created
provider.CreateObject(containerName, data, objectName);
var metadata = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "Key", "Value" }
};
provider.UpdateObjectMetadata(containerName, objectName, metadata);
}
#endregion CreateObjectWithMetadata
}
}