3. 快速入门¶
使用S3 C# SDK前,您可以先参照 API 手册 熟悉NOS的基本概念,如Bucket、Object、EndPoint、AccessKeyId和AccessKeySecret等。 本节您将看到如何快速的使用S3 C# SDK,完成常用的操作,上传文件、下载文件等。
3.1. 常用类¶
常用类 | 备注 |
---|---|
AmazonS3Client | NOS客户端类,用户通过AmazonS3Client调用服务 |
AmazonException | NOS异常,在调用过程中去捕获这个异常,查看失败原因 |
3.2. 基本操作¶
3.2.1. 上传文件¶
对象(Object)是NOS中最基本的数据单元,您可以把它简单的理解为文件,以下代码可以实现简单的对象上传:
public void PutObjectByFilePath(string bucket, string key, string filePath)
{
PutObjectRequest putObjectRequest = new PutObjectRequest()
{
BucketName = bucket,
Key = key,
FilePath = filePath,
};
try
{
PutObjectResponse resp = s3Client.PutObject(putObjectRequest);
}
catch (AmazonS3Exception ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3};",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
Attention
- 对象命名规则请参见 API 手册 对象
- 更多的上传文件信息,请参见 S3-C#-SDK 上传文件
3.2.2. 下载文件¶
上传对象成功之后,您可以读取它的内容,以下代码可以实现文件的下载:
public void GetObject(string bucket,string key){
GetObjectRequest request = new GetObjectRequest
{
BucketName = bucket,
Key = key
};
try
{
using (GetObjectResponse response = s3Client.GetObject(request))
{
using (StreamReader reader = new StreamReader(response.ResponseStream))
{
string contents = reader.ReadToEnd();
Console.WriteLine(" Object - " + response.Key);
Console.WriteLine(" Contents - " + contents);
}
}
} catch(AmazonS3Exception ex){
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3};",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId);
} catch(Exception ex){
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
更多的下载文件信息,请参见 S3 C# SDK 下载文件
3.2.3. 列举文件¶
当上传文件成功之后,可以查看桶中包含的文件列表,以下代码展示如何列举桶内的文件:
public void ListObjects(string bucketName){
try
{
var keys = new List<string>();
ListObjectsResponse result = null;
string nextMarker = string.Empty;
do
{
var listObjectsRequest = new ListObjectsRequest()
{
BucketName = bucketName,
Marker = nextMarker,
MaxKeys = 5
};
result = s3Client.ListObjects(listObjectsRequest);
foreach (var obj in result.S3Objects)
{
Console.WriteLine(obj.Key);
}
nextMarker = result.NextMarker;
} while (result.IsTruncated);
Console.WriteLine("List objects of bucket:{0} succeeded ", bucketName);
}
catch (AmazonS3Exception ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3};",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
Note
- 上面的代码默认列举100个object
更多的管理文件信息,请参见 S3 C# SDK 文件管理
3.2.4. 删除文件¶
文件上传成功后,可以指定删除桶中的文件,以下代码实现桶中文件的删除:
public void DeleteObject(string bucketNaame, string key)
{
try
{
var resp = s3Client.DeleteObject(bucketNaame,key);
Console.WriteLine("StatusCode : {0}", resp.HttpStatusCode);
}
catch (AmazonS3Exception ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3};",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
3.2.5. 返回结果处理¶
S3 提供的所有接口都是有返回值的,如果你关心,那么就接住返回值,然后取你想要的信息,如果不关注,那么就不用获取
例如:
try
{
var request = new CopyObjectRequest(){
SourceBucket = sourceBucket,
SourceKey = sourceKey,
DestinationBucket = targetBucket,
DestinationKey = targetKey
};
s3Client.CopyObject(request);
Console.WriteLine("Copy object succeeded");
}
catch (AmazonS3Exception ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3};",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}