Skip to content

Commit a4dc95b

Browse files
committed
init
1 parent 04985a1 commit a4dc95b

17 files changed

+1553
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@
2121
*.i*86
2222
*.x86_64
2323
*.hex
24+
25+
obj
26+
bin
27+
qiniu-csharp-sdk.userprefs

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,67 @@ qiniu-csharp-sdk
22
================
33

44
七牛云存储SDK
5+
6+
此SDK实现了七牛云存储的核心部分,即文件上传,目的是简化文件上传并提供更加便捷的编程接口,从更高层面进行了抽象,而非官方SDK那样,仅是对API的一套封装。
7+
8+
项目地址: [https://github.com/icattlecoder/qiniu-csharp-sdk](https://github.com/icattlecoder/qiniu-csharp-sdk)
9+
10+
- [初始化](#init)
11+
- [上传文件](#upload)
12+
- [上传事件](#event)
13+
- [上传结果](#result)
14+
- [文件操作](#rsop)
15+
- [查看信息](#stat)
16+
- [删除](#delete)
17+
- [问题反馈](#issue)
18+
19+
<a id="init"></a>
20+
# 初始化
21+
22+
初始化工作包括对七牛的API Keys的赋值,如:
23+
```c#
24+
qiniu.Config.ACCESS_KEY = "IT9iP3J9wdXXYsT1p8ns0gWD-CQOdLvIQuyE0FOK";
25+
qiniu.Config.SECRET_KEY = "zUCzekBtEqTZ4-WJPCGlBrr2PeyYxsYn98LPaivM";
26+
```
27+
<a id="upload"></a>
28+
# 上传文件
29+
30+
```c#
31+
QiniuFile qfile = new QiniuFile ("<input your bucket name>", "<input qiniu file key>", "<local disk file path>");
32+
qfile.Upload();
33+
```
34+
35+
一个QiniuFile对象表示一个七牛云空间的文件,初始化QiniuFile提供以下三个参数:
36+
- `bucketName`,七牛云空间名称
37+
- `key`,七牛文件key
38+
- `localfile`,本地文件。该参数为可选,如果要上传本地的文件至七牛云空间,则需要指定此参数的值。
39+
40+
注意上传为异步操作,上传的结果由事件通知。
41+
42+
<a id="event"></a>
43+
## 上传事件
44+
45+
共包括五个事件
46+
47+
事件名称 | 说明
48+
:--------- | :---------
49+
UploadCompleted; | 上传完成
50+
UploadFailed; | 上传失败
51+
UploadProgressChanged; | 上传进度
52+
UploadBlockCompleted; | 上传块完成
53+
UploadBlockFailed; | 上传块失败
54+
55+
前三个事件比较容易理解,后两个事件是根据七牛的大文件上传机制衍生出来的,合理利用这两个事件可以完成大文件分块上传结果持久化。
56+
57+
<a id="result"></a>
58+
## 上传结果
59+
成功上传一个文件后,结果通过事件`uploadCompleted`获取得到,包括文件的`Hash``Key`以及从七牛云存储返回的原始字符串(主要考虑到上传凭证中指定了自定义的returnBody)。
60+
61+
<a id="rsop"></a>
62+
## 文件操作
63+
简单的实现了文件的基本信息获取及删除操作,分别为`Stat``Delete`
64+
65+
<a id="issue"></a>
66+
# 问题反馈
67+
[https://github.com/icattlecoder/qiniu-csharp-sdk/issues/new](https://github.com/icattlecoder/qiniu-csharp-sdk/issues/new)
68+

demo/Program.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using qiniu;
3+
using System.Threading;
4+
5+
namespace demo
6+
{
7+
class MainClass
8+
{
9+
public static void Main (string[] args)
10+
{
11+
// 初始化qiniu配置,主要是API Keys
12+
qiniu.Config.ACCESS_KEY = "IT9iP3J9wdXXYsT1p8ns0gWD-CQOdLvIQuyE0FOi";
13+
qiniu.Config.SECRET_KEY = "zUCzekBtEqTZ4-WJPCGlBrr2PeyYxsYn98LxaivM";
14+
15+
/**********************************************************************
16+
可以用下面的方法从配置文件中初始化
17+
qiniu.Config.InitFromAppConfig ();
18+
**********************************************************************/
19+
20+
21+
//======================================================================
22+
{
23+
QiniuFile qfile = new QiniuFile ("<input your bucket name>", "<input qiniu file key>", "<local disk file path");
24+
ManualResetEvent done = new ManualResetEvent (false);
25+
qfile.UploadCompleted += (sender, e) => {
26+
Console.WriteLine (e.RawString);
27+
done.Set ();
28+
};
29+
qfile.UploadFailed += (sender, e) => {
30+
Console.WriteLine (e.Error.ToString ());
31+
done.Set ();
32+
};
33+
qfile.UploadProgressChanged += (sender, e) => {
34+
int percentage = (int)(100 * e.BytesSent / e.TotalBytes);
35+
Console.Write (percentage);
36+
};
37+
// 上传为异步操作
38+
//上传本地文件到七牛云存储
39+
qfile.Upload ();
40+
done.WaitOne ();
41+
}
42+
43+
//======================================================================
44+
{
45+
46+
try {
47+
QiniuFile qfile = new QiniuFile ("<input your bucket Name>", "<input qiniu file key>");
48+
QiniuFileInfo finfo = qfile.Stat ();
49+
if (finfo != null) {
50+
//删除七牛云空间的文件
51+
qfile.Delete ();
52+
}
53+
} catch (QiniuWebException e) {
54+
Console.WriteLine (e.Error.HttpCode);
55+
Console.WriteLine (e.Error.ToString ());
56+
}
57+
}
58+
}
59+
}
60+
}

demo/Properties/AssemblyInfo.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
4+
// Information about this assembly is defined by the following attributes.
5+
// Change them to the values specific to your project.
6+
[assembly: AssemblyTitle ("demo")]
7+
[assembly: AssemblyDescription ("")]
8+
[assembly: AssemblyConfiguration ("")]
9+
[assembly: AssemblyCompany ("")]
10+
[assembly: AssemblyProduct ("")]
11+
[assembly: AssemblyCopyright ("icattlecoder")]
12+
[assembly: AssemblyTrademark ("")]
13+
[assembly: AssemblyCulture ("")]
14+
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
15+
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
16+
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
17+
[assembly: AssemblyVersion ("1.0.*")]
18+
// The following attributes are used to specify the signing key for the assembly,
19+
// if desired. See the Mono documentation for more information about signing.
20+
//[assembly: AssemblyDelaySign(false)]
21+
//[assembly: AssemblyKeyFile("")]
22+

demo/demo.csproj

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>10.0.0</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{4F38040D-651B-4273-AE85-D96BC59C4698}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<RootNamespace>demo</RootNamespace>
11+
<AssemblyName>demo</AssemblyName>
12+
</PropertyGroup>
13+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
14+
<DebugSymbols>true</DebugSymbols>
15+
<DebugType>full</DebugType>
16+
<Optimize>false</Optimize>
17+
<OutputPath>bin\Debug</OutputPath>
18+
<DefineConstants>DEBUG;</DefineConstants>
19+
<ErrorReport>prompt</ErrorReport>
20+
<WarningLevel>4</WarningLevel>
21+
<Externalconsole>true</Externalconsole>
22+
</PropertyGroup>
23+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
24+
<DebugType>full</DebugType>
25+
<Optimize>true</Optimize>
26+
<OutputPath>bin\Release</OutputPath>
27+
<ErrorReport>prompt</ErrorReport>
28+
<WarningLevel>4</WarningLevel>
29+
<Externalconsole>true</Externalconsole>
30+
</PropertyGroup>
31+
<ItemGroup>
32+
<Reference Include="System" />
33+
</ItemGroup>
34+
<ItemGroup>
35+
<Compile Include="Program.cs" />
36+
<Compile Include="Properties\AssemblyInfo.cs" />
37+
</ItemGroup>
38+
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
39+
<ItemGroup>
40+
<ProjectReference Include="..\sdk\qiniu.csproj">
41+
<Project>{8D120ACA-4BF9-42A7-81C6-8BE39FF5ED42}</Project>
42+
<Name>qiniu</Name>
43+
</ProjectReference>
44+
</ItemGroup>
45+
</Project>

qiniu-csharp-sdk.sln

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 11.00
3+
# Visual Studio 2010
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "qiniu", "sdk\qiniu.csproj", "{8D120ACA-4BF9-42A7-81C6-8BE39FF5ED42}"
5+
EndProject
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "demo", "demo\demo.csproj", "{4F38040D-651B-4273-AE85-D96BC59C4698}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{4F38040D-651B-4273-AE85-D96BC59C4698}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{4F38040D-651B-4273-AE85-D96BC59C4698}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{4F38040D-651B-4273-AE85-D96BC59C4698}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{4F38040D-651B-4273-AE85-D96BC59C4698}.Release|Any CPU.Build.0 = Release|Any CPU
18+
{8D120ACA-4BF9-42A7-81C6-8BE39FF5ED42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{8D120ACA-4BF9-42A7-81C6-8BE39FF5ED42}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{8D120ACA-4BF9-42A7-81C6-8BE39FF5ED42}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{8D120ACA-4BF9-42A7-81C6-8BE39FF5ED42}.Release|Any CPU.Build.0 = Release|Any CPU
22+
EndGlobalSection
23+
GlobalSection(MonoDevelopProperties) = preSolution
24+
StartupItem = qiniu.csproj
25+
EndGlobalSection
26+
EndGlobal

sdk/Properties/AssemblyInfo.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
4+
// Information about this assembly is defined by the following attributes.
5+
// Change them to the values specific to your project.
6+
[assembly: AssemblyTitle ("qiniu-csharp-sdk")]
7+
[assembly: AssemblyDescription ("")]
8+
[assembly: AssemblyConfiguration ("")]
9+
[assembly: AssemblyCompany ("")]
10+
[assembly: AssemblyProduct ("")]
11+
[assembly: AssemblyCopyright ("icattlecoder")]
12+
[assembly: AssemblyTrademark ("")]
13+
[assembly: AssemblyCulture ("")]
14+
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
15+
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
16+
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
17+
[assembly: AssemblyVersion ("1.0.*")]
18+
// The following attributes are used to specify the signing key for the assembly,
19+
// if desired. See the Mono documentation for more information about signing.
20+
//[assembly: AssemblyDelaySign(false)]
21+
//[assembly: AssemblyKeyFile("")]
22+

sdk/QiniuConf.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace qiniu
5+
{
6+
/// <summary>
7+
/// Config.
8+
/// </summary>
9+
public class Config
10+
{
11+
public static string USER_AGENT = "qiniu-csharp-sdk icattlecoder v1.0.0";
12+
#region 帐户信息
13+
/// <summary>
14+
/// 七牛提供的公钥,用于识别用户
15+
/// </summary>
16+
public static string ACCESS_KEY = "<Please apply your access key>";
17+
/// <summary>
18+
/// 七牛提供的秘钥,不要在客户端初始化该变量
19+
/// </summary>
20+
public static string SECRET_KEY = "<Dont send your secret key to anyone>";
21+
#endregion
22+
23+
#region 七牛服务器地址
24+
/// <summary>
25+
/// 七牛资源管理服务器地址
26+
/// </summary>
27+
public static string RS_HOST = "http://rs.Qbox.me";
28+
/// <summary>
29+
/// 七牛资源上传服务器地址.
30+
/// </summary>
31+
public static string UP_HOST = "http://up.qiniu.com";
32+
/// <summary>
33+
/// 七牛资源列表服务器地址.
34+
/// </summary>
35+
public static string RSF_HOST = "http://rsf.Qbox.me";
36+
37+
public static string PREFETCH_HOST = "http://iovip.qbox.me";
38+
39+
public static string API_HOST = "http://api.qiniu.com";
40+
#endregion
41+
42+
/// <summary>
43+
/// 初始化七牛帐户、请求地址等信息,不应在客户端调用。
44+
/// </summary>
45+
public static void InitFromAppConfig()
46+
{
47+
USER_AGENT = System.Configuration.ConfigurationManager.AppSettings["USER_AGENT"];
48+
ACCESS_KEY = System.Configuration.ConfigurationManager.AppSettings["ACCESS_KEY"];
49+
SECRET_KEY = System.Configuration.ConfigurationManager.AppSettings["SECRET_KEY"];
50+
RS_HOST = System.Configuration.ConfigurationManager.AppSettings["RS_HOST"];
51+
UP_HOST = System.Configuration.ConfigurationManager.AppSettings["UP_HOST"];
52+
RSF_HOST = System.Configuration.ConfigurationManager.AppSettings["RSF_HOST"];
53+
PREFETCH_HOST = System.Configuration.ConfigurationManager.AppSettings["PREFETCH_HOST"];
54+
}
55+
}
56+
}

sdk/QiniuErrors.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace qiniu
5+
{
6+
public class QiniuErrors
7+
{
8+
/// <summary>
9+
/// See http://developer.qiniu.com/docs/v6/api/reference/codes.html
10+
/// </summary>
11+
public static Dictionary<int,string> ErrorCodes = new Dictionary<int, string> {
12+
{298,"部分操作执行成功"},
13+
{400,"请求报文格式错误"},
14+
{401,"认证授权失败,可能是密钥信息不正确、数字签名错误或授权已超时"},
15+
{404,"资源不存在"},
16+
{405,"请求方式错误,非预期的请求方式"},
17+
{406,"上传的数据 CRC32 校验错"},
18+
{419,"用户账号被冻结"},
19+
{503,"服务端不可用"},
20+
{504,"服务端操作超时"},
21+
{579,"资源上传成功,但是回调失败"},
22+
{599,"服务端操作失败"},
23+
{608,"资源内容被修改"},
24+
{612,"指定资源不存在或已被删除"},
25+
{614,"目标资源已存在"},
26+
{630,"已创建的空间数量达到上限,无法创建新空间"},
27+
{631,"指定空间不存在"},
28+
{701,"上传数据块校验出错"}
29+
};
30+
31+
private int httpCode;
32+
33+
/// <summary>
34+
/// Gets the http code.
35+
/// </summary>
36+
/// <value>The http code.</value>
37+
public int HttpCode {
38+
get { return httpCode; }
39+
}
40+
41+
/// <summary>
42+
/// Initializes a new instance of the <see cref="qiniu.QiniuErrors"/> class.
43+
/// </summary>
44+
/// <param name="code">Code.</param>
45+
public QiniuErrors (int code)
46+
{
47+
this.httpCode = code;
48+
}
49+
50+
/// <summary>
51+
/// Returns a <see cref="System.String"/> that represents the current <see cref="qiniu.QiniuErrors"/>.
52+
/// </summary>
53+
/// <returns>A <see cref="System.String"/> that represents the current <see cref="qiniu.QiniuErrors"/>.</returns>
54+
public override string ToString ()
55+
{
56+
return ErrorCodes [this.httpCode];
57+
}
58+
}
59+
}
60+

0 commit comments

Comments
 (0)