Skip to content

Commit 179eaaa

Browse files
committed
Merge pull request #8 from shijiayun/master
ResumablePut
2 parents 5d6d7f4 + 0e2d273 commit 179eaaa

26 files changed

+813
-305
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## CHANGE LOG
2+
3+
### v3.0.0
4+
5+
增加 DownloadToken 支持(QBox/Auth)
6+
7+
AuthPolicy 增加 Escape,AsyncOps,ReturnBody 字段(QBox/Auth)
8+
9+
增加简易的断点续上传支持(QBox/RS)
10+
11+
更新了文档(QBox/Docs)
12+
13+
修复了 Base64 编码不支持中文的情况(QBox/Util)

Demo/Demo.cs

Lines changed: 53 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using QBox.Auth;
33
using QBox.RS;
44
using QBox.FileOp;
5+
using QBox.RPC;
56

67
namespace QBox.Demo
78
{
@@ -10,39 +11,41 @@ public class Demo
1011
public static string bucketName;
1112
public static string key;
1213
public static string localFile;
14+
public static string bigkey;
15+
public static string bigFile;
1316
public static string DEMO_DOMAIN;
1417
public static Client conn;
1518
public static RSService rs;
16-
public static ImageOp imageOp;
1719

1820
public static void Main()
1921
{
2022
Config.ACCESS_KEY = "<Please apply your access key>";
2123
Config.SECRET_KEY = "<Dont send your secret key to anyone>";
2224

23-
bucketName = "csharpbucket";
24-
DEMO_DOMAIN = "csharpbucket.dn.qbox.me";
25-
localFile = "Resource/gogopher.jpg";
25+
bucketName = "yourbucket";
26+
DEMO_DOMAIN = bucketName + ".qiniudn.com";
2627
key = "gogopher.jpg";
28+
localFile = "Resource/gogopher.jpg";
29+
bigkey = key;
30+
bigFile = localFile;
2731

2832
conn = new DigestAuthClient();
2933
rs = new RSService(conn, bucketName);
30-
imageOp = new ImageOp(conn);
3134

3235
MkBucket();
3336
RSClientPutFile();
34-
Get();
35-
Stat();
36-
Publish();
37-
UnPublish();
38-
Delete();
37+
Get(key);
38+
ResumablePutFile();
39+
Stat(bigkey);
40+
Delete(key);
3941
Drop();
4042

4143
MkBucket();
4244
RSPutFile();
43-
Publish();
4445
ImageOps();
4546

47+
MakeDownloadToken();
48+
4649
Console.ReadLine();
4750
}
4851

@@ -70,52 +73,45 @@ public static void RSPutFile()
7073

7174
public static void RSClientPutFile()
7275
{
73-
Console.WriteLine("\n==> PutAuth");
74-
PutAuthRet putAuthRet = rs.PutAuth();
75-
PrintRet(putAuthRet);
76-
if (putAuthRet.OK)
77-
{
78-
Console.WriteLine("Expires: " + putAuthRet.Expires.ToString());
79-
Console.WriteLine("Url: " + putAuthRet.Url);
80-
}
81-
else
82-
{
83-
Console.WriteLine("Failed to PutAuth");
84-
}
76+
Console.WriteLine("\n===> RSClient Generate UpToken");
77+
var authPolicy = new AuthPolicy(bucketName, 3600);
78+
string upToken = authPolicy.MakeAuthTokenString();
79+
Console.WriteLine("upToken: " + upToken);
8580

86-
Console.WriteLine("\n===> RSClient.PutFile");
87-
PutFileRet putFileRet = RSClient.PutFile(putAuthRet.Url, bucketName, key, null, localFile, null, "key=<key>");
81+
Console.WriteLine("\n===> RSClient.PutFileWithUpToken");
82+
PutFileRet putFileRet = RSClient.PutFileWithUpToken(upToken, bucketName, key, null, localFile, null, "key=<key>");
8883
PrintRet(putFileRet);
8984
if (putFileRet.OK)
9085
{
9186
Console.WriteLine("Hash: " + putFileRet.Hash);
9287
}
9388
else
9489
{
95-
Console.WriteLine("Failed to RSClient.PutFile");
90+
Console.WriteLine("Failed to RSClient.PutFileWithUpToken");
9691
}
92+
}
9793

98-
Console.WriteLine("\n===> Generate UpToken");
94+
public static void ResumablePutFile()
95+
{
96+
Console.WriteLine("\n===> ResumablePut.PutFile");
9997
var authPolicy = new AuthPolicy(bucketName, 3600);
10098
string upToken = authPolicy.MakeAuthTokenString();
101-
Console.WriteLine("upToken: " + upToken);
102-
103-
Console.WriteLine("\n===> RSClient.PutFileWithUpToken");
104-
putFileRet = RSClient.PutFileWithUpToken(upToken, bucketName, key, null, localFile, null, "key=<key>");
99+
PutAuthClient client = new PutAuthClient(upToken);
100+
PutFileRet putFileRet = ResumablePut.PutFile(client, bucketName, bigkey, null, bigFile, null, "key=<key>");
105101
PrintRet(putFileRet);
106102
if (putFileRet.OK)
107103
{
108104
Console.WriteLine("Hash: " + putFileRet.Hash);
109105
}
110106
else
111107
{
112-
Console.WriteLine("Failed to RSClient.PutFileWithUpToken");
108+
Console.WriteLine("Failed to ResumablePut.PutFile");
113109
}
114110
}
115111

116-
public static void Get()
112+
public static void Get(string key)
117113
{
118-
Console.WriteLine("\n===> Get");
114+
Console.WriteLine("\n===> RSService.Get");
119115
GetRet getRet = rs.Get(key, "attName");
120116
PrintRet(getRet);
121117
if (getRet.OK)
@@ -130,7 +126,7 @@ public static void Get()
130126
Console.WriteLine("Failed to Get");
131127
}
132128

133-
Console.WriteLine("\n===> GetIfNotModified");
129+
Console.WriteLine("\n===> RSService.GetIfNotModified");
134130
getRet = rs.GetIfNotModified(key, "attName", getRet.Hash);
135131
PrintRet(getRet);
136132
if (getRet.OK)
@@ -146,9 +142,9 @@ public static void Get()
146142
}
147143
}
148144

