• AWS S3服务使用


    AWS S3是亚马逊的一种文件存储服务使用方便。

    一、配置服务

    public static class AWS_S3ClientInfo
        {
            private static readonly string awsAccessKey = "AKIAJOXFZDXXXXX";
    
            private static readonly string awsSecretKey = "6s2AGbNYmhF4xeXXXXXXXXX/";
    
            public static readonly string bucketName = "test";
    
            //提供最基本的一个配置
            public static AmazonS3Config config = new AmazonS3Config()
            {
                //这个地址是固定的,可以不写
                //ServiceURL = "http://s3.amazonaws.com",
    
                //因为aws服务器是分地区的,这里如果不配置区域会一直报错,还找不到头绪。
                RegionEndpoint = RegionEndpoint.APSoutheast1,
    
            };
    
            public static IAmazonS3 GetS3ClientInfo()
            {
                return new AmazonS3Client(awsAccessKey, awsSecretKey, config);
            }
    
        }

    二、调用上传服务

      public void UpLoadFile()
            {
                try
                {
                    using (IAmazonS3 s3Client = AWS_S3ClientInfo.GetS3ClientInfo())
                    {
                        var bt = File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "../../img/a.txt");
                       var mm= new MemoryStream(bt);
                        PutObjectRequest putObjectRequest = new PutObjectRequest();
                        putObjectRequest.InputStream = mm;
                        //可以上传string类型
                        //putObjectRequest.ContentBody = "你好!";
                        putObjectRequest.BucketName = AWS_S3ClientInfo.bucketName;
                        putObjectRequest.ContentType = "application/octet-stream";
     putObjectRequest.Metadata.Add("Access-Control-Allow-Origin", "*");
    //文件的路径 putObjectRequest.Key = $"img/test-6.jpg"; //文件权限 putObjectRequest.CannedACL = S3CannedACL.PublicRead; PutObjectResponse putObjectResponse = s3Client.PutObject(putObjectRequest); mm.Dispose(); Console.WriteLine("结果=" + putObjectResponse.HttpStatusCode); } } catch (Exception e) { Console.WriteLine("Exception message: {0}", e.Message); } }

    三、下载服务

                using (IAmazonS3 s3Client = AWS_S3ClientInfo.GetS3ClientInfo())
                {
                    GetObjectRequest request = new GetObjectRequest()
                    {
                        BucketName = AWS_S3ClientInfo.bucketName,
                        Key = "s3.txt"
                    };
                    //下载
                    GetObjectResponse response = s3Client.GetObject(request);
                    response.WriteResponseStreamToFile(AppDomain.CurrentDomain.BaseDirectory + "1.txt");
                }

    四、删除服务

            using (IAmazonS3 s3Client = AWS_S3ClientInfo.GetS3ClientInfo())
              {
                    GetObjectRequest request = new GetObjectRequest()
                    {
                        BucketName = AWS_S3ClientInfo.bucketName,
                        Key = "s3.txt"
                    };
    
                    //删除
                     s3Client.DeleteObject(request.BucketName, request.Key);
    
                }

    这个服务在Windows平台上没问题,在linux平台上上传不了,需要添加忽略证书

        //忽略验证ssl/tls证书
        ServicePointManager.ServerCertificateValidationCallback =(sender, certificate, chain, errors) => true;

    关于忽略证书

    关于S3文档

    S3的SDK

    AWS的RegionEndpoint集合

  • 相关阅读:
    DP实验
    Linux shell学习
    线性排序算法-计数排序
    算法笔试题练习
    堆排序
    node.js初识
    linux下vim命令详解
    html5学习笔记
    有趣的参数收集
    算法学习-归并排序
  • 原文地址:https://www.cnblogs.com/xiaoyaodijun/p/8527495.html
Copyright © 2020-2023  润新知