zoukankan      html  css  js  c++  java
  • C#调用百度云存储接口上传文件

    因前几日见园子里有人说可以把网站静态文件放在百度上,于是去百度开放平台看了看,发现之前那篇文章不是调的云存储接口啊。。。

    于是自己写了个C#能调百度云存储的例子(百度云开放平台只提供php、java、Python、c++),下面贴一下步骤和代码,还请多多指点啊!

    一、libcurl.NET

     

    百度提供的四种途径我们选择的是curl方式,但是大家都知道curl需要linux环境支持,这里引用了国外的libcurl.NET开源项目。下载最新版本的libcurlnet-1_3.zip,解压后将这(ca-bundle.crt、libcurl.dll、LibCurlNet.dll、LibCurlShim.dll)四个文件放在你自己项目的bin目录下。

    然后项目里添加LibCurlNet.dll引用。

    二、签名算法

    百度的URL签名算法说是用的sha1加密,千万别理解错!这里我是从php的hash_hmac算法转的,代码如下:

     1 /// <summary>
     2         /// hmacSha1算法加密
     3         /// </summary>
     4         /// <param name="encryptText">加密明文</param>
     5         /// <param name="encryptKey">加密密钥</param>
     6         /// <returns></returns>
     7         public static string hmacSha1(string encryptText, string encryptKey)
     8         {
     9             HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.Default.GetBytes(encryptKey));
    10             byte[] RstRes = myHMACSHA1.ComputeHash(Encoding.Default.GetBytes(encryptText));
    11             return Convert.ToBase64String(RstRes);
    12         }
    View Code

    三、实现curl上传

    其实就是要将下面这段命令翻译成C#

    curl -T bs.txt -v 
    "http://bcs.duapp.com/bucket/testobject?sign=MBO:f:PVaOdaf%2BxNoC7aXRqL%2FJq5Kp7a4%3D"
    

      废话不多直接上代码

      1 using System;
      2 using System.Collections.Generic;
      3 using System.IO;
      4 using System.Linq;
      5 using System.Security.Cryptography;
      6 using System.Text;
      7 using System.Web;
      8 using SeasideResearch.LibCurlNet;
      9 
     10 namespace BaiduCloudStorage
     11 {
     12     class Program
     13     {
     14         static void Main(string[] args)
     15         {
     16             string flag = "MBO";
     17             string accessKey = "83ee44XXXX0799394e9fXXXXa731XXXX";
     18             string secrectKey = "0Aa2XXXXdff516c44XXXX9e1ef0XXXX1";
     19             string bucket = "sitefiles", sobject = "Media/80cb39dbb6fd526694279e79aa18972bd407364a.jpg";
     20             string content = flag + "
    ";
     21             content += "Method=PUT
    ";
     22             content += "Bucket=sitefiles
    ";
     23             content += "Object=/" + sobject + "
    ";
     24             //content += "Ip=115.173.192.3
    ";
     25             //content += "Time=2014-01-13
    ";
     26             //content += "Size=1024
    "; // B字节
     27 
     28             string signture = flag + ":" + accessKey + ":" + HttpUtility.UrlEncode(hmacSha1(content, secrectKey));
     29             string url = "http://bcs.duapp.com/" + bucket + "/" + HttpUtility.UrlEncode(sobject) + "?sign=" + signture;
     30 
     31             FileStream fs = new FileStream(@"D:Media9e1e73600c338744337ee82c500fd9f9d62aa0e6.jpg", FileMode.Open, FileAccess.Read, FileShare.Read);
     32 
     33             try
     34             {
     35                 Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
     36 
     37                 Easy easy = new Easy();
     38                 Easy.ReadFunction rf = new Easy.ReadFunction(OnReadData);
     39                 easy.SetOpt(CURLoption.CURLOPT_READFUNCTION, rf);
     40                 easy.SetOpt(CURLoption.CURLOPT_READDATA, fs);
     41 
     42                 Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);
     43                 easy.SetOpt(CURLoption.CURLOPT_URL, url);
     44                 easy.SetOpt(CURLoption.CURLOPT_UPLOAD, 1);
     45                 easy.SetOpt(CURLoption.CURLOPT_INFILESIZE, fs.Length);
     46 
     47                 easy.SetOpt(CURLoption.CURLOPT_VERBOSE, 1);
     48                 easy.SetOpt(CURLoption.CURLOPT_WRITEDATA, wf);
     49 
     50                 Easy.DebugFunction df = new Easy.DebugFunction(OnDebug);
     51                 easy.SetOpt(CURLoption.CURLOPT_DEBUGFUNCTION, df);
     52                 easy.SetOpt(CURLoption.CURLOPT_VERBOSE, true);
     53 
     54                 easy.Perform();
     55                 easy.Cleanup();
     56                 fs.Close();
     57 
     58                 Curl.GlobalCleanup();
     59 
     60             }
     61             catch (Exception ex)
     62             {
     63                 Console.WriteLine(ex);
     64             }
     65 
     66             Console.ReadKey();
     67         }
     68 
     69         public static Int32 OnReadData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
     70         {
     71             FileStream fs = (FileStream)extraData;
     72             return fs.Read(buf, 0, size * nmemb);
     73         }
     74 
     75         public static void OnDebug(CURLINFOTYPE infoType, String msg, Object extraData)
     76         {
     77             // Console.WriteLine(msg);
     78         }
     79 
     80         public static Int32 OnProgress(Object extraData, Double dlTotal, Double dlNow, Double ulTotal, Double ulNow)
     81         {
     82             Console.WriteLine("Progress: {0} {1} {2} {3}", dlTotal, dlNow, ulTotal, ulNow);
     83             return 0; // standard return from PROGRESSFUNCTION
     84         }
     85 
     86         public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
     87         {
     88             Console.Write(System.Text.Encoding.UTF8.GetString(buf));
     89             return size * nmemb;
     90         }
     91 
     92         /// <summary>
     93         /// hmacSha1算法加密
     94         /// </summary>
     95         /// <param name="encryptText">加密明文</param>
     96         /// <param name="encryptKey">加密密钥</param>
     97         /// <returns></returns>
     98         public static string hmacSha1(string encryptText, string encryptKey)
     99         {
    100             HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.Default.GetBytes(encryptKey));
    101             byte[] RstRes = myHMACSHA1.ComputeHash(Encoding.Default.GetBytes(encryptText));
    102             return Convert.ToBase64String(RstRes);
    103         }
    104     }
    105 }
    View Code

    至于libcurl.NET具体咋用,我这儿真没什么资料,都谷歌上查的,兄弟们有相关资料的给留个地址啊,我这还想继续完善完善啊。。。

  • 相关阅读:
    不规范的json文档 转化成 java 对象的处理
    财经接口
    Back-off pulling image "registry.access.redhat.com/rhel7/pod-infrastructure:latest
    VMware Workstation 14 Pro永久激活密钥
    Angular2入门:TypeScript的装饰器
    Angular2入门:TypeScript的模块
    Angular2入门:TypeScript的类
    51nod“省选”模测第二场 B 异或约数和(数论分块)
    51nod1238 最小公倍数之和 V3(莫比乌斯反演)
    cf1139D. Steps to One(dp)
  • 原文地址:https://www.cnblogs.com/sheng9hhd/p/3519685.html
Copyright © 2011-2022 走看看