【实例简介】OSS可以通过阿里云STS (Security Token Service) 进行临时授权访问。阿里云STS是为云计算用户提供临时访问令牌的Web服务。通过STS,您可以为第三方应用或子用户(即用户身份由您自己管理的用户)颁发一个自定义时效和权限的访问凭证。最终实现 让第三方应用(可以是客户端) 直接向阿里云oss上传文件,而不经过中转服务器。
当然 下载和删除操作 也可以通过此方法实现,只是改下授权规则即可
【实例截图】
自定义权限策略内容可参考如下:
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "oss:Put*",//所有上传权限 "oss:Get*"//所有下载权限 ], "Resource": [ "acs:oss:*:*:yourbucketName/*" ] } ]}【核心代码】
using Aliyun.Acs.Core;using Aliyun.Acs.Core.Auth.Sts;using Aliyun.Acs.Core.Http;using Aliyun.Acs.Core.Profile;using Aliyun.OSS;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace ApiDemo{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnTest_Click(object sender, EventArgs e) { //完整逻辑参考这里 https://help.aliyun.com/document_detail/100624.html?spm=a2c4g.11186623.2.10.471d41f0riXKMd string accessKeyId = "********";//这是子的账号accessKeyId string secret = "********************";//这是子的账号secret var RoleArn = "acs:ram::*******:role/*********";//这个要参考这里 https://help.aliyun.com/document_detail/100624.html?spm=a2c4g.11186623.2.10.471d41f0riXKMd var RoleSessionName = "FileUploadAssistantClient";//这个名字随意 const string REGIONID = ""; const string ENDPOINT = "sts.aliyuncs.com"; // 构建一个阿里云client, 用于发起请求 // 构建阿里云client时需要设置AccessKey ID和AccessKey Secret DefaultProfile.AddEndpoint(REGIONID, REGIONID, "Sts", ENDPOINT); IClientProfile profile = DefaultProfile.GetProfile(REGIONID, accessKeyId, secret); DefaultAcsClient client = new DefaultAcsClient(profile); // 构建AssumeRole请求 AssumeRoleRequest request = new AssumeRoleRequest(); request.AcceptFormat = FormatType.JSON; // 指定角色ARN request.RoleArn = RoleArn; request.RoleSessionName = RoleSessionName; // 设置token有效期,可选参数,默认3600秒; // request.DurationSeconds = 3600; // 设置token的附加权限策略;在获取token时,通过额外设置一个权限策略进一步减小token的权限; // request.Policy="<policy-content>" try { #region 这里就是生成的临时凭证,通常以接口的形式返回出来 AssumeRoleResponse response = client.GetAcsResponse(request); Console.WriteLine("AccessKeyId: " response.Credentials.AccessKeyId); Console.WriteLine("AccessKeySecret: " response.Credentials.AccessKeySecret); Console.WriteLine("SecurityToken: " response.Credentials.SecurityToken); //Token过期时间;服务器返回UTC时间,这里转换成北京时间显示; Console.WriteLine("Expiration: " DateTime.Parse(response.Credentials.Expiration).ToLocalTime()); #endregion #region 实际上这里的内容应该是第三方 获得了 以上的response.Credentials 后调用的 var ossBucktName = "这里是你的oss bucket名称"; var ossEndpoint = "这里是您的oss所在地区"; //例如:"oss-cn-hangzhou.aliyuncs.com"; var ossAccessKeyId = response.Credentials.AccessKeyId; var ossAccessKeySecret = response.Credentials.AccessKeySecret; var ossSecurityToken = response.Credentials.SecurityToken; // 拿到STS临时凭证后,通过其中的安全令牌(SecurityToken)和临时访问密钥(AccessKeyId和AccessKeySecret)生成OSSClient。 // 创建OSSClient实例。 var ossStsClient = new OssClient(ossEndpoint, ossAccessKeyId, ossAccessKeySecret, ossSecurityToken); // 进行OSS操作。 var result = ossStsClient.PutObject(ossBucktName, "2020/07/26/ab/3/8/aaa.txt",System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test.txt")); if(result.HttpStatusCode==System.Net.HttpStatusCode.OK) MessageBox.Show("上传成功"); #endregion } catch (Exception ex) { Console.Write(ex.ToString()); } Console.ReadLine(); //System.IO.File.WriteAllText("C:/test.txt","www.haolizi.net"); //MessageBox.Show("写入成功"); } }}下载声明:
1、本站所有资源、仅供学习交流,不得商业运营资源,不确保资源完整性,图片和资源仅供参考, 不提供任何技术服务。
2、本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权
归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。举报邮箱 :859840003@qq.com




评论