149-
public static void Stat()
145+
public static void Stat(string key)
150146
{
151-
Console.WriteLine("\n===> Stat");
147+
Console.WriteLine("\n===> RSService.Stat");
152148
StatRet statRet = rs.Stat(key);
153149
PrintRet(statRet);
154150
if (statRet.OK)
@@ -164,9 +160,9 @@ public static void Stat()
164160
}
165161
}
166162

167-
public static void Delete()
163+
public static void Delete(string key)
168164
{
169-
Console.WriteLine("\n===> Delete");
165+
Console.WriteLine("\n===> RSService.Delete");
170166
CallRet deleteRet = rs.Delete(key);
171167
PrintRet(deleteRet);
172168
if (!deleteRet.OK)
@@ -177,7 +173,7 @@ public static void Delete()
177173

178174
public static void Drop()
179175
{
180-
Console.WriteLine("\n===> Drop");
176+
Console.WriteLine("\n===> RSService.Drop");
181177
CallRet dropRet = rs.Drop();
182178
PrintRet(dropRet);
183179
if (!dropRet.OK)
@@ -186,32 +182,19 @@ public static void Drop()
186182
}
187183
}
188184

189-
public static void Publish()
185+
public static void MakeDownloadToken()
190186
{
191-
Console.WriteLine("\n===> Publish");
192-
CallRet publishRet = rs.Publish(DEMO_DOMAIN);
193-
PrintRet(publishRet);
194-
if (!publishRet.OK)
195-
{
196-
Console.WriteLine("Failed to Publish");
197-
}
198-
}
199-
200-
public static void UnPublish()
201-
{
202-
Console.WriteLine("\n===> UnPublish");
203-
CallRet publishRet = rs.Unpublish(DEMO_DOMAIN);
204-
PrintRet(publishRet);
205-
if (!publishRet.OK)
206-
{
207-
Console.WriteLine("Failed to UnPublish");
208-
}
187+
Console.WriteLine("\n===> Auth.MakeDownloadToken");
188+
string pattern = "*/*";
189+
var downloadPolicy = new DownloadPolicy(pattern, 3600);
190+
string dnToken = downloadPolicy.MakeAuthTokenString();
191+
Console.WriteLine("dnToken: " + dnToken);
209192
}
210193

