初始化
快速入门
-
初始化一个BosClient。
BosClient是与BOS服务交互的客户端,BOS C# SDK的BOS操作都是通过BosClient完成的。
示例代码:
C#1class BosClientSample 2{ 3 static void Main(string[] args) 4 { 5 const string accessKeyId = <AccessKeyID>; // 您的Access Key ID 6 const string secretAccessKey = <SecretAccessKey>; // 您的Secret Access Key 7 const string endpoint = "https://bj.bcebos.com"; //传入Bucket所在区域域名 8 9 // 初始化一个BosClient 10 BceClientConfiguration config = new BceClientConfiguration(); 11 config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey); 12 config.Endpoint = endpoint; 13 BosClient client = new BosClient(config); 14 } 15}
-
新建一个Bucket。
Bucket是BOS上的命名空间,相当于数据的容器,可以存储若干数据实体(Object)。在您上传数据前,必须先创建一个Bucket。
示例代码:
C#1public void CreateBucket(BosClient client, string bucketName) 2{ 3 // 新建一个Bucket 4 client.CreateBucket(bucketName); 5}
-
上传Object。
Object是BOS中最基本的数据单元,您可以把Object简单的理解为文件。对于一个简单的Object的上传,BOS为您提供了四种方式:文件形式上传、数据流形式上传、二进制串上传和字符串上传。
示例代码:
C#1public void PutObject(BosClient client, String bucketName, String objectKey, byte[] byte1, String string1) 2{ 3 // 获取指定文件 4 FileInfo file = new FileInfo(<FilePath>); //指定文件路径 5 6 // 以文件形式上传Object 7 PutObjectResponse putObjectFromFileResponse = client.PutObject(bucketName, objectKey, file); 8 9 // 获取数据流 10 Stream inputStream = file.OpenRead(); 11 12 // 以数据流形式上传Object 13 PutObjectResponse putObjectResponseFromInputStream = client.PutObject(bucketName, objectKey, inputStream); 14 15 // 以二进制串上传Object 16 PutObjectResponse putObjectResponseFromByte = client.PutObject(bucketName, objectKey, Encoding.Default.GetBytes("sampledata")); 17 18 // 以字符串上传Object 19 PutObjectResponse putObjectResponseFromString = client.PutObject(bucketName, objectKey, "sampledata"); 20 21 // 打印ETag 22 Console.WriteLine(putObjectFromFileResponse.ETAG); 23}
-
查看Bucket下的Object列表。
当您完成一系列上传后,可以参考如下代码来查看Bucket下的全部Object。
示例代码:
C#1public void ListObjects(BosClient client, string bucketName) 2{ 3 // 获取指定Bucket下的所有Object信息 4 ListObjectsResponse listObjectsResponse = client.ListObjects(bucketName); 5 6 // 遍历所有Object 7 foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents) 8 { 9 Console.WriteLine("ObjectKey: " + objectSummary.Key); 10 } 11}
-
获取指定Object
用户可以参考如下代码来实现对一个或者多个Object的获取。
示例代码:
C#1public void GetObject(BosClient client, String bucketName, String objectKey) 2{ 3 // 获取Object,返回结果为BosObject对象 4 BosObject bosObject = client.GetObject(bucketName, objectKey); 5 6 // 获取ObjectMeta 7 ObjectMetadata meta = bosObject.ObjectMetadata; 8 9 // 获取Object的输入流 10 Stream objectContent = bosObject.ObjectContent; 11 12 // 处理Object 13 ... 14 15 // 关闭流 16 objectContent.Close(); 17}
完整示例
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Text;
6using BaiduBce;
7using BaiduBce.Auth;
8using BaiduBce.Services.Bos;
9using BaiduBce.Services.Bos.Model;
10
11namespace DotnetSample
12{
13 internal class BaseSample
14 {
15 private static void Main(string[] args)
16 {
17 BosClient client = GenerateBosClient();
18 const string bucketName = <BucektName>; //指定Bucket名称
19 const string objectKey = <ObjectKey>; //指定object名称
20
21 //创建Bucket
22 client.CreateBucket(bucketName);
23
24 //上传Object
25 FileInfo file = new FileInfo("d:\\lzb\\sample.txt"); //指定上传文件的路径
26 PutObjectResponse putObjectFromFileResponse = client.PutObject(bucketName, objectKey, file);
27 Console.WriteLine(putObjectFromFileResponse.ETAG);
28
29 //查看Object
30 ListObjectsResponse listObjectsResponse = client.ListObjects(bucketName);
31 foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents)
32 {
33 Console.WriteLine("ObjectKey: " + objectSummary.Key);
34 }
35
36 // 获取Object
37 BosObject bosObject = client.GetObject(bucketName, objectKey);
38 // 获取ObjectMeta
39 ObjectMetadata meta = bosObject.ObjectMetadata;
40 // 获取Object的输入流
41 Stream objectContent = bosObject.ObjectContent;
42 // 处理Object
43 FileStream fileStream = new FileInfo("d:\\lzb\\sampleout.txt").OpenWrite(); //指定下载文件的目录/文件名
44 byte[] buffer = new byte[2048];
45 int count = 0;
46 while ((count = objectContent.Read(buffer, 0, buffer.Length)) > 0)
47 {
48 fileStream.Write(buffer, 0, count);
49 }
50
51 // 关闭流
52 objectContent.Close();
53 fileStream.Close();
54 Console.WriteLine(meta.ETag);
55 Console.WriteLine(meta.ContentLength);
56 }
57
58 private static BosClient GenerateBosClient()
59 {
60 const string accessKeyId = <AccessKeyID>; // 您的Access Key ID
61 const string secretAccessKey = <SecretAccessKey>; // 您的Secret Access Key
62 const string endpoint = "https://bj.bcebos.com"; //指定Bucket所在区域域名
63
64 // 初始化一个BosClient
65 BceClientConfiguration config = new BceClientConfiguration();
66 config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
67 config.Endpoint = endpoint;
68
69 return new BosClient(config);
70 }
71 }
72}
新建BosClient
BosClient是BOS服务的客户端,为开发者与BOS服务进行交互提供了一系列的方法。
使用AK/SK新建BosClient
-
基本流程
- 确定EndPoint。EndPoint是指BOS服务在各个区域的域名地址,默认域名为北京http://bj.bcebos.com。
- 创建一个BceClientConfiguration实例。
- 使用您的AK/SK创建DefaultBceCredentials并赋值给BceClientConfiguration的Credentials属性。
- 用配置好的BceClientConfiguration创建BosClient实例。
- 示例代码
1class BosClientSample
2{
3 static void Main(string[] args)
4 {
5 const string accessKeyId = <AccessKeyID>; // 您的Access Key ID
6 const string secretAccessKey = <SecretAccessKey>; // 您的Secret Access Key
7 const string endpoint = "https://bj.bcebos.com"; // 传入Bucket所在区域域名
8
9 // 初始化一个BosClient
10 BceClientConfiguration config = new BceClientConfiguration();
11 config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
12 config.Endpoint = endpoint;
13 BosClient client = new BosClient(config);
14 }
15}
EndPoint参数只能用指定的包含区域的域名来进行定义,不指定时默认为北京区域http://bj.bcebos.com。百度智能云目前开放了多区域支持,请参考区域选择说明。 目前支持“华北-北京”、“华南-广州”和“华东-苏州”三个区域。北京区域:http://bj.bcebos.com,广州区域:http://gz.bcebos.com,苏州区域:http://su.bcebos.com。
使用STS新建BosClient
-
基本流程
- 确定StsEndPoint 。StsEndPoint 是指授权服务的域名地址,默认域名为http://sts.bj.baidubce.com。
- 创建一个BceClientConfiguration实例。
- 使用您的AK/SK创建DefaultBceCredentials并赋值给BceClientConfiguration的Credentials属性。
- 用配置好的BceClientConfiguration创建StsClient实例。
- 通过StsClient获取临时AK/SK、token用于临时授权。
- 确定EndPoint。EndPoint是指BOS服务在各个区域的域名地址,默认域名为北京http://bj.bcebos.com。
- 再次创建一个BceClientConfiguration实例。
- 使用临时AK/SK、token创建DefaultBceCredentials并赋值给BceClientConfiguration的Credentials属性。
- 用配置好的BceClientConfiguration创建BosClient实例。
- 示例代码
1class BosClientSample
2{
3 static void Main(string[] args)
4 {
5 const string accessKeyId = <AccessKeyID>; // 您的Access Key ID
6 const string secretAccessKey = <SecretAccessKey>; // 您的Secret Access Key
7 const string endpoint = "https://bj.bcebos.com"; // 指定Bucket所在区域域名
8 const string stsEndpoint = "http://sts.bj.baidubce.com";// 指定授权服务域名
9
10 // 获取临时授权Token
11 BceClientConfiguration stsConfig = new BceClientConfiguration();
12 stsConfig.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
13 stsConfig.Endpoint = stsEndpoint;
14 StsClient stsClient = new StsClient(stsConfig);
15 GetSessionTokenResponse response = stsClient.GetSessionToken();
16
17 // 使用Token初始化一个BosClient
18 BceClientConfiguration config = new BceClientConfiguration();
19 config.Credentials = new DefaultBceSessionCredentials(
20 response.AccessKeyId,
21 response.SecretAccessKey,
22 response.SessionToken);
23 config.Endpoint = endpoint;
24 BosClient client = new BosClient(config);
25 }
26}
注意: 目前使用STS配置client时,无论对应bucket的区域在哪里,stsEndpoint都需配置为http://sts.bj.baidubce.com, 但创建BosClient时,仍需使用BOS的endpoint,如bj.bcebos.com、su.bcebos.com等.
配置HTTPS协议访问BOS
BOS支持HTTPS传输协议,您可以将EndPoint直接设置为https即可,不设置EndPoint时,默认为http协议。
-
示例代码
PHP1class BosClientSample 2{ 3 static void Main(string[] args) 4 { 5 const string accessKeyId = <AccessKeyID>; // 您的Access Key ID 6 const string secretAccessKey = <SecretAccessKey>; // 您的Secret Access Key 7 const string endpoint = "https://bj.bcebos.com" ; //传入Bucket所在区域域名,并直接设置为https协议 8 9 // 初始化一个BosClient 10 BceClientConfiguration config = new BceClientConfiguration(); 11 config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey); 12 config.Endpoint = endpoint; 13 BosClient client = new BosClient(config); 14 } 15}
配置自定义域名访问BOS
如果希望使用自定义域名作为访问BOS的endpoint,在控制台将自定义域名和BOS某个bucket绑定之后,配置endpoint为自定义域名并打开CnameEnabled
开关,例如cdn-test.cdn.bcebos.com
,配置代码如下:
1class BosClientSample
2{
3 static void Main(string[] args)
4 {
5 const string accessKeyId = <AccessKeyID>; // 您的Access Key ID
6 const string secretAccessKey = <SecretAccessKey>; // 您的Secret Access Key
7 const string endpoint = <EndPoint>; // 传入自定义域名
8
9 // 初始化一个BosClient
10 BceClientConfiguration config = new BceClientConfiguration();
11 config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
12 config.Endpoint = endpoint;
13 config.CnameEnabled = true;
14 BosClient client = new BosClient(config);
15 }
16}
配置BosClient
-
基本流程
- 创建一个BceClientConfiguration实例。
- 使用您的AK/SK创建DefaultBceCredentials并赋值给BceClientConfiguration的Credentials属性。
- 对BceClientConfiguratio实例的各种属性进行赋值。
- 用配置好的BceClientConfiguration创建BosClient实例。
如果用户需要配置BosClient的一些细节的参数,可以在构造BosClient的时候传入BosClientConfiguration对象。 BosClientConfiguration是BOS服务的配置类,可以为客户端配置超时时间,最大连接数等参数。
设置网络参数
-
示例代码
C#1BceClientConfiguration config = new BceClientConfiguration(); 2 3// 设置HTTP最大连接数为10 4config.ConnectionLimit = 10; 5 6// 设置TCP连接超时为5000毫秒 7config.TimeoutInMillis = 5000; 8 9// 设置读写数据超时的时间为50000毫秒 10config.ReadWriteTimeoutInMillis = 50000;
-
参数说明
通过BceClientConfiguration能指定的所有参数如下表所示:
参数 | 说明 |
---|---|
UserAgent | 用户代理,指HTTP的User-Agent头 |
Protocol | 连接协议类型,缺省值为HTTP协议 |
TimeoutInMillis | 建立连接的超时时间(单位:毫秒),缺省值为30000 |
ReadWriteTimeoutInMillis | 通过打开的连接传输数据的超时时间(单位:毫秒),缺省值为30000 |
ConnectionLimit | 允许打开的最大HTTP连接数,缺省值为5 |
RetryPolicy | 连接重试策略 |
SocketBufferSizeInBytes | Socket缓冲区大小 |
CnameEnabled | 是否使用自定义域名,默认为false,开启前需确认已经申请自定义域名 |
PathStyleEnabled | 是否使用三级域名方式请求,默认为false使用四级域名,如有服务需要使用三级域名时开启,一般不建议设为true |
-
完整示例
下面示例代码演示了BosClient的创建和配置。
C#1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using BaiduBce; 6using BaiduBce.Auth; 7using BaiduBce.Services.Bos; 8 9namespace DotnetSample 10{ 11 internal class BosClientSample 12 { 13 private static void Main(string[] args) 14 { 15 const string accessKeyId = <AccessKeyID>; // 您的Access Key ID 16 const string secretAccessKey = <SecretAccessKey>; // 您的Secret Access Key 17 const string endpoint = "https://bj.bcebos.com"; 18 19 // 初始化一个BosClient 20 BceClientConfiguration config = new BceClientConfiguration(); 21 config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey); 22 config.Endpoint = endpoint; 23 24 // 设置HTTP最大连接数为10 25 config.ConnectionLimit = 10; 26 27 // 设置TCP连接超时为5000毫秒 28 config.TimeoutInMillis = 5000; 29 30 // 设置读写数据超时的时间为50000毫秒 31 config.ReadWriteTimeoutInMillis = 50000; 32 33 BosClient client = new BosClient(config); 34 } 35 } 36}