Qiniu 七牛问题解答
很多人反应上传凭证自己书写的时候出现很多问题。这里我给大家一个范例
问题解决方案
此段代码为c#案例,生成token
ak,sk需要创建七牛账户,并在空间秘钥中获取。
public String getToken()
{
Policy policy = new Policy("liuhanlin-work");
// policy.Deadline=1390528576;
// policy.PersistentNotifyUrl="";
// policy.Scope = "liuhanlin-work:-123456.jpg";
// policy.PersistentOps="avthumb/mp4";
// policy.Scope = "liuhanlin-work";
// policy.SaveKey = "Chrysanthemum,c_fill,w_60,h_90.jpg";
// policy.InsertOnly = 1;
policy.ReturnBody = "location=$(x:hello)";
Putpolicy = policy.ToString();
System.Console.WriteLine(Putpolicy);
encodedPutPolicy = setting.Bs64(Putpolicy);
System.Console.WriteLine(encodedPutPolicy);
encodedSign=setting.token_sign(encodedPutPolicy);
System.Console.WriteLine(encodedSign);
Token = setting.AK + ‘:‘+ encodedSign + ":"+ encodedPutPolicy;
return Token;
}
签名方法token_sign
public static string token_sign(string s)
{
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
byte[] bytesSK = encoding.GetBytes(SK);
HMACSHA1 hmac = new HMACSHA1(bytesSK);
byte[] ss = encoding.GetBytes(s);
byte[] digest = hmac.ComputeHash(ss);
s = Convert.ToBase64String(digest);
s = s.Replace(‘+‘, ‘-‘).Replace(‘/‘, ‘_‘);
return s;
}
上传策略policy类的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
namespace WebApplication1.IO
{
public class Policy
{
private string scope = "";
public string Scope
{
get { return scope; }
set { scope = value; }
}
private Int64 deadline = 0;
public Int64 Deadline
{
get { return deadline; }
set { deadline = value; }
}
private int insertOnly = 0;
public int InsertOnly
{
get { return insertOnly; }
set { insertOnly = value; }
}
private string saveKey = "";
public string SaveKey
{
get { return saveKey; }
set { saveKey = value; }
}
private string endUser = "";
public string EndUser
{
get { return endUser; }
set { endUser = value; }
}
private string returnUrl = "";
public string ReturnUrl
{
get { return returnUrl; }
set { returnUrl = value; }
}
private string returnBody = "";
public string ReturnBody
{
get { return returnBody; }
set { returnBody = value; }
}
private string callbackUrl = "";
public string CallbackUrl
{
get { return callbackUrl; }
set { callbackUrl = value; }
}
private string callbackBody = "";
public string CallbackBody
{
get { return callbackBody; }
set { callbackBody = value; }
}
private string persistentOps = "";
public string PersistentOps
{
get { return persistentOps; }
set { persistentOps = value; }
}
private string persistentNotifyUrl = "";
public string PersistentNotifyUrl
{
get { return persistentNotifyUrl; }
set { persistentNotifyUrl = value; }
}
private string persistentPipeline = "";
public string PersistentPipeline
{
get { return persistentPipeline; }
set { persistentPipeline = value; }
}
private string mimeLimit = "";
public string MimeLimit
{
get { return mimeLimit; }
set { mimeLimit = value; }
}
private int detectMime = 0;
public int DetectMime
{
get { return detectMime; }
set { detectMime = value; }
}
private Int64 fsizeLimit = 0;
public Int64 FsizeLimit
{
get { return fsizeLimit; }
set { fsizeLimit = value; }
}
private UInt32 expires = 0;
public UInt32 Expires
{
get { return expires; }
set { expires = value; }
}
private UInt32 callbackFetchKey = 0;
public UInt32 CallbackFetchKey
{
get { return callbackFetchKey; }
set { callbackFetchKey = value; }
}
private string callbackBodyType = "";
public string CallbackBodyType
{
get { return callbackBodyType; }
set { callbackBodyType = value; }
}
public Policy(string scope, UInt32 exp = 3600)
{
Scope = scope;
this.Expires = exp;
this.deadline = (UInt32)((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000)/10000000 + (long)exp);
}
public override string ToString()
{
return setting.setjson(this);
}
}
}
结果演示
结果这里不演示了,代码是可以直接用的。已经测试无数遍了。有需要的客户可以按照这样去自己写一遍,其实七牛有很完备的sdk去封装大部分的功能。
sdk地址 : http://developer.qiniu.com/docs/v6/sdk/
各种语言都有。
时间: 2024-10-09 21:00:25