You need to enable JavaScript to run this app.
导航
普通下载(.NET SDK)
最近更新时间:2024.02.04 18:31:07首次发布时间:2023.08.24 14:41:46

您可以通过 GetObject 接口从指定桶中下载对象。在使用此接口之前,请确保拥有对此对象的读访问权限。

注意事项

  • 下载对象前,您需要确保账号拥有 tos:GetObject 权限,具体操作,请参见权限配置指南
  • 对于开启多版本的桶,下载指定版本对象时,您必须具备 tos:GetObjectVersion 权限,具体操作,请见权限配置指南
  • 如果应用程序会在同一时刻大量下载同一个对象,您的访问速度会受到 TOS 带宽及地域的限制。建议您使用 CDN 产品,提升性能的同时也能降低您的成本。通过 CDN 访问 TOS 的详细信息,请参见使用 CDN 加速访问 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 ***";
            // 下载对象
            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);
            }
        }
    }
}