阿里云OSS搭建移动应用直传服务的.Net C#示例

2 篇文章 0 订阅
1 篇文章 0 订阅

OSS好几个都没有.Net示例,只有SDK

于是我就拿Java改成C#代码;使用前先去Nuget包管理器下载Aliyun.Acs.Core还有Aliyun.Acs.Sts;

在安装这个两个包的时候安装不了,可能因为是.Net Core的原因吧。

Nuget安装方式安装不了的,先去

aliyun-net-sdk-sts-3.0.0.zip 

aliyun-net-sdk-core-1.1.9.zip

aliyun_oss_dotnet_sdk_2_8_0.zip

aliyun-openapi-net-sdk-master.zip

下载这两个项目然后生成引用到自己项目中。

安装后复制下面的代码即可,记得要把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
            {
                // 创建一个 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; }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值