文档中心 > 简单上传
简单上传

最近更新时间:2021-10-29

以下代码用于简单上传文件。

using System;

using Amazon.S3;

using Amazon.S3.Model;

 

namespace PutObject

{

    class Program

    {

        static void Main(string[] args)

        {  

            var Ak = "xxx";

            var Sk = "xxx";

            var endpoint = "http://s3.test.com";

            AmazonS3Client serviceClient = new AmazonS3Client(Ak,Sk,

            new AmazonS3Config{ ServiceURL = endpoint });

 

            try

            {  

                PutObjectResponse response;

                PutObjectRequest putObject = new PutObjectRequest{

                    BucketName = "xxx",

                    Key = "xxx", // object name and path

                    FilePath = "xxx" // The full path and name to a file to be uploaded

                };

 

                response = serviceClient.PutObjectAsync(putObject).GetAwaiter().GetResult();

                Console.WriteLine(response.HttpStatusCode);          

 

            } catch (AmazonS3Exception e) {

                Console.WriteLine(e.Data);

                throw;

            } catch (Exception e) {

                Console.WriteLine(e);

                throw;

            }

        }

    }

}