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

普通上传是指通过 PutObject 方法上传单个对象(Object),上传字符串(字符流)、上传 Bytes(Bytes 流)、上传网络流和上传本地文件四种形式。

注意事项

  • 上传对象前,您需要确保账号已拥有 tos:PutObject 权限,具体操作,请参见权限配置指南
  • 上传对象时,对象名称必须满足命名规范,详细信息,请参见对象的命名规范
  • TOS 是面向海量存储设计的分布式对象存储产品,内部分区存储了对象索引数据。为横向扩展您上传对象和下载对象时的最大吞吐量和减小热点分区的概率,请您避免使用字典序递增的对象命名方式,详细信息请参见性能优化
  • 如果桶中已经存在同名对象,则新对象会覆盖已有的对象。在桶开启多版本的场景下,则会保留原有对象并生成一个新版本号用于标识新上传的对象。

示例代码

上传字符串

以下代码通过 PutObject 接口上传字符串数据到 TOS 指定 example_dir 目录下的 example.txt 文件。

using System;
using System.IO;
using System.Text;
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 ***";
            // 上传对象 Body,以 string 对象为例
            var objectContent = "Hello TOS";
            // 将文件上传到 example_dir 目录下的 example.txt 文件
            var objectKey = "example_dir/example.txt";

            // 创建TOSClient实例
            var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build();

            try
            {
                byte[] binaryData = Encoding.UTF8.GetBytes(objectContent);
                using (MemoryStream requestContent = new MemoryStream(binaryData))
                {
                    // 创建上传文件输入
                    var putObjectInput = new PutObjectInput()
                    {
                        Bucket = bucketName,
                        Key = objectKey,
                        Content = requestContent
                    };
                    // 将字符串 “Hello TOS” 上传到指定 example_dir 目录下的 example.txt
                    var putObjectOutput = client.PutObject(putObjectInput);
                    Console.WriteLine("Put object succeeded, ETag: {0} ", putObjectOutput.ETag);
                }
            }
            catch (TosServerException ex)
            {
                Console.WriteLine("Put object failed, request id {0}", ex.RequestID);
                Console.WriteLine("Put object failed, status code {0}", ex.StatusCode);
                Console.WriteLine("Put object failed, response error code {0}", ex.Code);
                Console.WriteLine("Put object failed, response error message {0}", ex.Message);
            }
            catch (TosClientException ex)
            {
                Console.WriteLine("Put object failed, error message {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Put object failed, {0}", ex.Message);
            }
        }
    }
}

上传网络流

以下代码通过 PutObject 接口上传网络流数据到 TOS 指定 example_dir 目录下的 example.txt 文件。

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 ***";
            // 将文件上传到 example_dir 目录下的 example.txt 文件
            var objectKey = "example_dir/example.txt";

            // 创建TOSClient实例
            var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build();

            try
            {
                // 从网络流中获取数据
                var request = (HttpWebRequest)WebRequest.Create("https://www.volcengine.com/");
                request.Method = WebRequestMethods.Http.Get;
                var response = (HttpWebResponse)request.GetResponse();

                // 创建上传文件输入
                var putObjectInput = new PutObjectInput()
                {
                    Bucket = bucketName,
                    Key = objectKey,
                    Content = response.GetResponseStream()
                };

                // 将网络流上传到指定 example_dir 目录下的 example.txt
                var putObjectOutput = client.PutObject(putObjectInput);
                Console.WriteLine("Put object succeeded, request id: {0} ", putObjectOutput.RequestID);
                response.Close();
            }
            catch (TosServerException ex)
            {
                Console.WriteLine("Put object failed, request id {0}", ex.RequestID);
                Console.WriteLine("Put object failed, status code {0}", ex.StatusCode);
                Console.WriteLine("Put object failed, response error code {0}", ex.Code);
                Console.WriteLine("Put object failed, response error message {0}", ex.Message);
            }
            catch (TosClientException ex)
            {
                Console.WriteLine("Put object failed, error message {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Put object failed, {0}", ex.Message);
            }
        }
    }
}

上传本地文件流

