码迷,mamicode.com
首页 > 其他好文 > 详细

AWS S3服务使用

时间:2018-03-08 13:59:25      阅读:634      评论:0      收藏:0      [点我收藏+]

标签:can   accesskey   cli   file   验证   errors   ase   文件   你好   

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;

关于忽略证书

 

AWS S3服务使用

标签:can   accesskey   cli   file   验证   errors   ase   文件   你好   

原文地址:https://www.cnblogs.com/xiaoyaodijun/p/8527495.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!