AWS S3服务使用

Posted 逍遥帝君

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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集合

以上是关于AWS S3服务使用的主要内容,如果未能解决你的问题,请参考以下文章

如何在云端路由 S3、EC2、aws

AWS S3服务使用

AWS系列-S3实现文件服务页面展示

将 AWS S3 存储桶同步到 EC2 服务器

为啥从远程 AWS S3 服务器下载 json 文件时 axios GET 错误

golang aws-sdk-go 之 s3 服务