编程语言
首页 > 编程语言> > asp.net core OSS STS 上传 Excel 文件

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 里的内容)

 

标签:core,asp,Console,OSS,request,Credentials,var,using,response
来源: https://www.cnblogs.com/modentime/p/15164880.html