.NET SDK 手册
文件管理
在 NOS 中,用户可以通过一系列的接口管理桶 (Bucket) 中的文件 (Object),比如 ListObjects,DeleteObject,CopyObject,DoesObjectExist 等。
列出桶中的文件
您可以使用 NosClient.ListObjects 列出存储中间中的文件:
// 初始化 NosClient
var nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
/// <summary>
/// 列出指定存储空间下的文件的摘要信息
/// </summary>
/// <param name="bucket">桶名</param>
public void ListObjects(string bucket)
{
try
{
var keys = new List<string>();
ObjectListing result = null;
string nextMarker = string.Empty;
do
{
var listObjectsRequest = new ListObjectsRequest(bucket)
{
Marker = nextMarker,
MaxKeys = 100
};
result = nosClient.ListObjects(listObjectsRequest);
foreach (var summary in result.ObjectSummarise)
{
Console.WriteLine(summary.Key);
keys.Add(summary.Key);
}
nextMarker = result.NextMarker;
} while (result.IsTruncated);
Console.WriteLine("List objects of bucket:{0} succeeded ", bucket);
}
catch (NosException ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
上述代码中的 ListObjectsRequest 中的可选参数如下所示:
参数 | 说明 |
---|---|
Delimiter | 用于对 Object 名字进行分组的字符。所有名字包含指定的前缀且第一次出现 delimiter 字符之间的 object 作为一组元素。限制长度为 10。 |
Prefix | 限定返回的 object key 必须以 prefix 作为前缀。注意使用 prefix 查询时,返回的 key 中仍会包含 prefix |
MaxKeys | 限定此次返回 object 的最大数,如果不设定,默认为 100 |
Marker | 设定结果从 marker 之后按字母排序的第一个开始返回 |
Note
- 上述表中的参数都是可选参数。
- 会列出文件、目录以及目录中的文件。
判断文件是否存在
您可以使用 NosClient.DoesObjectExist 判断文件是否存在:
// 初始化 NosClient
var nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
/// <summary>
/// 判断指定桶内是否存在指定文件
/// </summary>
/// <param name="bucket">桶名</param>
/// <param name="key">对象名</param>
public void DoesObjectExist(string bucket, string key)
{
try
{
var exist = nosClient.DoesObjectExist(bucket, key);
Console.WriteLine("exist ? " + exist);
}
catch (NosException ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
Attention
- 默认不能对目录进行判断。
- 判断目录内文件是否存在时参数 key 格式为 "dir1/file1"。
删除单个文件
您可以使用 NosClient.DeleteObject 删除单个需要删除的文件,删除目录时目录内不能有文件:
// 初始化 NosClient
var nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
/// <summary>
/// 删除单个文件
/// </summary>
/// <param name="bucket">桶名</param>
/// <param name="key">对象名</param>
public void DeleteObject(string bucket, string key)
{
try
{
nosClient.DeleteObject(bucket, key);
Console.WriteLine("Delete object succeeded");
}
catch (NosException ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
Note
参数 key 书写格式:
- key 为空目录时格式为 "dir1/"。
- key 为目录内文件时格式为 "dir1/file1"。
删除多个文件
您可以使用 NosClient.DeleteObjects 批量删除文件,默认会删除当前桶内所有文件、目录以及目录内文件:
// 初始化 NosClient
var nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
/// <summary>
/// 删除多个文件
/// </summary>
/// <param name="bucket">桶名</param>
public void DeleteObjects(string bucket)
{
try
{
var keys = new List<string>();
var listObjectsResult = nosClient.ListObjects(bucket);
foreach (var summary in listObjectsResult.ObjectSummarise)
{
keys.Add(summary.Key);
}
var result = nosClient.DeleteObjects(bucket, keys, false);
Console.WriteLine("Delete objects succeeded");
if (result.Keys != null)
{
foreach (var deletedObject in result.Keys)
{
Console.Write("\n[Deleted]: " + deletedObject.Key);
}
}
if (result.Error != null)
{
foreach (var deleteError in result.Error)
{
Console.Write("\n[Error]: " + deleteError.Key + "\t" + deleteError.Code + "\t" + deleteError.Message);
}
}
}
catch (NosException ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
Note
可以结合列出桶内文件的接口进行使用。
拷贝文件
您可以使用 NosClient.CopyObject 拷贝文件:
// 初始化 NosClient
var nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
/// <summary>
/// 拷贝文件
/// </summary>
/// <param name="sourceBucket">源桶名</param>
/// <param name="sourceKey">源对象名</param>
/// <param name="targetBucket">目标桶名</param>
/// <param name="targetKey">目标对象名</param>
public void CopyObject(string sourceBucket, string sourceKey, string targetBucket, string targetKey)
{
try
{
var request = new CopyObjectRequest(sourceBucket, sourceKey, targetBucket, targetKey);
nosClient.CopyObject(request);
Console.WriteLine("Copy object succeeded");
}
catch (NosException ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
Attention
支持跨桶的文件 copy
移动文件
您可以使用 NosClient.MoveObject 移动文件:
// 初始化 NosClient
var nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
/// <summary>
/// 移动文件
/// </summary>
/// <param name="sourceBucket">源桶名</param>
/// <param name="sourceKey">源对象名</param>
/// <param name="targetBucket">目标桶名</param>
/// <param name="targetKey">目标对象名</param>
public void MoveObject(string sourceBucket, string sourceKey, string targetBucket, string targetKey)
{
try
{
var request = new MoveObjectRequest(sourceBucket, sourceKey, targetBucket, targetKey);
nosClient.MoveObject(request);
Console.WriteLine("Move object succeeded");
}
catch (NosException ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
Attention
暂时不支持跨桶的文件 move
修改文件元信息
暂时不提供此类方法
获取文件的文件元信息
您可以使用 NosClient.GetObjectMetadata 获取对象的元数据信息:
// 初始化 NosClient
var nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
/// <summary>
/// 获取文件的文件元信息
/// </summary>
/// <param name="bucket">桶名</param>
/// <param name="key">对象名</param>
public void GetObjectMetadata(string bucket, string key)
{
try
{
var objectMetadata = nosClient.GetObjectMetadata(bucket, key);
Console.WriteLine("Get Object Metadata succeeded");
}
catch (NosException ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}