普通上传是指通过 PutObject 方法上传单个对象(Object),上传字符串(字符流)、上传 Bytes(Bytes 流)、上传网络流和上传本地文件四种形式。
以下代码通过 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); } } } }