Using Alibaba Cloud Object Storage Service In .NET Core

Alibaba is a cloud provider with a lot of market share in Asia, especially China.
 
Object Storage Services (OSS) is an encrypted and secure cloud storage service that can store, process, and access massive amounts of data from anywhere in the world. OSS Is the equivalent of Azure Blob Storage.
 
OSS is based on Buckets, every Bucket is like a container that allows you to insert a big amount of files with different extensions and formats.
 
First, you can create a new Bucket and start to store files. 
 
Using Alibaba cloud object storage service in .NET Core
 
In the overview section, you can see all the properties related to your bucket created, your versioning, Enable CORS, and Server-side Encryption for example.
 
Using Alibaba cloud object storage service in .NET Core
 
 
To use OSS with .NET Core we need to install the library first. 
 
Nuget package
 
 
To connect with the bucket you need 4 parameters,
  1. private readonly  string bucketName = "bucketName"; //bucket name in OSS
  2. private readonly string accessKeyId = "<yourAccessKeyId>";  // AccessKey from Alibaba Account
  3. private readonly string accessKeySecret = "<yourAccessKeySecret>";  // KeySecret from Alibaba Account
  4. private readonly string endpoint = "http://oss-cn-hangzhou.aliyuncs.com";  // endpoint from region where the OSS is hosted
Then, we need to create a OssClient to execute the command that we need it depending on our need. 
 
This is an example using PutObject to insert a new file in OSS,
  1. //class to transfer info  
  2. public class MyFile  
  3. {    
  4.    public string FileName {get;set;}     
  5.    public string ContentType {get;set;}    
  6.    public byte[] FileInfo {get;set;}    
  7. } 
  1. public async Task SaveFileAsync(MyFile file)  
  2. {  
  3.    // Create an OSSClient instance.  
  4.    var client = new OssClient(endpoint, accessKeyId, accessKeySecret);  
  5.   
  6.    client.PutObject(bucketName, file.FileName, new MemoryStream(file.FileInfo));  
  7. }  
We can use ListObjects to get all the files in the Bucket,
  1. // Create an OSSClient instance.  
  2. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);  
  3.   
  4. ObjectListing objects = client.ListObjects(bucketName);  
And we can use GetObject to get all the information about a specific file including the data in bytes,
  1. public async Task < MyFile > GetInfoFile(string fileName) {  
  2.     // Create an OSSClient instance.    
  3.     var client = new OssClient(endpoint, accessKeyId, accessKeySecret);  
  4.     var objectinfo = client.GetObject(bucketName, fileName);  
  5.     MemoryStream ms = new MemoryStream();  
  6.     await objectinfo.Content.CopyToAsync(ms);  
  7.     var myfile = new MyFile() {  
  8.         FileName = objectinfo.Key, FileInfo = ms.ToArray()  
  9.     };  
  10.     return myfile;  
  11. }   
Finally, this is an example including this method into an interface,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.Extensions.Configuration;  
  7. using shared;  
  8. using Aliyun.OSS;  
  9. using Aliyun.OSS.Common;  
  10. namespace api.Services {  
  11.     public class AlibabaCloudStorageService: IAlibabaCloudStorageService {  
  12.         private readonly IConfiguration Configuration;  
  13.         private readonly string bucketName = "mteheranst1";  
  14.         private readonly string accessKeyId = "<yourAccessKeyId>";  
  15.         private readonly string accessKeySecret = "<yourAccessKeySecret>";  
  16.         private readonly string endpoint = "http://oss-cn-hangzhou.aliyuncs.com";  
  17.         public AlibabaCloudStorageService(IConfiguration configuration) {  
  18.             Configuration = configuration;  
  19.             accessKeyId = Configuration["AccessKeyId"];  
  20.             accessKeySecret = Configuration["AccessKeySecret"];  
  21.             endpoint = Configuration["Endpoint"];  
  22.         }  
  23.         public async Task SaveFileAsync(MyFile file) {  
  24.             // Create an OSSClient instance.    
  25.             var client = new OssClient(endpoint, accessKeyId, accessKeySecret);  
  26.             client.PutObject(bucketName, file.FileName, new MemoryStream(file.FileInfo));  
  27.         }  
  28.         public async Task < IEnumerable < MyFile >> GetFiles() {  
  29.             // Create an OSSClient instance.    
  30.             var client = new OssClient(endpoint, accessKeyId, accessKeySecret);  
  31.             ObjectListing objects = client.ListObjects(bucketName);  
  32.             List < MyFile > list = new List < MyFile > ();  
  33.             foreach(var item in objects.ObjectSummaries) {  
  34.                 var myFile = new MyFile() {  
  35.                     FileName = item.Key  
  36.                 };  
  37.                 list.Add(myFile);  
  38.             }  
  39.             return list;  
  40.         }  
  41.         public async Task < Myfile > GetInfoFile(string fileName) {  
  42.             // Create an OSSClient instance.    
  43.             var client = new OssClient(endpoint, accessKeyId, accessKeySecret);  
  44.             var objectinfo = client.GetObject(bucketName, fileName);  
  45.             MemoryStream ms = new MemoryStream();  
  46.             await objectinfo.Content.CopyToAsync(ms);  
  47.             var myfile = new MyFile() {  
  48.                 FileName = objectinfo.Key, FileInfo = ms.ToArray()  
  49.             };  
  50.             return myfile;  
  51.         }  
  52.     }  
  53.     public interface IAlibabaCloudStorageService {  
  54.         Task SaveFileAsync(MyFilefile);  
  55.         Task < IEnumerable < MyFile >> GetFiles();  
  56.         Task < MyFile > GetInfoFile(string fileName);  
  57.     }  
  58. }   
I want to share with you this Guide where you can learn how to manage files in Blazor +.NET Core and Alibaba Cloud (OSS).