Object management
Upload Object
Simplest upload
-
Basic workflow
- Create an instance of the BOSClient class.
- Invoke the BosClient.PutObject() method. When using this method, ensure you provide the bucket name, ObjectKey, and the object's content. You can upload the object in one of the following ways: as a file, a data stream, a binary string, or a standard string.
-
Example code
C#1public void PutObject(BosClient client, String bucketName, String objectKey, byte[] byte1, String string1) 2{ 3 // Get specified file 4 ileInfo file = new FileInfo(<FilePath>); //Specify the file path 5 // Upload object as a file 6 PutObjectResponse putObjectFromFileResponse = client.PutObject(bucketName, objectKey, file); 7 // Obtain data stream 8 Stream inputStream = file.OpenRead(); 9 // Upload an object in the form of a data stream 10 PutObjectResponse putObjectResponseFromInputStream = client.PutObject(bucketName, objectKey, inputStream); 11 // Upload object as binary string 12 PutObjectResponse putObjectResponseFromByte = client.PutObject(bucketName, objectKey,Encoding.Default.GetBytes("sampledata")); 13 // Upload object in string form 14 PutObjectResponse putObjectResponseFromString = client.PutObject(bucketName, objectKey, "sampledata"); 15 // Print ETag 16 Console.WriteLine(putObjectFromFileResponse.ETAG); 17}
Note: Files are uploaded to BOS as objects. The putObject function supports uploading objects with a size of up to 5 GB. Once the putObject request is successfully processed, BOS will include the object's ETag in the response header, serving as the file identifier.
-
Complete example
Please refer to [complete example](#Complete example).
Set object's HTTP Header
BOS allows setting Http Headers when uploading an object.
-
Basic workflow
- Create an instance of the BOSClient class.
- When using BosClient.PutObject(), you can also provide an ObjectMetadata object, allowing you to set custom HTTP headers.
-
Example code
C#1//Initialize the upload input stream 2ObjectMetadata meta = new ObjectMetadata(); 3 // Set ContentLength size 4meta.ContentLength = <Length>; 5 // Set ContentType 6meta.ContentType = "application/json"; 7client.PutObject(bucketName, objectKey, <SampleData>, meta); -
Complete example
Please refer to [complete example](#Complete example).
User-defined metadata
-
Basic workflow
- Create an instance of the BOSClient class.
- When using BosClient.PutObject(), you can also provide an ObjectMetadata object, enabling you to set custom metadata to describe the object.
-
Example code
C#1// Set the value of custom metadata name to my-data 2meta.UserMetadata["name"] = "my-data"; 3 // Upload Object 4client.PutObject(bucketName, objectKey, <SampleData>, meta);Note: In the provided example, a user-defined metadata property is created with the key "name" and the value "my-data." This metadata can be retrieved when the object is downloaded. Though multiple such parameters can be added, the combined size of all User Metadata must not exceed 2KB.
-
Complete example
Please refer to [complete example](#Complete example).
Complete example
The following sample code demonstrates the complete process of simply uploading an object, setting the HTTP header of the object, and user-defined metadata:
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;
10namespace DotnetSample
11{
12 internal class PutObjectSample
13 {
14 private static void Main(string[] args)
15 {
16 BosClient client = GenerateBosClient();
17 const string bucketName = <BucketName>; //Your bucket name
18 const string objectNameFile = <ObjectNameFile>;//Name of the object uploaded in file form
19 const string objectNameStream = <ObjectNameStream>; //Name of the object uploaded in data stream form
20 const string objectNameString = <ObjectNameString>; //Name of the object uploaded in string form
21 const string objectNameByte = <ObjectNameByte>; //Name of the object uploaded in binary form
22 // Create a new bucket
23 client.CreateBucket(bucketName); //Specify bucket name
24 //Set the file name to be uploaded
25 const string fileName = "d:\\sample.txt";
26 // Upload object as a file
27 PutObjectResponse putObjectFromFileResponse = client.PutObject(bucketName, objectNameFile,
28 new FileInfo(fileName));
29 // Upload an object in the form of a data stream
30 PutObjectResponse putObjectResponseFromInputStream = client.PutObject(bucketName, objectNameStream,
31 new FileInfo(fileName).OpenRead());
32 // Upload object as binary string
33 PutObjectResponse putObjectResponseFromByte = client.PutObject(bucketName, objectNameByte,
34 Encoding.Default.GetBytes("sampledata"));
35 // Upload object in string form
36 PutObjectResponse putObjectResponseFromString = client.PutObject(bucketName, objectNameString,
37 "sampledata");
38 //Print the ETags of the four methods. In the example, the ETag of the file method and the stream method are equal, and the ETag of the string method and the byte method are equal
39 Console.WriteLine(putObjectFromFileResponse.ETAG);
40 Console.WriteLine(putObjectResponseFromInputStream.ETAG);
41 Console.WriteLine(putObjectResponseFromByte.ETAG);
42 Console.WriteLine(putObjectResponseFromString.ETAG);
43 //Upload the object and set custom parameters
44 ObjectMetadata meta = new ObjectMetadata();
45 // Set ContentLength size
46 meta.ContentLength = 10;
47 // Set ContentType
48 meta.ContentType = "application/json";
49 // Set the value of custom metadata name to my-data
50 meta.UserMetadata["name"] = "my-data";
51 //Upload the object and print the ETag
52 putObjectResponseFromString = client.PutObject(bucketName, objectNameString, "sampledata", meta);
53 Console.WriteLine(putObjectResponseFromString.ETAG);
54 }
55 private static BosClient GenerateBosClient()
56 {
57 const string accessKeyId = <AccessKeyID>;//Your Access Key ID
58 const string secretAccessKey = <SecretAccessKey>; //Your Secret Access Key
59 const string endpoint = "https://bj.bcebos.com";//Specify the BOS service domain name
60 // Initialize a BOSClient
61 BceClientConfiguration config = new BceClientConfiguration();
62 config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
63 config.Endpoint = endpoint;
64 return new BosClient(config);
65 }
66 }
67}
View the object in the bucket.
Simple query
View the list of objects in a bucket.
-
Basic workflow
- Instantiate the BOSClient class.
- Invoke the BOSClient.ListObjects() method. Provide the bucket name as a parameter when using this method.
-
Example code
C#1public void ListObjects(BosClient client, string bucketName) 2{ 3 // Obtain all object information under the specified bucket 4 ListObjectsResponse listObjectsResponse = client.ListObjects(bucketName); 5 // Traverse all objects 6 foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents) 7 { 8 Console.WriteLine("ObjectKey: " + objectSummary.Key); 9 } 10}Note: The listObjects() method returns a ListObjectsResponse object, which contains the results of this listObject request. Users can obtain the description information of all objects through the getContents method in ListObjectsResponse.
- By default, if the number of objects in the bucket exceeds 1,000, only 1,000 objects will be returned. In this case, the IsTruncated value in the returned result will be set to True, and NextMarker will be provided as the starting point for the next retrieval.
- To retrieve more objects, use the Marker parameter for reading in batches. Refer to [Extended Query](#Extended query).
-
Complete example
Please refer to [complete example](#Complete example - 1).
Extended query
Users can configure the ListObjectsRequest parameters to perform advanced query operations. The extended parameters available for configuration in ListObjectsRequest include the following:
| Parameter name | Description | Default value |
|---|---|---|
| MaxKeys | Specify the maximum number of objects to return, which must not exceed 1,000. | 1000 |
| Prefix | Set the prefix of the objectKey. The prefix means that the objectKey contains and starts with the value of Prefix. It is usually used in conjunction with Delimiter when [querying simulated folders](#Query simulated folders). |
- |
| Delimiter | It is a delimiter used to hierarchize objectKey. It is usually used in conjunction with Prefix when [querying simulated folders](#Query simulated folders). The objectKey from the Prefix to the first occurrence of the Delimiter character is called: CommonPrefixes. |
- |
| Marker | It is a string used to set the starting position of the returned results. After setting the Marker value, the returned objects will be returned starting from the Marker value in alphabetical order. |
- |
-
Basic workflow
- Instantiate the BOSClient class.
- Alternatively, construct a ListObjectsRequest to invoke methods like ListObjects() and ListObjectsRequest, enabling more advanced query operations.
-
Example code
C#1// Construct a ListObjectsRequest request 2ListObjectsRequest listObjectsRequest = new ListObjectsRequest() {BucketName = bucketName}; 3 // Set parameters 4listObjectsRequest.Delimiter=<Delimiter>; 5listObjectsRequest.Marker=<Marker>; 6ListObjectsResponse listObjectsResponse = client.ListObjects(listObjectsRequest);Description:
The above code calls an overloaded method from
listObjectsby passing aListObjectsRequestto complete the request. -
Complete example
Please refer to [complete example](#Complete example - 1).
Query simulated folders
Since BOS is inherently a (<Key>,<Value>) storage system, the concept of "folder" does not exist in principle. However, you can simulate the folder function by combining the Delimiter and Prefix} parameters.
Suppose the bucket contains five files: bos.jpg, fun/, fun/test.jpg, fun/movie/001.avi, and fun/movie/007.avi. The "/" symbol can be used as a delimiter to mimic folder structures.
Recursively list all files under the simulated folder
You can obtain all files in the a directory by setting the Prefix parameter:
1// Construct a ListObjectsRequest request
2ListObjectsRequest listObjectsRequest = new ListObjectsRequest() {BucketName = bucketName};
3 // Recursively list all files under the fun directory
4listObjectsRequest.Prefix = "fun/";
5// List Objects
6ListObjectsResponse listObjectsResponse = client.ListObjects(listObjectsRequest);
7 // Traverse all objects
8Console.WriteLine("Objects:");
9foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents)
10{
11 Console.WriteLine("ObjectKey: " + objectSummary.Key);
12}
Output:
1Objects:
2fun/
3fun/movie/001.avi
4fun/movie/007.avi
5fun/test.jpg
View files and subfolders under the simulated folder
With the combination of Prefix and Delimiter, it can list files and subfolders under the simulated folder:
1// Construct a ListObjectsRequest request
2ListObjectsRequest listObjectsRequest = new ListObjectsRequest() {BucketName = bucketName};
3 // "/" is the delimiter for folders
4listObjectsRequest.Delimiter = "/";
5 //List all files and folders under the “fun” directory
6listObjectsRequest.Prefix = "fun/";
7// List Objects
8ListObjectsResponse listObjectsResponse = client.ListObjects(listObjectsRequest);
9 //Traverse all objects, which is equivalent to retrieving all files under the “fun” directory
10Console.WriteLine("Objects:");
11foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents)
12{
13 Console.WriteLine("ObjectKey: " + objectSummary.Key);
14}
15 //Traverse all CommonPrefixes, which is equivalent to getting all subfolders under the “fun” directory
16Console.WriteLine("\nCommonPrefixs:");
17foreach (ObjectPrefix objectPrefix in listObjectsResponse.CommonPrefixes)
18{
19 Console.WriteLine(objectPrefix.Prefix);
20}
Output:
1Objects:
2fun/
3fun/test.jpg
4CommonPrefixs:
5fun/movie/
Description:
In the returned results, the list under
Objectsshows the files under the fun folder. The list inCommonPrefixsshows all subfolders under the fun folder. It can be seen that the two filesfun/movie/001.aviandfun/movie/007.aviare not listed because they belong to themoviesubfolder under thefunfolder.
-
Complete example
Please refer to [complete example](#Complete example - 1).
Complete example
The following sample code demonstrates the complete process of simple query of objects, batch query using nextmarker, extended query, and query of simulated folders:
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using BaiduBce;
6using BaiduBce.Auth;
7using BaiduBce.Services.Bos;
8using BaiduBce.Services.Bos.Model;
9namespace DotnetSample
10{
11 internal class ListObjectsSample
12 {
13 private static void Main(string[] args)
14 {
15 BosClient client = GenerateBosClient();
16 const string bucketName = <BucketName>; //Your bucket name
17 // Create a new bucket
18 client.CreateBucket(bucketName);
19 //Create five files: bos.jpg, fun/, fun/test.jpg, fun/movie/001.avi, fun/movie/007.avi
20 client.PutObject(bucketName, "bos.jpg", "sampledata");
21 client.PutObject(bucketName, "fun/", "sampledata");
22 client.PutObject(bucketName, "fun/test.jpg", "sampledata");
23 client.PutObject(bucketName, "fun/movie/001.avi", "sampledata");
24 client.PutObject(bucketName, "fun/movie/007.avi", "sampledata");
25 // Construct a ListObjectsRequest request
26 ListObjectsRequest listObjectsRequest = new ListObjectsRequest() { BucketName = bucketName };
27 //1. Simple query: list all files under the bucket
28 ListObjectsResponse listObjectsResponse = client.ListObjects(listObjectsRequest);
29 //Output:
30 // Objects:
31 // bos.jpg
32 // fun/
33 // fun/movie/001.avi
34 // fun/movie/007.avi
35 // fun/test.jpg
36 Console.WriteLine("Objects:");
37 foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents)
38 {
39 Console.WriteLine("ObjectKey: " + objectSummary.Key);
40 }
41 //2. Use NextMarker to list all files in batches
42 listObjectsRequest.MaxKeys = 2;
43 listObjectsResponse = client.ListObjects(listObjectsRequest);
44 //Output:
45 // Objects:
46 // bos.jpg
47 // fun/
48 // fun/movie/001.avi
49 // fun/movie/007.avi
50 // fun/test.jpg
51 Console.WriteLine("Objects:");
52 while (listObjectsResponse.IsTruncated)
53 {
54 foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents)
55 {
56 Console.WriteLine("ObjectKey: " + objectSummary.Key);
57 }
58 listObjectsResponse = client.ListNextBatchOfObjects(listObjectsResponse);
59 }
60 foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents)
61 {
62 Console.WriteLine("ObjectKey: " + objectSummary.Key);
63 }
64 //3. Recursively list all files and subfolders under the “fun/”
65 listObjectsRequest.Prefix = "fun/";
66 listObjectsResponse = client.ListObjects(listObjectsRequest);
67 //Output:
68 // Objects:
69 // fun/
70 // fun/movie/001.avi
71 // fun/movie/007.avi
72 // fun/test.jpg
73 Console.WriteLine("Objects:");
74 foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents)
75 {
76 Console.WriteLine("ObjectKey: " + objectSummary.Key);
77 }
78 //4. List all files and subfolders under the “fun”
79 listObjectsRequest.Delimiter = "/";
80 listObjectsResponse = client.ListObjects(listObjectsRequest);
81 //Output:
82 // Objects:
83 // fun/
84 // fun/test.jpg
85 Console.WriteLine("Objects:");
86 foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents)
87 {
88 Console.WriteLine("ObjectKey: " + objectSummary.Key);
89 }
90 //Traverse all CommonPrefixes, which is equivalent to getting all subfolders under the “fun” directory
91 //Output:
92 // CommonPrefixs:
93 // fun/movie
94 Console.WriteLine("\nCommonPrefixs:");
95 foreach (ObjectPrefix objectPrefix in listObjectsResponse.CommonPrefixes)
96 {
97 Console.WriteLine(objectPrefix.Prefix);
98 }
99 }
100 private static BosClient GenerateBosClient()
101 {
102 const string accessKeyId = <AccessKeyID>;//Your Access Key ID
103 const string secretAccessKey = <SecretAccessKey>; //Your Secret Access Key
104 const string endpoint = "https://bj.bcebos.com";//Specify the BOS service domain name
105 // Initialize a BOSClient
106 BceClientConfiguration config = new BceClientConfiguration();
107 config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
108 config.Endpoint = endpoint;
109 return new BosClient(config);
110 }
111 }
112}
Get Object
Simple object get
-
Basic workflow
- Create an instance of the BOSClient class.
- Execute the BosClient.GetObject() method to retrieve the specified BosObject.
- Load the BosObject into a stream for further processing.
-
Example code
C#1public void GetObject(BosClient client, String bucketName, String objectKey) 2{ 3 //Obtain the object, with returned result BosObject object 4 BosObject bosObject = client.GetObject(bucketName, objectKey); 5 // Retrieve ObjectMeta 6 ObjectMetadata meta = bosObject.ObjectMetadata; 7 //Obtain the object's input stream 8 Stream objectContent = bosObject.ObjectContent; 9 // Process object 10 ... 11 // Close stream 12 objectContent.Close(); 13}Note:
- BosObject contains various information about the Object, including the bucket where the object is located, the name of the object, MetaData, and a input stream.
- You can read the object's content into a file or memory by operating on the input stream.
- ObjectMetadata contains information such as the ETag defined during object upload, HTTP headers, and custom metadata.
- Using the getObjectContent() method of BosObject, users can access the input stream of the returned object, enabling them to read and process the object's content.
-
Complete example
Please refer to [complete example](#Complete example - 2).
Obtain object via GetObjectRequest
To unlock more capabilities, GetObjectRequest can be used to retrieve objects.
-
Basic workflow
- Create an instance of the GetObjectRequest class.
- Invoke the setRange() method on GetObjectRequest to retrieve specific byte ranges from the object.
- Perform the GetObject() operation.
-
Example code
C#1// Create GetObjectRequest 2 GetObjectRequest getObjectRequest = new GetObjectRequest() {BucketName = bucketName, Key = objectKey}; 3 // Obtain data within the 0~100 byte range 4getObjectRequest.SetRange(0, 100); 5 //Obtain the object, with returned result BosObject object 6BosObject bosObject = client.GetObject(getObjectRequest);Note: The setRange() method in GetObjectRequest allows users to specify a range for the returned object. This feature can be used to implement segmented downloads and resumable uploads.
-
Complete example
Please refer to [complete example](#Complete example - 2).
Directly download object to a specified path
Users can use the following code to directly download an object to a specified path.
-
Basic workflow
- Create an instance of the GetObjectRequest class.
- Execute the client.getObject() operation.
- Download an object directly to a specified path.
-
Example code
C#1// Create GetObjectRequest 2GetObjectRequest getObjectRequest = new GetObjectRequest() {BucketName = bucketName, Key = objectKey}; 3 // Download object to file 4ObjectMetadata objectMetadata = client.GetObject(getObjectRequest, new FileInfo("d:\\sample.txt"));Note: When using the above method to directly download an object to a specified path, the method returns an ObjectMetadata object.
-
Complete example
Please refer to [complete example](#Complete example - 2).
Obtain only ObjectMetadata
The getObjectMetadata() method retrieves only the ObjectMetadata, without obtaining the object itself.
-
Example code
C#1ObjectMetadata objectMetadata = client.GetObjectMetadata(bucketName, objectKey); -
Complete example
Please refer to [complete example](#Complete example - 2).
Get object URL
The following code allows you to get the URL of a specific object, which is commonly used for temporarily sharing the object's URL with others.
-
Basic workflow
- Create an instance of the BOSClient class.
- Use the BosClient.GeneratePresignedUrl() method. Supply parameters like the bucket name, object name, and expiration time when calling this method.
- The method returns the URL of the object.
-
Example code
C#1public string GeneratePresignedUrl(BosClient client, string bucketName, string objectKey, 2 int expirationInSeconds) 3{ 4 //Specify the name of the bucket where the object that the user needs to get is located, the name of the object, timestamp and the valid duration of the URL 5 Uri url = client.GeneratePresignedUrl(bucketName, objectKey, expirationInSeconds); 6 return url.AbsoluteUri; 7}Note:
ExpirationInSecondsis the specified URL validity period, calculated from the current time. It is an optional parameter, and the system default value is 1,800 s if not configured. To set a permanent non-expiration time, theExpirationInSecondsparameter can be set to -1. You cannot set it to any other negative value. -
Complete example
Please refer to [complete example](#Complete example - 2).
Complete example
The following sample code demonstrates the complete process of simply getting an object, getting an object through GetObjectRequest, directly downloading an object to a specified path, only getting ObjectMetadata, and getting the URL of the object:
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;
10namespace DotnetSample
11{
12 internal class GetObjectsSample
13 {
14 private static void Main(string[] args)
15 {
16 BosClient client = GenerateBosClient();
17 const string bucketName = <BucketName>; //Your bucket name
18 //Initialization: create a bucket and an object
19 client.CreateBucket(bucketName);
20 string objectName = <ObjectKey>;
21 client.PutObject(bucketName, objectName, <SampleData>);
22 //Get the BosObject object and get the content through the input stream of BosObject
23 BosObject bosObject = client.GetObject(bucketName, objectName);
24 Stream objectContent = bosObject.ObjectContent;
25 string content = new StreamReader(objectContent).ReadToEnd();
26 Console.WriteLine(content);//<SampleData> you specified
27 //Get only part of the data through GetObjectRequest
28 GetObjectRequest getObjectRequest = new GetObjectRequest() {BucketName = bucketName, Key = objectName};
29 getObjectRequest.SetRange(0, 5);
30 bosObject = client.GetObject(getObjectRequest);
31 objectContent = bosObject.ObjectContent;
32 content = new StreamReader(objectContent).ReadToEnd();
33 Console.WriteLine(content);//<SampleData> you specified
34 //Directly get the byte[] content through GetObjectContent
35 byte[] bytes = client.GetObjectContent(bucketName, objectName);
36 content = Encoding.Default.GetString(bytes);
37 Console.WriteLine(content);//<SampleData> you specified
38 // Download object content to file
39 FileInfo fileInfo = new FileInfo("my file path and name");
40 client.GetObject(bucketName, objectName,fileInfo );
41 content = File.ReadAllText(fileInfo.FullName);
42 Console.WriteLine(content);//<SampleData> you specified
43 //Only get the object’s meta, not the content
44 ObjectMetadata objectMetadata = client.GetObjectMetadata(bucketName, objectName);
45 Console.WriteLine(objectMetadata.ContentLength);
46 Console.ReadKey();
47 //Generate a URL and directly download and print the object content through this URL
48 string url = client.GeneratePresignedUrl(bucketName, objectName, 60).AbsoluteUri;
49 using (WebClient webClient = new WebClient())
50 {
51 using (Stream stream = webClient.OpenRead(url))
52 using (StreamReader streamReader = new StreamReader(stream))
53 {
54 string response = streamReader.ReadToEnd();
55 Console.WriteLine(response);//<SampleData> you specified
56 }
57 }
58 }
59 private static BosClient GenerateBosClient()
60 {
61 const string accessKeyId = <AccessKeyID>;//Your Access Key ID
62 const string secretAccessKey = <SecretAccessKey>; //Your Secret Access Key
63 const string endpoint = "https://bj.bcebos.com";//Specify the BOS service domain name
64 // Initialize a BOSClient
65 BceClientConfiguration config = new BceClientConfiguration();
66 config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
67 config.Endpoint = endpoint;
68 return new BosClient(config);
69 }
70 }
71}
Delete Object
-
Basic workflow
- Create an instance of the BOSClient class.
- Invoke the BosClient.DeleteObject() method. Provide parameters such as the bucket name and object name when calling this method.
-
Example code
C#1public void DeleteObject(BosClient client, string bucketName, string objectKey) 2{ 3 // Delete Object 4client.DeleteObject(bucketName, objectKey); 5} -
Complete example
C#1using System; 2using System.Collections.Generic; 3using System.IO; 4using System.Linq; 5using System.Net; 6using System.Text; 7using BaiduBce; 8using BaiduBce.Auth; 9using BaiduBce.Services.Bos; 10using BaiduBce.Services.Bos.Model; 11namespace DotnetSample 12{ 13 internal class DeleteObjectSample 14 { 15 private static void Main(string[] args) 16 { 17 BosClient client = GenerateBosClient(); 18 const string bucketName = <BucketName>; //Specify bucket name 19 //Initialization: create a bucket sample and an object 20 client.CreateBucket(bucketName); 21 string objectName = <ObjectKey>; 22 client.PutObject(bucketName, objectName, <Sampledata>); 23 // Delete Object 24 client.DeleteObject(bucketName, objectName); 25 } 26 private static BosClient GenerateBosClient() 27 { 28 const string accessKeyId = <AccessKeyID>;//Your Access Key ID 29 const string secretAccessKey = <SecretAccessKey>; //Your Secret Access Key 30 const string endpoint = "https://bj.bcebos.com";//Specify the BOS service domain name 31 // Initialize a BOSClient 32 BceClientConfiguration config = new BceClientConfiguration(); 33 config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey); 34 config.Endpoint = endpoint; 35 return new BosClient(config); 36 } 37 } 38}
Copy Object
Simply Copy Object
-
Basic workflow
- Initialize an instance of the BOSClient class.
- Execute the BosClient.copyObject() method.
-
Example code
C#1public void CopyObject(BosClient client, String srcBucketName, String srcKey, String destBucketName, 2String destKey) 3{ 4 // Copy Object 5 CopyObjectResponse copyObjectResponse = client.CopyObject(srcBucketName, srcKey, destBucketName, destKey); 6 // Print results 7 Console.WriteLine("ETag: " + copyObjectResponse.ETag + " LastModified: " + copyObjectResponse.LastModified); 8}Note: The copyObject method returns a
CopyObjectResponseobject containing the new object's ETag and modification time. -
Complete example
Please refer to [complete example](#Complete example - 3).
Copy Object via CopyObjectRequest
You can also copy object via CopyObjectRequest This function is typically used in the following scenarios:
- Copy an object while resetting its metadata.
- Reset the metadata of an existing object (set the source and destination to the same object).
-
Basic workflow
- Create an instance of the CopyObjectRequest class, and pass in the parameters
<SrcBucketName>,<SrcKey>,<DestBucketName>,<DestKey>. - Create an ObjectMetadata instance.
- Create an instance of the CopyObjectRequest class, and pass in the parameters
-
Example code
C#1// Initialize BosClient 2BosClient client = ...; 3 // Create CopyObjectRequest object 4CopyObjectRequest copyObjectRequest = new CopyObjectRequest() 5{ 6 SourceBucketName = srcBucketName, 7 SourceKey = srcKey, 8 BucketName = destBucketName, 9 Key = destKey 10}; 11 // Set new Metadata 12Dictionary<String, String> userMetadata = new Dictionary<String, String>(); 13userMetadata["usermetakey"] = "usermetavalue"; 14ObjectMetadata objectMetadata = new ObjectMetadata() 15{ 16 UserMetadata = userMetadata 17}; 18copyObjectRequest.NewObjectMetadata = objectMetadata; 19 //Copy the object and print new ETag 20CopyObjectResponse copyObjectResponse = client.CopyObject(copyObjectRequest); 21Console.WriteLine("ETag: " + copyObjectResponse.ETag + " LastModified: " + copyObjectResponse.LastModified);Note:
CopyObjectRequestallows users to modify the ObjectMeta of the target object and also provides the setting of theMatchingETagConstraintsparameter. -
Complete example
Please refer to [complete example](#Complete example - 3).
Complete example
The following sample code demonstrates the complete process of simply copying an object and copying an object through CopyObjectRequest:
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Net;
6using System.Text;
7using BaiduBce;
8using BaiduBce.Auth;
9using BaiduBce.Services.Bos;
10using BaiduBce.Services.Bos.Model;
11namespace DotnetSample
12{
13 internal class CopyObjectSample
14 {
15 private static void Main(string[] args)
16 {
17 BosClient client = GenerateBosClient();
18 const string bucketName = <SrcBucketName>;
19 //Initialization: create a bucket sample and an object
20 client.CreateBucket(bucketName);
21 string objectName = <SrcObjectKey>;
22 client.PutObject(bucketName, objectName, <SrcSampleData>);
23 //Ordinary copy and print the result
24 string newObjectName = <DesObjectKey>;
25 CopyObjectResponse copyObjectResponse = client.CopyObject(bucketName, objectName, bucketName,
26 newObjectName);
27 Console.WriteLine(Encoding.Default.GetString(client.GetObjectContent(bucketName, newObjectName)));
28 //Copy and set new meta
29 newObjectName = <DesObjectKey>;
30 CopyObjectRequest copyObjectRequest = new CopyObjectRequest()
31 {
32 SourceBucketName = bucketName,
33 SourceKey = objectName,
34 BucketName = bucketName,
35 Key = newObjectName
36 };
37 Dictionary<String, String> userMetadata = new Dictionary<String, String>();
38 userMetadata["usermetakey"] = "usermetavalue";
39 ObjectMetadata objectMetadata = new ObjectMetadata()
40 {
41 UserMetadata = userMetadata
42 };
43 copyObjectRequest.NewObjectMetadata = objectMetadata;
44 client.CopyObject(copyObjectRequest);
45 // usermetavalue
46 Console.WriteLine(client.GetObjectMetadata(bucketName, newObjectName).UserMetadata["usermetakey"]);
47 }
48 private static BosClient GenerateBosClient()
49 {
50 const string accessKeyId = <AccessKeyID>;//Your Access Key ID
51 const string secretAccessKey = <SecretAccessKey>; //Your Secret Access Key
52 const string endpoint = "https://bj.bcebos.com";//Specify the BOS service domain name
53 // Initialize a BOSClient
54 BceClientConfiguration config = new BceClientConfiguration();
55 config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
56 config.Endpoint = endpoint;
57 return new BosClient(config);
58 }
59 }
60}
Multipart upload of objects
In addition to using the putObject() method for file uploads to BOS, BOS also supports another upload mode called Multipart Upload. This mode can be used in scenarios such as:
- When resumable uploads are required.
- When uploading files larger than 5GB.
- When the connection to the BOS server is frequently interrupted due to unstable network conditions.
- Enable streaming file uploads.
- The file size cannot be determined before uploading.
Complete Multipart Upload in parts
Suppose there is a file with the local path d:\sample.txt. Due to the large size of the file, it will be transmitted to BOS in parts.
-
Basic workflow
- Create an instance of the BOSClient class.
- Call the BosClient.InitiateMultipartUpload() method. When using this method, provide the bucket name and object name. The method will return an UploadId needed for subsequent steps.
- Invoke the BosClient.UploadPart method multiple times to upload the file in parts. Supply parameters such as the bucket name, object name, UploadId, part sequence number, part size, and part content. Each invocation returns the sequence number and ETag of that part for use in later steps.
- Use the BosClient.CompleteMultipartUpload() method to finalize the multipart upload. You need to provide the bucket name, object name, UploadId, and sequence numbers and ETags of all parts.
- During the upload process, you may utilize the BosClient.ListParts() method to obtain all uploaded parts associated with a specified UploadId. Alternatively, the BosClient.ListMultipartUploads() method can retrieve all unfinished UploadIds in a specific bucket.
Initialize multipart upload
Use InitiateMultipartUpload method to initialize a multipart upload event:
-
Example code
C#1// Initiate Multipart Upload 2InitiateMultipartUploadRequest initiateMultipartUploadRequest = 3 new InitiateMultipartUploadRequest() {BucketName = bucketName, Key = objectKey}; 4InitiateMultipartUploadResponse initiateMultipartUploadResponse = 5 client.InitiateMultipartUpload(initiateMultipartUploadRequest); 6 // Print UploadId 7Console.WriteLine("UploadId: " + initiateMultipartUploadResponse.UploadId);Note:
The return result of initiateMultipartUploadcontainsUploadId, which is the unique identifier for distinguishing multipart upload events, and we will use it in subsequent operations.
Upload parts
Upload the file in segments.
-
Example code
C#1// Set each part to 5MB 2long partSize = 1024 * 1024 * 5L; 3FileInfo partFile = new FileInfo("my file"); 4 // Calculate the count of parts 5int partCount = (int) (partFile.Length / partSize); 6if (partFile.Length % partSize != 0) 7{ 8 partCount++; 9} 10 // Create a list to save the ETag and PartNumber of each uploaded part 11List<PartETag> partETags = new List<PartETag>(); 12for (int i = 0; i < partCount; i++) 13{ 14 // Get file stream 15 Stream stream = partFile.OpenRead(); 16 //Jump to the beginning of each part 17 long skipBytes = partSize * i; 18 stream.Seek(skipBytes, SeekOrigin.Begin); 19 // Calculate the size of each part 20 long size = Math.Min(partSize, partFile.Length - skipBytes); 21 // Create UploadPartRequest to upload parts 22 uploadPartRequest uploadPartRequest = new UploadPartRequest(); 23 uploadPartRequest.BucketName = bucketName; 24 uploadPartRequest.Key = objectKey; 25 uploadPartRequest.UploadId = initiateMultipartUploadResponse.UploadId; 26 uploadPartRequest.InputStream = stream; 27 uploadPartRequest.PartSize = size; 28 uploadPartRequest.PartNumber = i + 1; 29 UploadPartResponse uploadPartResponse = client.UploadPart(uploadPartRequest); 30 // Save the returned PartETag to the List. 31 partETags.Add(new PartETag() 32 { 33 ETag = uploadPartResponse.ETag, 34 PartNumber = uploadPartResponse.PartNumber 35 }); 36 // Close the file 37 stream.Close(); 38 }Note:
The core of the above code is to call the
UploadPartmethod to upload each part, but the following points should be noted:- The multipart upload method requires each part, except the last one, to be at least 5 MB in size. However, the Upload Part API does not validate the part size; this validation only happens when completing the multipart upload.
- To ensure no errors during network transmission, it is recommended to use the Content-MD5 value returned by BOS for each part after
UploadPartto verify the correctness of the uploaded part data. When all part data is combined into one Object, it no longer contains the MD5 value. - The part number must be within the range of 1 to 10,000. If this limit is exceeded, BOS will return an InvalidArgument error code.
- For each uploaded part, the stream must be positioned at the beginning of the respective part.
- After each Part upload, the return result of BOS will include a
PartETagobject, which is a combination of the uploaded block's ETag and block number (PartNumber). It will be used in subsequent steps to complete the multipart upload, so it must be saved. Generally speaking, thesePartETagobjects will be saved in a List.
Complete multipart upload
-
Example code
C#1CompleteMultipartUploadRequest completeMultipartUploadRequest = 2new CompleteMultipartUploadRequest() 3{ 4 BucketName = bucketName, 5 Key = objectKey, 6 UploadId = initiateMultipartUploadResponse.UploadId, 7 PartETags = partETags 8}; 9 // Complete multipart upload 10CompleteMultipartUploadResponse completeMultipartUploadResponse = 11client.CompleteMultipartUpload(completeMultipartUploadRequest); 12 // Print Object's ETag 13Console.WriteLine(completeMultipartUploadResponse.ETag);Note: The
partETagsin the above code is the list of partETag saved in the second step. After BOS receives the part list submitted by the user, it will verify the validity of each data Part one by one. Once all data parts are validated, BOS will assemble the data parts into a complete Object.
Cancel multipart upload event
Users can cancel multipart uploads by using the abortMultipartUpload method.
-
Example code
C#1AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest() 2{ 3 BucketName = bucketName, 4 Key = objectKey, 5 UploadId = initiateMultipartUploadResponse.UploadId, 6}; 7 // Cancel multipart upload 8client.AbortMultipartUpload(abortMultipartUploadRequest);
Get unfinished multipart upload event
Users can obtain the unfinished multipart upload events in the bucket by the ListMultipartUploads method.
-
Example code
C#1ListMultipartUploadsRequest listMultipartUploadsRequest = 2 new ListMultipartUploadsRequest() {BucketName = bucketName}; 3 // Retrieve all upload events within the bucket 4ListMultipartUploadsResponse listMultipartUploadsResponse = 5 client.ListMultipartUploads(listMultipartUploadsRequest); 6 // Traverse all upload events 7foreach (MultipartUploadSummary multipartUpload in listMultipartUploadsResponse.Uploads) 8{ 9 Console.WriteLine("Key: " + multipartUpload.Key + " UploadId: " + multipartUpload.UploadId); 10}Note:
- By default, if a bucket contains more than 1,000 multipart upload events, only 1,000 will be returned. The IsTruncated value in the result will be True, and NextKeyMarker will indicate the starting point for the next query.
- To fetch more multipart upload events, use the KeyMarker parameter to retrieve data in batches.
Get all uploaded information
Users can obtain all uploaded parts in an upload event by the ListParts method.
-
Example code
C#1ListPartsRequest listPartsRequest = new ListPartsRequest() 2{ 3 BucketName = bucketName, 4 Key = objectKey, 5 UploadId = initiateMultipartUploadResponse.UploadId, 6}; 7 // Retrieve all uploaded part information 8ListPartsResponse listPartsResponse = client.ListParts(listPartsRequest); 9 // Traverse all parts 10foreach (PartSummary part in listPartsResponse.Parts) 11{ 12 Console.WriteLine("PartNumber: " + part.PartNumber + " ETag: " + part.ETag); 13}Note:
- By default, if a bucket has more than 1,000 multipart upload events, only 1,000 parts will be returned. The IsTruncated value in the result will be True, and NextPartNumberMarker will indicate the starting point for the next query.
- To retrieve more information about uploaded parts, use the PartNumberMarker parameter to fetch data in batches.
Complete example
The following sample code demonstrates the complete process of multipart upload:
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;
10namespace DotnetSample
11{
12 internal class MultiUploadSample
13 {
14 private static void Main(string[] args)
15 {
16 BosClient client = GenerateBosClient();
17 const string bucketName = <BucketName>;//Bucket name
18 //Initialization: create a sample bucket
19 client.CreateBucket(bucketName);
20 string objectKey = <ObjectKey>;
21 // Initiate Multipart Upload
22 InitiateMultipartUploadRequest initiateMultipartUploadRequest =
23 new InitiateMultipartUploadRequest() { BucketName = bucketName, Key = objectKey };
24 InitiateMultipartUploadResponse initiateMultipartUploadResponse =
25 client.InitiateMultipartUpload(initiateMultipartUploadRequest);
26 //Get Multipart Upload in the bucket
27 ListMultipartUploadsRequest listMultipartUploadsRequest =
28 new ListMultipartUploadsRequest() { BucketName = bucketName };
29 ListMultipartUploadsResponse listMultipartUploadsResponse =
30 client.ListMultipartUploads(listMultipartUploadsRequest);
31 foreach (MultipartUploadSummary multipartUpload in listMultipartUploadsResponse.Uploads)
32 {
33 Console.WriteLine("Key: " + multipartUpload.Key + " UploadId: " + multipartUpload.UploadId);
34 }
35 //Multipart upload, first set each part to 5 MB
36 long partSize = 1024 * 1024 * 5L;
37 FileInfo partFile = new FileInfo("d:\\lzb\\sample");
38 // Calculate the count of parts
39 int partCount = (int)(partFile.Length / partSize);
40 if (partFile.Length % partSize != 0)
41 {
42 partCount++;
43 }
44 // Create a list to save the ETag and PartNumber of each uploaded part
45 List<PartETag> partETags = new List<PartETag>();
46 for (int i = 0; i < partCount; i++)
47 {
48 // Get file stream
49 Stream stream = partFile.OpenRead();
50 //Jump to the beginning of each part
51 long skipBytes = partSize * i;
52 stream.Seek(skipBytes, SeekOrigin.Begin);
53 // Calculate the size of each part
54 long size = Math.Min(partSize, partFile.Length - skipBytes);
55 // Create UploadPartRequest to upload parts
56 UploadPartRequest uploadPartRequest = new UploadPartRequest();
57 uploadPartRequest.BucketName = bucketName;
58 uploadPartRequest.Key = objectKey;
59 uploadPartRequest.UploadId = initiateMultipartUploadResponse.UploadId;
60 uploadPartRequest.InputStream = stream;
61 uploadPartRequest.PartSize = size;
62 uploadPartRequest.PartNumber = i + 1;
63 UploadPartResponse uploadPartResponse = client.UploadPart(uploadPartRequest);
64 // Save the returned PartETag to the List.
65 partETags.Add(new PartETag()
66 {
67 ETag = uploadPartResponse.ETag,
68 PartNumber = uploadPartResponse.PartNumber
69 });
70 // Close the file
71 stream.Close();
72 }
73 //Get all Upload Parts of the UploadId
74 ListPartsRequest listPartsRequest = new ListPartsRequest()
75 {
76 BucketName = bucketName,
77 Key = objectKey,
78 UploadId = initiateMultipartUploadResponse.UploadId,
79 };
80 // Retrieve all uploaded part information
81 ListPartsResponse listPartsResponse = client.ListParts(listPartsRequest);
82 // Traverse all parts
83 foreach (PartSummary part in listPartsResponse.Parts)
84 {
85 Console.WriteLine("PartNumber: " + part.PartNumber + " ETag: " + part.ETag);
86 }
87 // Complete multipart upload
88 CompleteMultipartUploadRequest completeMultipartUploadRequest =
89 new CompleteMultipartUploadRequest()
90 {
91 BucketName = bucketName,
92 Key = objectKey,
93 UploadId = initiateMultipartUploadResponse.UploadId,
94 PartETags = partETags
95 };
96 CompleteMultipartUploadResponse completeMultipartUploadResponse =
97 client.CompleteMultipartUpload(completeMultipartUploadRequest);
98 Console.WriteLine(completeMultipartUploadResponse.ETag);
99 Console.ReadKey();
100 }
101 private static BosClient GenerateBosClient()
102 {
103 const string accessKeyId = <AccessKeyID>;//Your Access Key ID
104 const string secretAccessKey = <SecretAccessKey>; //Your Secret Access Key
105 const string endpoint = "https://bj.bcebos.com";//Specify the BOS service domain name
106 // Initialize a BOSClient
107 BceClientConfiguration config = new BceClientConfiguration();
108 config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
109 config.Endpoint = endpoint;
110 return new BosClient(config);
111 }
112 }
113}
Support single-link rate limit
Baidu AI Cloud Object Storage (BOS) provides a public network bandwidth limit of 10 Gbit/s per bucket and an intranet bandwidth limit of 50 Gbit/s per bucket. When the upload or download usage reaches the bandwidth limit, the error code RequestRateLimitExceeded will be returned. To ensure normal service usage, BOS supports traffic control during uploads and downloads to prevent high traffic services from affecting other applications.
Example of single-link speed-limited upload and download APIs
The value range of the rate limit value is 819200~838860800 in bit/s, that is, 100KB/s~100MB/s. The rate limit value must be a number. BOS will limit the rate of this request according to the specified rate limit value. When the rate limit value is not within this range or is illegal, it will return the error code 400.
1public void ObjectTrafficLimit(BosClient client, String bucketName, String objectKey, byte[] byte1, String string1)
2{
3 //Specific speed limit value
4 long trafficLimit = 819200;
5 // Get specified file
6 ileInfo file = new FileInfo(<FilePath>); //Specify the file path
7 //Generate put request
8 PutObjectRequest putRequest = new PutObjectRequest()
9 {
10 BucketName = bucketName,
11 Key = objectKey,
12 FileInfo = file,
13 TrafficLimit = trafficLimit
14 };
15 // Upload object as a file
16 PutObjectResponse putObjectFromFileResponse = client.PutObject(putRequest);
17 // Print ETag
18 Console.WriteLine(putObjectFromFileResponse.ETAG);
19 //Generate get request
20 GetObjectRequest getRequest = new GetObjectRequest()
21 {
22 BucketName = bucketName,
23 Key = objectKey,
24 TrafficLimit = trafficLimit
25 };
26 //Get object
27 BosObject bosObject = client.GetObject(getRequest);
28 // Print ETag
29 Console.WriteLine(bosObject.ObjectMetadata.ETag);
30}
