-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathObjectStorageExample.cs
More file actions
110 lines (96 loc) · 3.78 KB
/
ObjectStorageExample.cs
File metadata and controls
110 lines (96 loc) · 3.78 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
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
*/
using System;
using System.IO;
using System.Threading.Tasks;
using Oci.Common;
using Oci.Common.Auth;
using Oci.ObjectstorageService;
using Oci.ObjectstorageService.Requests;
using Oci.ObjectstorageService.Responses;
namespace Oci.Examples
{
public class ObjectStorageExamples
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public static async Task MainOS()
{
logger.Info("Starting Object Storage Examples");
var provider = new ConfigFileAuthenticationDetailsProvider("DEFAULT");
var osClient = new ObjectStorageClient(provider, new ClientConfiguration());
try
{
await PutObjectExample(osClient);
await GetObjectExample(osClient);
}
catch (Exception e)
{
logger.Error($"Failed ObjectStorage example: {e.Message}");
}
finally
{
osClient.Dispose();
}
}
public static async Task<PutObjectResponse> PutObjectExample(ObjectStorageClient osClient)
{
var putObjectRequest = new PutObjectRequest()
{
BucketName = Environment.GetEnvironmentVariable("BUCKET_NAME"),
NamespaceName = Environment.GetEnvironmentVariable("NAMESPACE_NAME"),
ObjectName = Environment.GetEnvironmentVariable("OBJECT_NAME"),
PutObjectBody = GenerateStreamFromString("Hello World!!!!")
};
try
{
var response = await osClient.PutObject(putObjectRequest);
logger.Info($"Put Object is successful: " + response.ETag);
return response;
}
catch (Exception e)
{
logger.Error($"Failed at PutObjectExample:\n{e}");
throw;
}
}
public static async Task<GetObjectResponse> GetObjectExample(ObjectStorageClient osClient)
{
var getObjectObjectRequest = new GetObjectRequest()
{
BucketName = Environment.GetEnvironmentVariable("BUCKET_NAME"),
NamespaceName = Environment.GetEnvironmentVariable("NAMESPACE_NAME"),
ObjectName = Environment.GetEnvironmentVariable("OBJECT_NAME"),
Range = new Common.Model.Range()
{
StartByte = 2,
EndByte = 10
}
};
try
{
var response = await osClient.GetObject(getObjectObjectRequest);
logger.Info($"Get Object is successful: " + response.ETag);
var fileContents = new StreamReader(response.InputStream).ReadToEnd();
logger.Info($"file contents: {fileContents}");
logger.Info($"Range: Start Index: {response.ContentRange.StartByte}, End Index: {response.ContentRange.EndByte}");
return response;
}
catch (Exception e)
{
logger.Error($"Failed at GetObjectExample:\n{e}");
throw;
}
}
public static Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
}
}