码迷,mamicode.com
首页 > Web开发 > 详细

七牛上传凭证怎样自己写

时间:2015-03-11 14:50:46      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

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/
各种语言都有。

七牛上传凭证怎样自己写

标签:

原文地址:http://blog.csdn.net/guoer9973/article/details/44198275

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!