6. 文件管理¶
在NOS中,用户可以通过一系列的接口管理桶(Bucket)中的文件(Object),比如ListObjects,DeleteObject,CopyObject,DoesObjectExist等。
6.1. 列出桶中的文件¶
您可以使用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
- 上述表中的参数都是可选参数
6.2. 判断文件是否存在¶
您可以使用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);
}
}
6.3. 删除单个文件¶
您可以使用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);
}
}
6.4. 删除多个文件¶
您可以使用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);
break;
}
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);
}
}
6.5. 拷贝文件¶
您可以使用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
6.6. 移动文件¶
您可以使用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
6.7. 修改文件元信息¶
暂时不提供此类方法
6.8. 获取文件的文件元信息¶
您可以使用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);
}
}