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.
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.
To use OSS with .NET Core we need to install the library first.
Nuget package
To connect with the bucket you need 4 parameters,
- private readonly string bucketName = "bucketName"; //bucket name in OSS
- private readonly string accessKeyId = "<yourAccessKeyId>"; // AccessKey from Alibaba Account
- private readonly string accessKeySecret = "<yourAccessKeySecret>"; // KeySecret from Alibaba Account
- 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,
-
- public class MyFile
- {
- public string FileName {get;set;}
- public string ContentType {get;set;}
- public byte[] FileInfo {get;set;}
- }
- public async Task SaveFileAsync(MyFile file)
- {
-
- var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
-
- client.PutObject(bucketName, file.FileName, new MemoryStream(file.FileInfo));
- }
We can use ListObjects to get all the files in the Bucket,
-
- var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
-
- ObjectListing objects = client.ListObjects(bucketName);
And we can use GetObject to get all the information about a specific file including the data in bytes,
- public async Task < MyFile > GetInfoFile(string fileName) {
-
- var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
- var objectinfo = client.GetObject(bucketName, fileName);
- MemoryStream ms = new MemoryStream();
- await objectinfo.Content.CopyToAsync(ms);
- var myfile = new MyFile() {
- FileName = objectinfo.Key, FileInfo = ms.ToArray()
- };
- return myfile;
- }
Finally, this is an example including this method into an interface,
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.Extensions.Configuration;
- using shared;
- using Aliyun.OSS;
- using Aliyun.OSS.Common;
- namespace api.Services {
- public class AlibabaCloudStorageService: IAlibabaCloudStorageService {
- private readonly IConfiguration Configuration;
- private readonly string bucketName = "mteheranst1";
- private readonly string accessKeyId = "<yourAccessKeyId>";
- private readonly string accessKeySecret = "<yourAccessKeySecret>";
- private readonly string endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
- public AlibabaCloudStorageService(IConfiguration configuration) {
- Configuration = configuration;
- accessKeyId = Configuration["AccessKeyId"];
- accessKeySecret = Configuration["AccessKeySecret"];
- endpoint = Configuration["Endpoint"];
- }
- public async Task SaveFileAsync(MyFile file) {
-
- var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
- client.PutObject(bucketName, file.FileName, new MemoryStream(file.FileInfo));
- }
- public async Task < IEnumerable < MyFile >> GetFiles() {
-
- var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
- ObjectListing objects = client.ListObjects(bucketName);
- List < MyFile > list = new List < MyFile > ();
- foreach(var item in objects.ObjectSummaries) {
- var myFile = new MyFile() {
- FileName = item.Key
- };
- list.Add(myFile);
- }
- return list;
- }
- public async Task < Myfile > GetInfoFile(string fileName) {
-
- var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
- var objectinfo = client.GetObject(bucketName, fileName);
- MemoryStream ms = new MemoryStream();
- await objectinfo.Content.CopyToAsync(ms);
- var myfile = new MyFile() {
- FileName = objectinfo.Key, FileInfo = ms.ToArray()
- };
- return myfile;
- }
- }
- public interface IAlibabaCloudStorageService {
- Task SaveFileAsync(MyFilefile);
- Task < IEnumerable < MyFile >> GetFiles();
- Task < MyFile > GetInfoFile(string fileName);
- }
- }
I want to share with you this
Guide where you can learn how to manage files in Blazor +.NET Core and Alibaba Cloud (OSS).