TosClient 在向服务端发起请求时,默认会对请求 header 里包含签名。SDK 也支持构造带签名的 URL,用户可直接使用该 URL 发起 HTTP 请求,也可以将该 URL 共享给第三方实现访问授权。本文介绍预签名 URL 的 SDK 示例。
以下代码用于生成预签名的 URL,然后使用预签名的 URL 向指定桶中上传对象。
using System; using System.Collections.Generic; using System.IO; using System.Net; using TOS; using TOS.Error; using TOS.Model; namespace ConsoleApp { internal class Program { private static void Main(string[] args) { var ak = Environment.GetEnvironmentVariable("TOS_ACCESS_KEY"); var sk = Environment.GetEnvironmentVariable("TOS_SECRET_KEY"); // endpoint 若没有指定HTTP协议(HTTP/HTTPS),默认使用 HTTPS // Bucket 的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com var endpoint = "https://tos-cn-beijing.volces.com"; var region = "cn-beijing"; // 填写 BucketName var bucketName = "*** Provide your bucket name ***"; // 填写对象名 var objectKey = "*** Provide your object key ***"; var data = "Hello TOS"; // 创建TOSClient实例 var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build(); try { var preSignedURLInput = new PreSignedURLInput() { Bucket = bucketName, Key = objectKey, HttpMethod = HttpMethodType.HttpMethodPut, Header = new Dictionary<string, string>() { { "Content-Type", "text/plain" } } }; var preSignedURLOutput = client.PreSignedURL(preSignedURLInput); // 上传对象 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(preSignedURLOutput.SignedUrl); request.Method = WebRequestMethods.Http.Put; var body = System.Text.Encoding.UTF8.GetBytes(data); request.ContentLength = data.Length; request.ContentType = "text/plain"; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(body, 0, data.Length); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine("PreSignedURL Put Object Status Code {0}", response.StatusCode); response.Close(); } catch (TosServerException ex) { Console.WriteLine("PreSignedURL failed, Request ID {0}", ex.RequestID); Console.WriteLine("PreSignedURL failed, Status Code {0}", ex.StatusCode); Console.WriteLine("PreSignedURL failed, Response Error Code {0}", ex.Code); Console.WriteLine("PreSignedURL failed, Response Error Message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("PreSignedURL failed, Error Message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("PreSignedURL failed, {0}", ex.Message); } } } }
以下代码用于生成预签名的 URL ,然后使用预签名的 URL 向指定桶中下载对象。
using System; using System.Net; using TOS; using TOS.Error; using TOS.Model; namespace ConsoleApp { internal class Program { private static void Main(string[] args) { var ak = Environment.GetEnvironmentVariable("TOS_ACCESS_KEY"); var sk = Environment.GetEnvironmentVariable("TOS_SECRET_KEY"); // endpoint 若没有指定HTTP协议(HTTP/HTTPS),默认使用 HTTPS // Bucket 的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com var endpoint = "https://tos-cn-beijing.volces.com"; var region = "cn-beijing"; // 填写 BucketName var bucketName = "*** Provide your bucket name ***"; // 填写对象名 var objectKey = "*** Provide your object key ***"; var data = "Hello TOS"; // 创建TOSClient实例 var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build(); try { var preSignedURLInput = new PreSignedURLInput() { Bucket = bucketName, Key = objectKey, HttpMethod = HttpMethodType.HttpMethodGet, }; var preSignedURLOutput = client.PreSignedURL(preSignedURLInput); // 获取对象 var request = (HttpWebRequest)WebRequest.Create(preSignedURLOutput.SignedUrl); request.Method = WebRequestMethods.Http.Get; var response = (HttpWebResponse)request.GetResponse(); Console.WriteLine("PreSignedURL Get Object Status Code {0}", response.StatusCode); response.Close(); } catch (TosServerException ex) { Console.WriteLine("PreSignedURL failed, Request ID {0}", ex.RequestID); Console.WriteLine("PreSignedURL failed, Status Code {0}", ex.StatusCode); Console.WriteLine("PreSignedURL failed, Response Error Code {0}", ex.Code); Console.WriteLine("PreSignedURL failed, Response Error Message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("PreSignedURL failed, Error Message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("PreSignedURL failed, {0}", ex.Message); } } } }
以下代码用于生成预签名的 URL,然后使用预签名的 URL 在指定桶中删除对象。
using System; using System.Net; using TOS; using TOS.Error; using TOS.Model; namespace ConsoleApp { internal class Program { private static void Main(string[] args) { var ak = Environment.GetEnvironmentVariable("TOS_ACCESS_KEY"); var sk = Environment.GetEnvironmentVariable("TOS_SECRET_KEY"); // endpoint 若没有指定HTTP协议(HTTP/HTTPS),默认使用 HTTPS // Bucket 的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com var endpoint = "https://tos-cn-beijing.volces.com"; var region = "cn-beijing"; // 填写 BucketName var bucketName = "*** Provide your bucket name ***"; // 填写对象名 var objectKey = "*** Provide your object key ***"; var data = "Hello TOS"; // 创建TOSClient实例 var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build(); try { var preSignedURLInput = new PreSignedURLInput() { Bucket = bucketName, Key = objectKey, HttpMethod = HttpMethodType.HttpMethodDelete, }; var preSignedURLOutput = client.PreSignedURL(preSignedURLInput); // 删除对象 var request = (HttpWebRequest)WebRequest.Create(preSignedURLOutput.SignedUrl); request.Method = "DELETE"; var response = (HttpWebResponse)request.GetResponse(); Console.WriteLine("PreSignedURL Delete Object Status Code {0}", response.StatusCode); response.Close(); } catch (TosServerException ex) { Console.WriteLine("PreSignedURL failed, Request ID {0}", ex.RequestID); Console.WriteLine("PreSignedURL failed, Status Code {0}", ex.StatusCode); Console.WriteLine("PreSignedURL failed, Response Error Code {0}", ex.Code); Console.WriteLine("PreSignedURL failed, Response Error Message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("PreSignedURL failed, Error Message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("PreSignedURL failed, {0}", ex.Message); } } } }
关于 URL 包含签名的详细信息,请参见 URL 中包含签名。