211194
public static void ImageOps()
212195
{
213-
Console.WriteLine("\n===> ImageInfo");
214-
ImageInfoRet infoRet = imageOp.ImageInfo("http://" + DEMO_DOMAIN + "/" + key);
196+
Console.WriteLine("\n===> FileOp.ImageInfo");
197+
ImageInfoRet infoRet = ImageOp.ImageInfo("http://" + DEMO_DOMAIN + "/" + key);
215198
PrintRet(infoRet);
216199
if (infoRet.OK)
217200
{
@@ -225,34 +208,34 @@ public static void ImageOps()
225208
Console.WriteLine("Failed to ImageInfo");
226209
}
227210

228-
Console.WriteLine("\n===> ImageExif");
229-
CallRet exifRet = imageOp.ImageExif("http://" + DEMO_DOMAIN + "/" + key);
211+
Console.WriteLine("\n===> FileOp.ImageExif");
212+
CallRet exifRet = ImageOp.ImageExif("http://" + DEMO_DOMAIN + "/" + key);
230213
PrintRet(exifRet);
231214
if (!exifRet.OK)
232215
{
233216
Console.WriteLine("Failed to ImageExif");
234217
}
235218

236-
Console.WriteLine("\n===> ImageViewUrl");
219+
Console.WriteLine("\n===> FileOp.ImageViewUrl");
237220
ImageViewSpec viewSpec = new ImageViewSpec{Mode = 0, Width = 200, Height= 200};
238-
string viewUrl = imageOp.ImageViewUrl("http://" + DEMO_DOMAIN + "/" + key, viewSpec);
221+
string viewUrl = ImageOp.ImageViewUrl("http://" + DEMO_DOMAIN + "/" + key, viewSpec);
239222
Console.WriteLine("ImageViewUrl 1:" + viewUrl);
240223
viewSpec.Quality = 1;
241224
viewSpec.Format = "gif";
242-
viewUrl = imageOp.ImageViewUrl("http://" + DEMO_DOMAIN + "/" + key, viewSpec);
225+
viewUrl = ImageOp.ImageViewUrl("http://" + DEMO_DOMAIN + "/" + key, viewSpec);
243226
Console.WriteLine("ImageViewUrl 2:" + viewUrl);
244227
viewSpec.Quality = 90;
245228
viewSpec.Sharpen = 10;
246229
viewSpec.Format = "png";
247-
viewUrl = imageOp.ImageViewUrl("http://" + DEMO_DOMAIN + "/" + key, viewSpec);
230+
viewUrl = ImageOp.ImageViewUrl("http://" + DEMO_DOMAIN + "/" + key, viewSpec);
248231
Console.WriteLine("ImageViewUrl 3:" + viewUrl);
249232

250-
Console.WriteLine("\n===> ImageMogrifyUrl");
233+
Console.WriteLine("\n===> FileOp.ImageMogrifyUrl");
251234
ImageMogrifySpec mogrSpec = new ImageMogrifySpec {
252235
Thumbnail = "!50x50r", Gravity = "center", Rotate = 90,
253236
Crop = "!50x50", Quality = 80, AutoOrient = true
254237
};
255-
string mogrUrl = imageOp.ImageMogrifyUrl("http://" + DEMO_DOMAIN + "/" + key, mogrSpec);
238+
string mogrUrl = ImageOp.ImageMogrifyUrl("http://" + DEMO_DOMAIN + "/" + key, mogrSpec);
256239
Console.WriteLine("ImageMogrifyUrl:" + mogrUrl);
257240

258241
Console.WriteLine("\n===> Get");
@@ -269,7 +252,7 @@ public static void ImageOps()
269252
{
270253
Console.WriteLine("Failed to Get");
271254
}
272-
Console.WriteLine("\n===> ImageMogrifySaveAs");
255+
Console.WriteLine("\n===> FileOp.ImageMogrifySaveAs");
273256
PutFileRet saveAsRet = rs.ImageMogrifySaveAs(getRet.Url, mogrSpec, key + ".mogr-save-as");
274257
PrintRet(saveAsRet);
275258
if (saveAsRet.OK)

QBox/Auth/AuthPolicy.cs

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using LitJson;
55
using System.Security.Cryptography;
66
using QBox.RS;
7+
using QBox.Util;
78

89
namespace QBox.Auth
910
{
@@ -12,7 +13,10 @@ public class AuthPolicy
1213
public string Scope { get; set; }
1314
public long Deadline { get; set; }
1415
public string CallbackUrl { get; set; }
15-
public string ReturnUrl { get; set; }
16+
public string CallbackBodyType { get; set; }
17+
public bool Escape { get; set; }
18+
public string AsyncOps { get; set; }
19+
public string ReturnBody { get; set; }
1620

1721
public AuthPolicy(string scope, long expires)
1822
{
@@ -28,43 +32,22 @@ public string Marshal()
2832
JsonData data = new JsonData();
2933
data["scope"] = Scope;
3034
data["deadline"] = Deadline;
31-
if (CallbackUrl != null)
35+
if (!String.IsNullOrEmpty(CallbackUrl))
3236
data["callbackUrl"] = CallbackUrl;
33-
if (ReturnUrl != null)
34-
data["returnUrl"] = ReturnUrl;
37+
if (!String.IsNullOrEmpty(CallbackBodyType))
38+
data["callbackBodyType"] = CallbackBodyType;
39+
if (Escape)
40+
data["escape"] = 1;
41+
if (!String.IsNullOrEmpty(AsyncOps))
42+
data["asyncOps"] = AsyncOps;
43+
if (!String.IsNullOrEmpty(ReturnBody))
44+
data["returnBody"] = ReturnBody;
3545
return data.ToJson();
3646
}
3747

3848
public byte[] MakeAuthToken()
3949
{
40-
Encoding encoding = Encoding.ASCII;
41-
byte[] accessKey = encoding.GetBytes(Config.ACCESS_KEY);
42-
byte[] secretKey = encoding.GetBytes(Config.SECRET_KEY);
43-
byte[] upToken = null;
44-
try
45-
{
46-
byte[] policyBase64 = encoding.GetBytes(Base64UrlSafe.Encode(Marshal()));
47-
byte[] digestBase64 = null;
48-
using (HMACSHA1 hmac = new HMACSHA1(secretKey))
49-
{
50-
byte[] digest = hmac.ComputeHash(policyBase64);
51-
digestBase64 = encoding.GetBytes(Base64UrlSafe.Encode(digest));
52-
}
53-
using (MemoryStream buffer = new MemoryStream())
54-
{
55-
buffer.Write(accessKey, 0, accessKey.Length);
56-
buffer.WriteByte((byte)':');
57-
buffer.Write(digestBase64, 0, digestBase64.Length);
58-
buffer.WriteByte((byte)':');
59-
buffer.Write(policyBase64, 0, policyBase64.Length);
60-
upToken = buffer.ToArray();
61-
}
62-
}
63-
catch (Exception e)
64-
{
65-
Console.WriteLine(e.ToString());
66-
}
67-
return upToken;
50+
return AuthToken.Make(Marshal());
6851
}
6952

7053
public string MakeAuthTokenString()

QBox/Auth/AuthToken.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Text;
3+
using System.IO;
4+
using System.Security.Cryptography;
5+
using QBox.RS;
6+
using QBox.Util;
7+
8+
namespace QBox.Auth
9+
{
10+
public static class AuthToken
11+
{
12+
public static byte[] Make(string scope)
13+
{
14+
Encoding encoding = Encoding.ASCII;
15+
byte[] accessKey = encoding.GetBytes(Config.ACCESS_KEY);
16+
byte[] secretKey = encoding.GetBytes(Config.SECRET_KEY);
17+
byte[] upToken = null;
18+
try
19+
{
20+
byte[] policyBase64 = encoding.GetBytes(Base64UrlSafe.Encode(scope));
21+
byte[] digestBase64 = null;
22+
using (HMACSHA1 hmac = new HMACSHA1(secretKey))
23+
{
24+
byte[] digest = hmac.ComputeHash(policyBase64);
25+
digestBase64 = encoding.GetBytes(Base64UrlSafe.Encode(digest));
26+
}
27+
using (MemoryStream buffer = new MemoryStream())
28+
{
29+
buffer.Write(accessKey, 0, accessKey.Length);
30+
buffer.WriteByte((byte)':');
31+
buffer.Write(digestBase64, 0, digestBase64.Length);
32+
buffer.WriteByte((byte)':');
33+
buffer.Write(policyBase64, 0, policyBase64.Length);
34+
upToken = buffer.ToArray();
35+
}
36+
}
37+
catch (Exception e)
38+
{
39+
Console.WriteLine(e.ToString());
40+
}
41+
return upToken;
42+
}
43+
44+
}
45+
}

0 commit comments

Comments
 (0)