Created
August 25, 2020 06:47
-
-
Save viralmodi/12e936b1f418742a8a9f75082e18c62a to your computer and use it in GitHub Desktop.
Pushing a web image to Oracle Cloud Object Storage service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Oci.Common; | |
using Oci.Common.Auth; | |
using Oci.ObjectstorageService; | |
using Oci.ObjectstorageService.Models; | |
using Oci.ObjectstorageService.Requests; | |
using Oci.ObjectstorageService.Responses; | |
using System; | |
using System.IO; | |
using System.Net; | |
using System.Threading.Tasks; | |
namespace TestConsoleApp | |
{ | |
class Program | |
{ | |
/* logger used in the code for logging on console */ | |
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); | |
/* name of the oracle cloud object storage service bucket */ | |
private static string bucketName; | |
/* name of the object to be stored in the bucket of Oracle Cloud Object Storage service */ | |
private static string objectName; | |
/* variable to store Object Storage Service Namespace - an uneditable and unique Oracle Cloud system-generated string */ | |
private static string ns; | |
/* A sample Oracle logo image that we will upload to Object Storage */ | |
private static string imageUrl = "https://www.oracle.com/a/ocom/img/social-og-oracle-badge.jpg"; | |
static async Task Main(string[] args) | |
{ | |
/* get bucket name and object name from Environment variables */ | |
bucketName = Environment.GetEnvironmentVariable("BUCKET_NAME"); | |
objectName = Environment.GetEnvironmentVariable("OBJECT_NAME"); | |
logger.Info("Storing " + objectName + " in " + bucketName + " on Oracle Cloud"); | |
/* Create an authentication provider for Oracle Cloud and use the DEFAULT profile from SDK config. | |
* This profile is used to sign requests and authenticate with Oracle Cloud | |
*/ | |
var provider = new ConfigFileAuthenticationDetailsProvider("DEFAULT"); | |
/* Create an object storage client which could be used to make calls to Object Storage service */ | |
var osClient = new ObjectStorageClient(provider, new ClientConfiguration()); | |
/* get the object storage namespace since it is required in most of Object Storage service calls */ | |
ns = await GetNamespaceAsync(osClient); | |
/* Create a bucket to which we will upload our image */ | |
var bucket = await CreateBucketAsync(osClient, provider.TenantId); | |
logger.Info("Bucket " + bucket.Name + " created in root compartment"); | |
/* Push an Oracle logo image from web to object storage bucket we created above */ | |
await PutObjectAsync(osClient); | |
logger.Info("Object " + objectName + " pushed to bucket " + bucket.Name); | |
} | |
public static async Task<String> GetNamespaceAsync(ObjectStorageClient osClient) | |
{ | |
var getNamespaceRequest = new GetNamespaceRequest(); | |
try | |
{ | |
var namespaceRsp = await osClient.GetNamespace(getNamespaceRequest); | |
logger.Info($"Get Namespace is successful"); | |
return namespaceRsp.Value; | |
} | |
catch (Exception e) | |
{ | |
logger.Error($"Failed at GetNamespaceAsync:\n{e}"); | |
throw; | |
} | |
} | |
public static async Task<Bucket> CreateBucketAsync(ObjectStorageClient osClient, string compartmentId) | |
{ | |
var createBucketDetails = new CreateBucketDetails | |
{ | |
Name = bucketName, | |
CompartmentId = compartmentId | |
}; | |
var createBucketRequest = new CreateBucketRequest | |
{ | |
CreateBucketDetails = createBucketDetails, | |
NamespaceName = ns | |
}; | |
try | |
{ | |
var createBucketRsp = await osClient.CreateBucket(createBucketRequest); | |
logger.Info($"Create Bucket is successful: " + createBucketRsp.ETag); | |
return createBucketRsp.Bucket; | |
} | |
catch (Exception e) | |
{ | |
logger.Error($"Failed at CreateBucketAsync:\n{e}"); | |
throw; | |
} | |
} | |
public static async Task<PutObjectResponse> PutObjectAsync(ObjectStorageClient osClient) | |
{ | |
var putObjectRequest = new PutObjectRequest() | |
{ | |
BucketName = bucketName, | |
NamespaceName = ns, | |
ObjectName = objectName, | |
PutObjectBody = GenerateStreamFromWebImage() | |
}; | |
try | |
{ | |
var putObjectRsp = await osClient.PutObject(putObjectRequest); | |
logger.Info($"Put Object is successful: " + putObjectRsp.ETag); | |
return putObjectRsp; | |
} | |
catch (Exception e) | |
{ | |
logger.Error($"Failed at PutObjectAsync:\n{e}"); | |
throw; | |
} | |
} | |
/** Download a web image and convert it into binary stream which is required by Object Storage PutObject call | |
*/ | |
public static Stream GenerateStreamFromWebImage() | |
{ | |
WebClient client = new WebClient(); | |
byte[] bytes = client.DownloadData(imageUrl); | |
MemoryStream stream = new MemoryStream(bytes); | |
return stream; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment