Overview
The OSS SDK provides SDKs that integrate the signature function and the upload and download function. However, in practice, you may need to use APIs to upload and download objects with a signature in mind. This topic uses the PutObject API as an example to describe how to upload and download objects in C#.
Note: We recommend that you use the OSS SDK. This topic provides an example of how to use a signature to upload objects. You can modify the business code in actual use.
Detail
The sample code for using the PutObject API in C# is as follows:
Note: The following example is implemented in the Visual Studio 2017 tool and NET Framework4.0 package.
using System; using System.Security.Cryptography; using System.Text; using System.Net; using System.IO; using System.Reflection;
namespace Aliyun.OSS.Samples { class Program {
//Your AccessKey public static string AccessKey = "xx"; //Your AccessKeySecret public static string AccessKeySecret = "xx"; //bucket endpoint public static string Endpoint = "oss-cn-hangzhou.aliyuncs.com"; public static string BucketName = "xx"; public static string objectName = "mytest/1.txt"; public static string HmacSha1Sign(string secret, string strOrgData) { var hmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(secret)); var dataBuffer = Encoding.UTF8.GetBytes(strOrgData); var hashBytes = hmacsha1.ComputeHash(dataBuffer); return Convert.ToBase64String(hashBytes); }
public static string HttpRequest(string url, string data, string sign, string contentType, string time1) {
byte[] datas = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "PUT"; request.Timeout = 150000; request.Headers.Add("Authorization", "OSS " + AccessKey + ":" + sign); CallPrivateMethod(request, "SetSpecialHeaders", "Date", time1); request.ContentType = contentType; Stream requestStream = null; string responseStr = null; try { if (datas != null) { request.ContentLength = datas.Length; requestStream = request.GetRequestStream(); requestStream.Write(datas, 0, datas.Length); requestStream.Close(); } else { request.ContentLength = 0; } HttpWebResponse response = request.GetResponse() as HttpWebResponse; responseStr = response.Headers.GetValues("x-oss-request-id")[0]; //responseStr = sr.ReadToEnd(); } catch (Exception) { Console.WriteLine("error"); } finally { request = null; requestStream = null; } return responseStr; }
public static string ToGMTString() { return DateTime.Now.ToUniversalTime().ToString("r"); } public static void CallPrivateMethod(object instance, string name, params object[] param) { BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic; Type type = instance.GetType(); MethodInfo method = type.GetMethod(name, flag); method.Invoke(instance, param); }
static void Main(string[] args) {
string varb = "PUT"; string content_type = "application/json"; string timeGmt = ToGMTString(); string str = varb + "\n\n" + content_type + "\n" + timeGmt + "\n/" + BucketName + "/" + objectName; string signature = HmacSha1Sign(AccessKeySecret, str); string url = "http://" + BucketName + "." + Endpoint + "/" + objectName; string data = "{ \"key\":\"this is a oss's test\"}"; string result = HttpRequest(url, data, signature, content_type, timeGmt); Console.WriteLine("requestId: " + result);
} } }
Documentation
For more information about how to use the OSS SDK, see OSS add signatures to headers.
Scope
- OSS