Skip to content

Commit dbffe9a

Browse files
committed
增加listFiles(获取空间文件列表)支持
1 parent efa4125 commit dbffe9a

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

Qiniu/Storage/BucketManager.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,84 @@ public CdnRefreshResult cdnRefresh(List<string> urls, List<string> dirs)
364364
return result;
365365
}
366366

367+
/// <summary>
368+
///
369+
/// 获取空间文件列表
370+
/// listFiles(bucket, prefix, marker, limit, delimiter)
371+
///
372+
/// bucket: 目标空间名称
373+
///
374+
/// prefix: 返回指定文件名前缀的文件列表(prefix可设为null)
375+
///
376+
/// marker: 考虑到设置limit后返回的文件列表可能不全(需要重复执行listFiles操作)
377+
/// 执行listFiles操作时使用marker标记来追加新的结果
378+
/// 特别注意首次执行listFiles操作时marker为null
379+
///
380+
/// limit: 每次返回结果所包含的文件总数限制(limit<=1000,建议值100)
381+
///
382+
/// delimiter: 分隔符,比如-或者/等等,可以模拟作为目录结构(参考下述示例)
383+
/// 假设指定空间中有2个文件 fakepath/1.txt fakepath/2.txt
384+
/// 现设置分隔符delimiter = / 得到返回结果items =[],commonPrefixes = [fakepath/]
385+
/// 然后调整prefix = fakepath/ delimiter = null 得到所需结果items = [1.txt,2.txt]
386+
/// 于是可以在本地先创建一个目录fakepath,然后在该目录下写入items中的文件
387+
///
388+
/// </summary>
389+
public ListFilesResult listFiles(string bucket,string prefix,string marker,int limit,string delimiter)
390+
{
391+
ListFilesResult result = null;
392+
393+
StringBuilder sb = new StringBuilder("bucket=" + bucket);
394+
395+
if (!string.IsNullOrEmpty(marker))
396+
{
397+
sb.Append("&marker=" + marker);
398+
}
399+
400+
if(!string.IsNullOrEmpty(prefix))
401+
{
402+
sb.Append("&prefix=" + prefix);
403+
}
404+
405+
if(!string.IsNullOrEmpty(delimiter))
406+
{
407+
sb.Append("&delimiter=" + delimiter);
408+
}
409+
410+
if(limit>1000||limit<1)
411+
{
412+
sb.Append("&limit=1000");
413+
}
414+
else
415+
{
416+
sb.Append("&limit=" + limit);
417+
}
418+
419+
string listFilesUrl = Config.ZONE.RsfHost + "/list?" + sb.ToString();
420+
string accessToken = Auth.createManageToken(listFilesUrl, null, mac);
421+
422+
Dictionary<string, string> listFilesHeaders = new Dictionary<string, string>();
423+
listFilesHeaders.Add("Authorization", accessToken);
424+
425+
CompletionHandler listFilesCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
426+
{
427+
result = new ListFilesResult();
428+
result.Response = response;
429+
result.ResponseInfo = respInfo;
430+
if (respInfo.isOk())
431+
{
432+
ListFilesResponse resp = JsonConvert.DeserializeObject<ListFilesResponse>(response);
433+
434+
result.Marker = resp.Marker;
435+
result.Items = resp.Items;
436+
result.CommonPrefixes = resp.CommonPrefixes;
437+
}
438+
});
439+
440+
this.mHttpManager.postForm(listFilesUrl, listFilesHeaders, null, listFilesCompletionHandler);
441+
442+
return result;
443+
}
444+
367445
public string statOp(string bucket, string key)
368446
{
369447
return string.Format("/stat/{0}", StringUtils.encodedEntry(bucket, key));
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Qiniu.Http;
2+
using System.Collections.Generic;
3+
4+
namespace Qiniu.Storage.Model
5+
{
6+
/// <summary>
7+
/// 获取空间文件列表
8+
/// </summary>
9+
public class ListFilesResult : HttpResult
10+
{
11+
public string Marker { get; set; }
12+
public List<FileDesc> Items { set; get; }
13+
14+
public List<string> CommonPrefixes { get; set; }
15+
public ListFilesResult() { }
16+
}
17+
18+
/// <summary>
19+
/// 返回JSON字符串
20+
///
21+
/// {
22+
/// "marker":"MARKER",
23+
/// "items":
24+
/// [
25+
/// {
26+
/// "key":"KEY",
27+
/// "hash":"HASH",
28+
/// "fsize":FSIZE,
29+
/// "mimeType":"MIME_TYPE",
30+
/// "putTime":PUT_TIME
31+
/// },
32+
/// {
33+
/// ...
34+
/// }
35+
/// ],
36+
/// "CmmonPrefixes":"COMMON_PREFIXES"
37+
/// }
38+
///
39+
/// </summary>
40+
public class ListFilesResponse
41+
{
42+
public string Marker { get; set; }
43+
44+
public List<FileDesc> Items { get; set; }
45+
46+
public List<string> CommonPrefixes { get; set; }
47+
}
48+
49+
public class FileDesc
50+
{
51+
public string Key { get; set; }
52+
53+
public string Hash { get; set; }
54+
55+
public long Fsize { get; set; }
56+
57+
public string MimeType { get; set; }
58+
59+
public long PutTime { get; set; }
60+
}
61+
62+
}

0 commit comments

Comments
 (0)