asp.net core OSS STS 上传 Excel 文件
2021/8/20 9:05:53
本文主要是介绍asp.net core OSS STS 上传 Excel 文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
OSS 怎么配STS, 需要参考阿里云官方文档
using Aliyun.Acs.Core; using Aliyun.Acs.Core.Auth.Sts; using Aliyun.Acs.Core.Http; using Aliyun.Acs.Core.Profile; using Aliyun.OSS; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace aspcore { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static void uploadFile() { var bucketName = "****"; var accessKeyId = "Access Key"; var accessKeySecret = "Access Secret"; var endpoint = "http://oss-cn-***.aliyuncs.com"; var region = "oss-cn-***"; var roleArn = "acs:ram::***:role/***"; // 通过阿里云RAM管理角色管理可以拿到 var roleSessionName = "***"; // 随机指定一个即可 var objectName = "test/test.txt"; IClientProfile profile = DefaultProfile.GetProfile(region, accessKeyId, accessKeySecret); DefaultAcsClient client = new DefaultAcsClient(profile); AssumeRoleRequest request = new AssumeRoleRequest(); request.AcceptFormat = FormatType.JSON; //指定角色ARN request.RoleArn = roleArn; request.RoleSessionName = roleSessionName; request.DurationSeconds = 3600; request.Policy = BuildPolicy(bucketName, "test"); // 配置对应的权限 //request.Policy = BuildPolicy(.setPolicy(policy); AssumeRoleResponse response = client.GetAcsResponse(request); Console.WriteLine("AccessKeyId: " + response.Credentials.AccessKeyId); Console.WriteLine("AccessKeySecret: " + response.Credentials.AccessKeySecret); Console.WriteLine("SecurityToken: " + response.Credentials.SecurityToken); Console.WriteLine("Expiration: " + DateTime.Parse(response.Credentials.Expiration).ToLocalTime()); var ossClient = new OssClient(endpoint, response.Credentials.AccessKeyId, response.Credentials.AccessKeySecret, response.Credentials.SecurityToken); try { byte[] binaryData = Encoding.ASCII.GetBytes("test"); MemoryStream requestContent = new MemoryStream(binaryData); // 上传文件。 ossClient.PutObject(bucketName, $"{bucketName}/{objectName}", requestContent); Console.WriteLine("Put object succeeded"); } catch (Exception ex) { Console.WriteLine("Put object failed, {0}", ex.Message); } Console.ReadKey(); } public static string BuildPolicy(string bucket, string dir) { return "{\n" + " \"Version\": \"1\", \n" + " \"Statement\": [\n" + " {\n" + " \"Action\": [\n" + " \"oss:PutObject\"\n" + " ], \n" + " \"Resource\": [\n" + $" \"acs:oss:*:*:{bucket}/*\" \n" + " ], \n" + " \"Effect\": \"Allow\"\n" + " }\n" + " ]\n" + "}"; } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>().UseUrls("http://*:9000"); }); } }
上传 Excel 文件, 解决乱码问题 (下面的代码替换 Try 里的内容)
这篇关于asp.net core OSS STS 上传 Excel 文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具
- 2024-01-24.NET集成IdGenerator生成分布式全局唯一ID