您可以通过 GetObject 接口从指定桶中下载对象。在使用此接口之前,请确保拥有对此对象的读访问权限。
以下代码用于下载对象到内存。
using System; using System.IO; 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 = "example_dir/example.txt"; // 创建TOSClient实例 var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build(); try { var getObjectInput = new GetObjectInput { Bucket = bucketName, Key = objectKey, // 下载时重写响应头 ResponseContentType = "application/json", ResponseContentEncoding = "deflate" }; var getObjectOutput = client.GetObject(getObjectInput); Console.WriteLine("GetObject succeeded, request id {0}", getObjectOutput.RequestID); // 下载时前设置的 response content type Console.WriteLine("GetObject succeeded, content type {0}", getObjectOutput.ContentType); // 下载时前设置的 response content encoding Console.WriteLine("GetObject succeeded, content encoding {0}", getObjectOutput.ContentEncoding); // 下载数据大小 Console.WriteLine("GetObject succeeded, content length {0}", getObjectOutput.ContentLength); // 完成下载 using (var reader = new StreamReader(getObjectOutput.Content)) { Console.WriteLine("GetObject succeeded, content {0}", reader.ReadToEnd()); getObjectOutput.Content.Close(); } } catch (TosServerException ex) { Console.WriteLine("GetObject failed, request id {0}", ex.RequestID); Console.WriteLine("GetObject failed, status code {0}", ex.StatusCode); Console.WriteLine("GetObject failed, response error code {0}", ex.Code); Console.WriteLine("GetObject failed, response error message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("GetObject failed, error message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("GetObject failed, {0}", ex.Message); } } } }
以下代码用于从 TOS 下载对象到指定的本地路径。
using System; using System.IO; 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 ***"; // 填写 ObjectName var objectKey = "example_dir/example.txt"; // 下载指定文件路径 var downloadFilePath = "example_dir/example.txt"; // 创建TOSClient实例 var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build(); try { var getObjectToFileInput = new GetObjectToFileInput() { Bucket = bucketName, Key = objectKey, FilePath = downloadFilePath }; var getObjectToFileOutput = client.GetObjectToFile(getObjectToFileInput); Console.WriteLine("GetObjectToFile succeeded, request id {0}", getObjectToFileOutput.RequestID); Console.WriteLine("GetObjectToFile succeeded, content length {0}", getObjectToFileOutput.ContentLength); } catch (TosServerException ex) { Console.WriteLine("GetObjectToFile failed, request id {0}", ex.RequestID); Console.WriteLine("GetObjectToFile failed, status code {0}", ex.StatusCode); Console.WriteLine("GetObjectToFile failed, response error code {0}", ex.Code); Console.WriteLine("GetObjectToFile failed, response error message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("GetObjectToFile failed, error message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("GetObjectToFile failed, {0}", ex.Message); } } } }