阿里雲OSS搭建移動應用直傳服務的.Net C#示例

OSS好幾個都沒有.Net示例,只有SDKgit

因而我就拿Java改爲C#代碼;使用前先去Nuget包管理器下載Aliyun.Acs.Core還有Aliyun.Acs.Sts;github

在安裝這個兩個包的時候安裝不了,可能由於是.Net Core的緣由吧。api

Nuget安裝方式安裝不了的,先去函數

https://github.com/aliyun/aliyun-openapi-net-sdk阿里雲

下載這兩個項目而後生成引用到本身項目中。spa

安裝後複製下面的代碼便可,記得要把bucketName改爲本身bucket的名字code

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
            {
                // 建立一個 Aliyun Acs Client, 用於發起 OpenAPI 請求
                IClientProfile profile = DefaultProfile.GetProfile(REGION_CN_HANGZHOU, accessKeyId, accessKeySecret);
                DefaultAcsClient client = new DefaultAcsClient(profile);

                // 建立一個 AssumeRoleRequest 並設置請求參數
                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;

                // 發起請求,並獲得response
                AssumeRoleResponse response = client.GetAcsResponse(request);

                return response;
            }
            catch (ClientException e)
            {
                throw e;
            }
        }

        public StsTokenModel GetToken()
        {
            // 只有 RAM用戶(子帳號)才能調用 AssumeRole 接口
            // 阿里雲主帳號的AccessKeys不能用於發起AssumeRole請求
            // 請首先在RAM控制檯建立一個RAM用戶,併爲這個用戶建立AccessKeys

            // RoleArn 須要在 RAM 控制檯上獲取
            // RoleSessionName 是臨時Token的會話名稱,本身指定用於標識你的用戶,主要用於審計,或者用於區分Token頒發給誰
            // 可是注意RoleSessionName的長度和規則,不要有空格,只能有'-' '_' 字母和數字等字符
            // 具體規則請參考API文檔中的格式要求
           string roleSessionName = "alice-001";

            // 必須爲 HTTPS
            try
            {
                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()函數返回的STS憑據數據模型
    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; }
    }
相關文章
相關標籤/搜索