1、獲取aws . net sdk
aws官網(wǎng)sdk
https://amazonaws-china.com/cn/sdk-for-net/
下載msi安裝程序
https://sdk-for-net.amazonwebservices.com/latest/AWSToolsAndSDKForNet.msi
或者直接使用NuGet下載這兩個(gè)庫(kù)
2崎脉、具體使用Demo
demo包含以下高頻接口
- PutBucket
- DeleteBucket
- ListObjects
- PutObject
- GetObject
- DeleteObject
- Multiupload(分塊上傳)
- ListBucket(需要重新設(shè)置ServiceURL)
其他低頻接口可參考
http://docs.ceph.com/docs/master/radosgw/s3/csharp/
http://docs.aws.amazon.com/AmazonS3/latest/dev/usingHLmpuDotNet.html
實(shí)例
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>替換為相對(duì)應(yīng)的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.高級(jí)設(shè)置
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需要重新設(shè)置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);
}
}
}
}