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 文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程