接get方法后,提供V5 COS API封装put方法,用于文件的上传操作。
//1.参数定义
string SecretId = "这里输入您的SecretId";//SecretId替换成自己的
string SecretKey = "这里输入您的SecretKey";//SecretKey替换成自己的
string q_sign_algorithm = "sha1";
string q_ak = SecretId;
string q_sign_time = GetTimeStamp(DateTime.UtcNow) + ";" + GetTimeStamp(DateTime.UtcNow.AddHours(1));//签名有效期1个小时
string q_key_time = GetTimeStamp(DateTime.UtcNow) + ";" + GetTimeStamp(DateTime.UtcNow.AddHours(1));//签名有效期1个小时;
string q_header_list = "";
string q_url_param_list = "";
string q_signature = "";
string key = "/1.jpg";//上传的object名称为1.jpg
string method = "put";
//2.签名计算
string SignKey = ComputeHash(Encoding.ASCII.GetBytes(SecretKey), q_key_time);//对签名的有效起止时间加密计算值SignKey
string HttpString = method+"\n" + key + "\n\n\n";//根据固定格式组合生成
string StringToSign = q_sign_algorithm+"\n"+ q_sign_time + "\n"+ GetSH1(HttpString) + "\n";//加密 HttpString,并根据固定格式组合生成 StringToSign
string Signature = ComputeHash(Encoding.ASCII.GetBytes(SignKey), StringToSign);//加密 StringToSign,生成 Signature
string url = "http://{bucket}-{appid}.cos.ap-beijing.myqcloud.com" + key + "?q-sign-algorithm=" + q_sign_algorithm + "&q-ak=" + q_ak + "&q-sign-time=" + q_sign_time + "&q-key-time=" + q_key_time + "&q-header-list=&q-url-param-list=&q-signature=" + Signature;//把{bucket} {appid} 都替换成自己的
string data = "123456";
//3.上传对象
try
{
Put(url, data);
Response.Write("put上传完成");
}
catch (Exception ex)
{
Response.Write("put上传失败:" + ex.Message);
}
//4.工具方法
/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public static string GetTimeStamp(DateTime dt)
{
TimeSpan ts = dt - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds).ToString();
}
/// <summary>
/// 计算哈希
/// </summary>
/// <param name="key"></param>
/// <param name="input"></param>
/// <returns></returns>
public static string ComputeHash(byte[] key, string input)
{
HMACSHA1 myhmacsha1 = new HMACSHA1(key);
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(input);
MemoryStream stream = new MemoryStream(byteArray);
return myhmacsha1.ComputeHash(stream).Aggregate("", (s, e) => s + String.Format("{0:x2}", e), s => s);
}
/// <summary>
/// SHA1
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string GetSH1(string value)
{
SHA1 algorithm = SHA1.Create();
byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(value));
string sh1 = "";
for (int i = 0; i < data.Length; i++)
{
sh1 += data[i].ToString("x2").ToLower();
}
return sh1;
}
#region Put方式
private static string Put(string url, string data)
{
return CommonHttpRequest(url, "PUT", data);
}
#endregion
#region 私有方法
private static string CommonHttpRequest(string url, string type, string data = "")
{
HttpWebRequest myRequest = null;
Stream outstream = null;
HttpWebResponse myResponse = null;
StreamReader reader = null;
try
{
//构造http请求的对象
myRequest = (HttpWebRequest)WebRequest.Create(url);
//设置
myRequest.ProtocolVersion = HttpVersion.Version10;
myRequest.Method = type;
if (data.Trim() != "")
{
myRequest.ContentType = "text/xml";
myRequest.ContentLength = data.Length;
//转成网络流
byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
outstream = myRequest.GetRequestStream();
outstream.Flush();
outstream.Write(buf, 0, buf.Length);
outstream.Flush();
outstream.Close();
}
// 获得接口返回值
myResponse = (HttpWebResponse)myRequest.GetResponse();
reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string ReturnXml = reader.ReadToEnd();
reader.Close();
myResponse.Close();
myRequest.Abort();
return ReturnXml;
}
catch (Exception ex)
{
// throw new Exception();
if (outstream != null) outstream.Close();
if (reader != null) reader.Close();
if (myResponse != null) myResponse.Close();
if (myRequest != null) myRequest.Abort();
return ex.Message;
}
}
#endregion
抓包看下,上传成功。
以下是原始代码