阿里云OSS搭建移动应用直传服务的.Net C#示例
生活随笔
收集整理的這篇文章主要介紹了
阿里云OSS搭建移动应用直传服务的.Net C#示例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
OSS好幾個都沒有.Net示例,只有SDK
于是我就拿Java改成C#代碼;使用前先去Nuget包管理器下載Aliyun.Acs.Core還有Aliyun.Acs.Sts;
在安裝這個兩個包的時候安裝不了,可能因為是.Net Core的原因吧。
Nuget安裝方式安裝不了的,先去
https://github.com/aliyun/aliyun-openapi-net-sdk
下載這兩個項目然后生成引用到自己項目中。
安裝后復制下面的代碼即可,記得要把bucketName改成自己bucket的名字
using Aliyun.Acs.Core.Exceptions; using Aliyun.Acs.Sts.Model.V20150401; using Aliyun.Acs.Core.Http; using Aliyun.Acs.Core.Profile; using Aliyun.Acs.Core;namespace Test {public class OssServer{private const string REGION_CN_HANGZHOU = "cn-hangzhou";private const string STS_API_VERSION = "2015-04-01";private const string AccessKeyID = "****你的AccessKeyID****";private const string AccessKeySecret = "****你的AccessKeySecret****";private const string RoleArn = "****你的RoleArn****";private const int TokenExpireTime = 3600;//這里是權限配置,請參考oss的文檔private const string PolicyFile = @"{""Statement"": [{""Action"": [""oss:PutObject""],""Effect"": ""Allow"",""Resource"": [""acs:oss:*:*:bucketName/*"", ""acs:oss:*:*:bucketName""]}],""Version"": ""1"" }"; private AssumeRoleResponse assumeRole(String accessKeyId, String accessKeySecret, String roleArn,String roleSessionName, String policy, ProtocolType protocolType, long durationSeconds){try{// 創(chuàng)建一個 Aliyun Acs Client, 用于發(fā)起 OpenAPI 請求IClientProfile profile = DefaultProfile.GetProfile(REGION_CN_HANGZHOU, accessKeyId, accessKeySecret);DefaultAcsClient client = new DefaultAcsClient(profile);// 創(chuàng)建一個 AssumeRoleRequest 并設置請求參數(shù)AssumeRoleRequest request = new AssumeRoleRequest();//request.Version = STS_API_VERSION;request.Method = MethodType.POST;//request.Protocol = protocolType; request.RoleArn = roleArn;request.RoleSessionName = roleSessionName;request.Policy = policy;request.DurationSeconds = durationSeconds;// 發(fā)起請求,并得到responseAssumeRoleResponse response = client.GetAcsResponse(request);return response;}catch (ClientException e){throw e;}}public StsTokenModel GetToken(){// 只有 RAM用戶(子賬號)才能調用 AssumeRole 接口// 阿里云主賬號的AccessKeys不能用于發(fā)起AssumeRole請求// 請首先在RAM控制臺創(chuàng)建一個RAM用戶,并為這個用戶創(chuàng)建AccessKeys// RoleArn 需要在 RAM 控制臺上獲取// RoleSessionName 是臨時Token的會話名稱,自己指定用于標識你的用戶,主要用于審計,或者用于區(qū)分Token頒發(fā)給誰// 但是注意RoleSessionName的長度和規(guī)則,不要有空格,只能有'-' '_' 字母和數(shù)字等字符// 具體規(guī)則請參考API文檔中的格式要求string roleSessionName = "alice-001";// 必須為 HTTPStry{AssumeRoleResponse stsResponse = assumeRole(AccessKeyID, AccessKeySecret, RoleArn, roleSessionName,PolicyFile, ProtocolType.HTTPS, TokenExpireTime);return new StsTokenModel(){status = 200,AccessKeyId = stsResponse.Credentials.AccessKeyId,AccessKeySecret = stsResponse.Credentials.AccessKeySecret,Expiration = stsResponse.Credentials.Expiration,Security = stsResponse.Credentials.SecurityToken};}catch (ClientException e){return new StsTokenModel() { status = Convert.ToInt32(e.ErrorCode) };}}} } GetToken()函數(shù)返回的STS憑據(jù)數(shù)據(jù)模型public class StsTokenModel{public int status { get; set; }public string AccessKeyId { get; set; }public string AccessKeySecret { get; set; }public string Security { get; set; }public string Expiration { get; set; }}
轉載于:https://www.cnblogs.com/myhalo/p/6626530.html
總結
以上是生活随笔為你收集整理的阿里云OSS搭建移动应用直传服务的.Net C#示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET Core:CMD命令行+
- 下一篇: C# 读取Excel文件,并写入word