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; } }