以下代码通过 PutObject 接口将指定文件流上传到 TOS 指定 example_dir 目录下的 example.txt 文件。

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 ***";
            // 将文件上传到 example_dir 目录下的 example.txt 文件
            var objectKey = "example_dir/example.txt";
            // 需要上传的本地文件
            var localFileName = "/usr/local/test.txt";

            // 创建TOSClient实例
            var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build();

            try
            {
                // 从文件流中获取数据
                using (var fileStream = File.OpenRead(localFileName))
                {
                    // 创建上传文件流输入
                    var putObjectInput = new PutObjectInput()
                    {
                        Bucket = bucketName,
                        Key = objectKey,
                        Content = fileStream
                    };

                    // 将文件流上传到指定 example_dir 目录下的 example.txt
                    var putObjectOutput = client.PutObject(putObjectInput);
                    Console.WriteLine("Put object succeeded, request id: {0} ", putObjectOutput.RequestID);
                }
            }
            catch (TosServerException ex)
            {
                Console.WriteLine("Put object failed, request id {0}", ex.RequestID);
                Console.WriteLine("Put object failed, status code {0}", ex.StatusCode);
                Console.WriteLine("Put object failed, response error code {0}", ex.Code);
                Console.WriteLine("Put object failed, response error message {0}", ex.Message);
            }
            catch (TosClientException ex)
            {
                Console.WriteLine("Put object failed, error message {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Put object failed, {0}", ex.Message);
            }
        }
    }
}

上传本地文件

以下代码通过 PutObjectFromFile 接口指定文件路径将文件上传到 TOS 指定 example_dir 目录下的 example.txt 文件。

using System;
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 ***";
            // 将文件上传到 example_dir 目录下的 example.txt 文件
            var objectKey = "example_dir/example.txt";
            // 需要上传的本地文件
            var localFileName = "/usr/local/test.txt";

            // 创建TOSClient实例
            var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build();

            try
            {
                // 创建上传本地文件输入
                var putObjectFromFileInput = new PutObjectFromFileInput()
                {
                    Bucket = bucketName,
                    Key = objectKey,
                    FilePath = localFileName
                };

                // 直接使用文件路径上传文件
                var putObjectFromFileOutput = client.PutObjectFromFile(putObjectFromFileInput);
                Console.WriteLine("Put object succeeded, request id: {0} ", putObjectFromFileOutput.RequestID);
            }
            catch (TosServerException ex)
            {
                Console.WriteLine("Put object failed, request id {0}", ex.RequestID);
                Console.WriteLine("Put object failed, status code {0}", ex.StatusCode);
                Console.WriteLine("Put object failed, response error code {0}", ex.Code);
                Console.WriteLine("Put object failed, response error message {0}", ex.Message);
            }
            catch (TosClientException ex)
            {
                Console.WriteLine("Put object failed, error message {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Put object failed, {0}", ex.Message);
            }
        }
    }
}

上传时设置对象元数据信息

以下代码通过 PutObject 接口上传字符串数据到指定 example_dir 目录下的 example.txt 文件,上传时指定对象存储类型为低频存储,权限为私有同时设置上传文件元数据信息。

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
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 ***";

            // 上传对象 Body,以 string 对象为例
            var objectContent = "Hello TOS";
            // 将文件上传到 example_dir 目录下的 example.txt 文件
            var objectKey = "example_dir/example.txt";

            // 创建TOSClient实例
            var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build();

            try
            {
                var binaryData = Encoding.UTF8.GetBytes(objectContent);
                using (var requestContent = new MemoryStream(binaryData))
                {
                    // 创建上传文件输入
                    var putObjectInput = new PutObjectInput
                    {
                        Bucket = bucketName,
                        Key = objectKey,
                        // 指定对象权限为私有
                        ACL = ACLType.ACLPrivate,
                        // 指定存储类型为低频存储
                        StorageClass = StorageClassType.StorageClassIa,
                        // 用户自定义元数据信息
                        Meta = new Dictionary<string, string> { { "key", "value" } },
                        Content = requestContent
                    };
                    // 将字符串 “Hello TOS” 上传到指定 example_dir 目录下的 example.txt
                    var putObjectOutput = client.PutObject(putObjectInput);
                    Console.WriteLine("Put object succeeded, ETag: {0} ", putObjectOutput.ETag);
                }
            }
            catch (TosServerException ex)
            {
                Console.WriteLine("Put object failed, request id {0}", ex.RequestID);
                Console.WriteLine("Put object failed, status code {0}", ex.StatusCode);
                Console.WriteLine("Put object failed, response error code {0}", ex.Code);
                Console.WriteLine("Put object failed, response error message {0}", ex.Message);
            }
            catch (TosClientException ex)
            {
                Console.WriteLine("Put object failed, error message {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Put object failed, {0}", ex.Message);
            }
        }
    }
}