cos兼容aws-.net-sdk(c#)使用指南

1、获取aws . net sdk

aws官网sdk

https://amazonaws-china.com/cn/sdk-for-net/

下载msi安装程序

https://sdk-for-net.amazonwebservices.com/latest/AWSToolsAndSDKForNet.msi

或者直接使用NuGet下载这两个库


cos兼容aws-.net-sdk(c#)使用指南_第1张图片

2、具体使用Demo

demo包含以下高频接口

  • PutBucket
  • DeleteBucket
  • ListObjects
  • PutObject
  • GetObject
  • DeleteObject
  • Multiupload(分块上传)
  • ListBucket(需要重新设置ServiceURL)
    其他低频接口可参考
http://docs.ceph.com/docs/master/radosgw/s3/csharp/
http://docs.aws.amazon.com/AmazonS3/latest/dev/usingHLmpuDotNet.html

实例

using System;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;

using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
namespace GettingStartedGuide
{
    class S3Sample
    {
        //注意bucketName是bucketname+appid
        static string bucketName = "lewzylutest-1252448703";
        static string keyName = "12312";
        static string filePath = "E:/a.txt";
        public static void Main(string[] args)
        {
            AmazonS3Config config = new AmazonS3Config();
            //secretid
            string accessKey = "******************************";
            //secretkey
            string secretKey = "******************************";

            //注意要将替换为相对应的region,如ap-beijing,ap-guangzhou...
            config.ServiceURL = "http://cos.ap-guangzhou.myqcloud.com";
            AmazonS3Client client = new AmazonS3Client(
                    accessKey,
                    secretKey,
                    config
                    );

            //PutBucket
            try
            {
                PutBucketRequest request = new PutBucketRequest();
                request.BucketName = bucketName;
                PutBucketResponse response = client.PutBucket(request);
                Console.WriteLine("PutBucket");
                Console.WriteLine(response.HttpStatusCode);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            //ListObjects
            try
            {
                ListObjectsRequest request = new ListObjectsRequest();
                request.BucketName = bucketName;
                ListObjectsResponse response = client.ListObjects(request);
                Console.WriteLine("ListObjects");
                Console.WriteLine(response.HttpStatusCode);
                foreach (S3Object o in response.S3Objects)
                {
                    Console.WriteLine("{0}\t{1}\t{2}", o.Key, o.Size, o.LastModified);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
            //PutObject
            try
            {
                PutObjectRequest request = new PutObjectRequest();
                request.BucketName = bucketName;
                request.Key = keyName;
                request.ContentType = "text/plain";
                //request.FilePath = filePath;
                request.ContentBody = "12321";
                PutObjectResponse response = client.PutObject(request);
                Console.WriteLine("PutObject");
                Console.WriteLine(response.HttpStatusCode);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            } 

            //GetObject
            try
            {
                GetObjectRequest request = new GetObjectRequest();
                request.BucketName = bucketName;
                request.Key = keyName;
                GetObjectResponse response = client.GetObject(request);
                response.WriteResponseStreamToFile("C:\\Users\\larry\\Documents\\hello.txt");
                Console.WriteLine("GetObject");
                Console.WriteLine(response.HttpStatusCode);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            //DeleteObject
            try
            {
                DeleteObjectRequest request = new DeleteObjectRequest();
                request.BucketName = bucketName;
                request.Key = keyName;
                DeleteObjectResponse response = client.DeleteObject(request);
                Console.WriteLine("DeleteObject");
                Console.WriteLine(response.HttpStatusCode);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            //DeleteBucket
            try
            {
                DeleteBucketRequest request = new DeleteBucketRequest();
                request.BucketName = bucketName;
                DeleteBucketResponse response = client.DeleteBucket(request);
                Console.WriteLine("DeleteBucket");
                Console.WriteLine(response.HttpStatusCode);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            //Multiupload
            try
            {
                TransferUtility fileTransferUtility = new
                    TransferUtility(client);

                // 1. 以本地文件的名字上传文件
                fileTransferUtility.Upload(filePath, bucketName);
                Console.WriteLine("Upload 1 completed");

                // 2. 将本地文件以keyName的名字上传
                fileTransferUtility.Upload(filePath,
                                          bucketName, keyName);
                Console.WriteLine("Upload 2 completed");

                // 3. 从流上传
                using (FileStream fileToUpload =
                    new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    fileTransferUtility.Upload(fileToUpload,
                                               bucketName, keyName);
                }
                Console.WriteLine("Upload 3 completed");

                // 4.高级设置
                TransferUtilityUploadRequest fileTransferUtilityRequest = new TransferUtilityUploadRequest
                {
                    BucketName = bucketName,
                    FilePath = filePath,
                    StorageClass = S3StorageClass.ReducedRedundancy,
                    PartSize = 6291456, // 6 MB.
                    Key = keyName,
                    CannedACL = S3CannedACL.PublicRead
                };
                fileTransferUtilityRequest.Metadata.Add("param1", "Value1");
                fileTransferUtilityRequest.Metadata.Add("param2", "Value2");
                fileTransferUtility.Upload(fileTransferUtilityRequest);
                Console.WriteLine("Upload 4 completed");
            }
            catch (AmazonS3Exception s3Exception)
            {
                Console.WriteLine(s3Exception.Message,
                                  s3Exception.InnerException);
            }

            //listBuckets
            //注意:listBuckets需要重新设置ServiceURL
            config.ServiceURL = "http://service.cos.myqcloud.com";
            client = new AmazonS3Client(
                    accessKey,
                    secretKey,
                    config
                    );
            try
            {
                ListBucketsResponse response = client.ListBuckets();
                foreach (S3Bucket b in response.Buckets)
                {
                    Console.WriteLine("{0}\t{1}", b.BucketName, b.CreationDate);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        

    }
}

你可能感兴趣的:(cos兼容aws-.net-sdk(c#)使用指南)