百度智能云

All Product Document

          Object Storage

          Object Management

          Object Uploading

          The Simplest Uploading

          • Basic procedure

            1.Create an instance of BOSClient class 2.Call BOSClient.Putobject() method. When calling this method, you need to provide bucket name, contents of ObjectKey and object. You can upload object with the following four methods: Files, data streams, binary strings, and character strings.

          • Sample Code

              public void PutObject(BosClient client, String BucketName, String objectKey, byte[] byte1, String string1) 
              { 
              
                // Access specified files. 
                FileInfo file = new FileInfo(<FilePath>);    //Designate file path. 
                        
                // Upload object as file. 
                PutObjectResponse putObjectFromFileResponse = client.PutObject(BucketName, ObjectKey, file); 
                        
                // Access data stream. 
                Stream inputStream = file.OpenRead(); 
                        
                // Upload object as the data stream. 
                PutObjectResponse putObjectResponseFromInputStream = client.PutObject(bucketName, objectKey, inputStream); 
                        
                // Upload object as the binary string. 
                PutObjectResponse putObjectResponseFromByte = client.PutObject(bucketName, objectKey,Encoding.Default.GetBytes("sampledata")); 
                            
                // Upload object as the character string. 
                PutObjectResponse putObjectResponseFromString = client.PutOobject(bucketName, objectKey, "sampledata"); 
              
                // Print ETag. 
                Console.WriteLine(putObjectFromFileResponse.ETAG); 
                
              } 

            Note: Upload object to BOS in the file form. objects no more than 5 GB can be uploaded through putObject. After the putObject request is successfully processed, BOS will return the ETag of object as a file identifier in the Header.

          • Complete example

            See [complete example](# complete example).

          Set the Http Header of Object

          Under BOS, Http Header setting is available during object uploading.

          • Basic procedure

            1.Create an instance of BOSClient class 2.When calling BOSClient.Putobject(), you can also introduce an ObjectMetadata object, which can set a custom Http Header.

          • Sample Code

              // Upload input stream in initialization 
              ObjectMetadata meta = new ObjectMetadata(); 
              
              // Set the size of ContentLength. 
              meta.ContentLength =<Length>; 
              
              // Set ContentType. 
              meta.ContentType = "application/json"; 
              
              client.PutObject(bucketName, objectKey,<SampleData>, meta); 
          • Complete example

            See [complete example](# complete example).

          Custom Metadata

          • Basic procedure

            1.Create an instance of BOSClient class 2.When calling BOSClient.Putobject(), you can also introduce an ObjectMetadata object, which supports customization of metadata by users to describe the object.

          • Sample Code

              // Set the name value of customized metadata as my-data. 
              meta.UserMetadata["name"] = "my-data"; 
              
              // Upload object
              client.PutObject(bucketName, objectKey,<SampleData>, meta); 

            Note: As for the above code, users have customized a metadata of which the name is "name" and the value is "my-data". When users download this object, they can get metadata together. One object possesses similar parameters, but the total size of User Meta bellows 2KB.

          • Complete example

            See [complete example](# complete example).

          Complete Example

          The following sample code demonstrates the complete process of simply uploading object, setting Http Header of object and customizing metadata by user.

          using System; 
          using System.Collections.Generic; 
          using System.IO; 
          using System.Linq; 
          using System.Text; 
          using BaiduBce; 
          using BaiduBce.Auth; 
          using BaiduBce.Services.Bos; 
          using BaiduBce.Services.Bos.Model; 
          
          namespace DotnetSample 
          { 
              internal class PutobjectSample 
              { 
                  private static void Main(string[] args) 
                  { 
                      BosClient client = GenerateBosClient(); 
                      const string bucketName =<BucketName>;        //Your bucket name 
                      const string objectNameFile =<ObjectNameFile>;  //Name of object uploaded in file 
                      const string objectNameStream =<ObjectNameStream>; //Name of object uploaded in data stream 
                      const string objectNameString =<ObjectNameString>; //Name of object uploaded in string 
                      const string objectNameByte =<ObjectNameByte>; //Name of object uploaded in binary 
          
                      // Create a bucket 
                      client.Createbucket(bucketName); //Designate the name of the bucket: 
          
                      // Set name of file to be uploaded 
                      const string fileName = "d:\\sample.txt"; 
          
                      // Upload object as file 
                      PutObjectResponse putObjectFromFileResponse = client.PutObject(bucketName, objectNameFile, 
                          new FileInfo(fileName)); 
                          
                      // Upload object as the data stream 
                      PutObjectResponse putObjectResponseFromInputStream = client.PutObject(bucketName, objectNameStream, 
                          new FileInfo(fileName).OpenRead()); 
                          
                      // Upload object as the binary string. 
                      PutObjectResponse putObjectResponseFromByte = client.PutObject(bucketName, objectNameByte, 
                          Encoding.Default.GetBytes("sampledata")); 
                          
                      // Upload object as the character string. 
                      PutObjectResponse putObjectResponseFromString = client.PutObject(bucketName, objectNameString, 
                          "sampledata"); 
          
                      // Print ETag of four methods. In the example, file mode and stream mode have the same ETag, and string mode and byte mode have the same ETag. 
                      Console.WriteLine(putObjectFromFileResponse.ETAG); 
                      Console.WriteLine(putObjectResponseFromInputStream.ETAG); 
                      Console.WriteLine(putObjectResponseFromByte.ETAG); 
                      Console.WriteLine(putObjectResponseFromString.ETAG); 
                      
                      // Upload object, and set custom parameters. 
                      ObjectMetadata meta = new ObjectMetadata(); 
                      // Set the size of ContentLength. 
                      meta.ContentLength = 10; 
                      // Set ContentType. 
                      meta.ContentType = "application/json"; 
                      // Set the name value of customized metadata as my-data. 
                      meta.UserMetadata["name"] = "my-data"; 
                      // Upload object and print ETag. 
                      putObjectResponseFromString = client.Putobject(bucketName, objectNameString, "sampledata", meta); 
                      Console.WriteLine(putObjectResponseFromString.ETAG); 
                  } 
                  
                  private static BosClient GenerateBosClient() 
                  { 
                      const string accessKeyId =<AccessKeyID>; // Your Access Key ID 
                      const string secretAccessKey =<SecretAccessKey>; // Your Secret Access Key 
                      const string endpoint =<EndPoint>; // Specify BOS service domain name 
          
                      // Initialize a BosClient 
                      BceClientConfiguration config = new BceClientConfiguration(); 
                      config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey); 
                      config.Endpoint = endpoint; 
          
                      return new BosClient(config); 
                  } 
              } 
          } 

          View the Object in Bucket

          View the object list in bucket.

          • Basic procedure

            1.Create an instance of BOSClient class 2.Call BOSClient.Listobjects() method. When calling this method, you need to provide bucket name.

          • Sample Code

              public void ListObjects(BosClient client, string BucketName) 
              { 
              
                // Access all object information of specified bucket. 
                ListObjectsResponse listObjectsResponse = client.ListObjects(bucketName); 
              
                // Traverse all objects. 
                foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents) 
                { 
                  Console.WriteLine("ObjectKey: " + objectSummary.Key); 
                } 
              
              } 

            Note: The listobjects( ) method returns the ListobjectsResponse object, which contains the result of this listobject request. You can access the description information of all objects through the getContents method in ListobjectsResponse.

            • By default, only 1,000 objects are returned and the IsTruncated value is True if bucket has over 1,000 objects. Besides, NextMarker is returned as the starting point for the next reading.
            • Use the Marker parameter to access more objects in batches. Refer to [query expansion](#query expansion).
          • Complete example

            See [complete example](#complete example-1).

          Query Expansion

          For more query expansion operation setting, you can set the ListobjectsRequest parameter. The extended parameters available to set in ListobjectsRequest are as follows:

          Parameter name Description Default value
          MaxKeys Set the maximum number of objects returned this time as 1000. 1000
          Prefix Sets the prefix of ObjectKey, which covers and starts with the value of Prefix.
          is usually used together with Delimiter in [searching simulation folders](#searching simulation folders).
          -
          Delimiter A delimiter, used to make ObjectKey hierarchies.
          is usually used together with Prefix in [searching simulation folders](#searching simulation folders).
          The ObjectKey between Prefix and Delimiter character (showing for the first time) is called: CommonPrefixes
          -
          Marker A character string, used to set the starting position of the returned result.
          After the Marker value is set, the returned object returns alphabetically after that value.
          -
          • Basic procedure

            1.Create an instance of BOSClient class 2.You can also construct a ListobjectsRequest to call Listobjects(), ListobjectsRequest, etc. for more query expansions.

          • Sample Code

              // Create the request of ListobjectsRequest
              ListObjectsRequest listObjectsRequest = new ListObjectsRequest() {BucketName = bucketName};
              
              // Parameter setting 
              listObjectsRequest.Delimiter=<Delimiter>;
              listObjectsRequest.Marker=<Marker>;
              
              ListObjectsResponse listObjectsResponse = client.ListObjects(listObjectsRequest);

            Note:

            An overloaded method of listobjects is called and requests are completed by ListobjectsRequest in the above codes.

          • Complete example

            See [complete example](#complete example-1).

          Search for Simulation Folders

          BOS, as a (<Key>,<Value>) storage system, does not have "folders" in principle. But folder function simulation can be realized by Delimiter and Prefix parameters.

          Suppose bucket have 5 files: bos.jpg, fun/, fun/test.jpg, fun/movie/001.avi, fun/movie/007.avi ("/" can be used as a delimiter to simulate folders.)

          Lists all files in simulation folders recursively.

          Set the Prefix parameter to access all files under a directory:

            // Create the request of ListobjectsRequest
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest() {BucketName = bucketName};
          
            // Recursively list all files in the fun directory
            listObjectsRequest.Prefix = "fun/";
          
            // List Objects
            ListObjectsResponse listObjectsResponse = client.ListObjects(listObjectsRequest);
          
            // Traverse all objects
            Console.WriteLine("Objects:");
            foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents)
            {
               Console.WriteLine("ObjectKey: " + objectSummary.Key);
             }

          Output:

          Objects: 
          fun/ 
          fun/movie/001.avi 
          fun/movie/007.avi 
          fun/test.jpg 

          Check the files and subfolders in simulation folders.

          The files and subfolders in simulation folders can be listed with the combination of Prefix and Delimiter:

            // Create the request of ListobjectsRequest
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest() {BucketName = bucketName};
          
            // '/' is a folder separator
            listObjectsRequest.Delimiter = "/";
          
            // List all files and folders under fun file directory
            listObjectsRequest.Prefix = "fun/";
          
            // List Objects
            ListObjectsResponse listObjectsResponse = client.ListObjects(listObjectsRequest);
          
            // Traversing all objects is equivalent to obtaining all files under fun directory
            Console.WriteLine("Objects:");
            foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents)
            {
              Console.WriteLine("ObjectKey: " + objectSummary.Key);
            }
          
            // Traversing all CommonPrefix is equivalent to obtaining all folders under fun directory
            Console.WriteLine("\nCommonPrefixs:");
            foreach (ObjectPrefix objectPrefix in listObjectsResponse.CommonPrefixes)
            {
              Console.WriteLine(objectPrefix.Prefix);
            }

          Output:

          Objects: 
          fun/ 
          fun/test.jpg 
          	
          CommonPrefixs: 
          fun/movie/

          Note:

          The returned result in the list of objects is the files in fun folders. All subfolders under fun folder are shown in the list of CommonPrefixs. It is clear that the fun/movie/001.avi file and fun/movie/007.avi file are not listed because they are the files in the movie subfolder under the fun folder.

          • Complete example

            See [complete example](#complete example-1).

          Complete Example

          The following sample code demonstrates the complete process of simply inquiring object, using nextmarker for inquiry in batch, expanded inquiry and inquiring mock folder.

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using BaiduBce;
          using BaiduBce.Auth;
          using BaiduBce.Services.Bos;
          using BaiduBce.Services.Bos.Model;
          
          namespace DotnetSample
          {
              internal class ListObjectsSample
              {
                  private static void Main(string[] args)
                  {
                      BosClient client = GenerateBosClient();
                      const string bucketName = <BucketName>;     //Your bucket name
          
                      // Create Bucket
                      client.CreateBucket(bucketName); 
          
                      // Create bos.jpg,fun/,fun/test.jpg,fun/movie/001.avi,fun/movie/007.avi五个文件
                      client.PutObject(bucketName, "bos.jpg", "sampledata");
                      client.PutObject(bucketName, "fun/", "sampledata");
                      client.PutObject(bucketName, "fun/test.jpg", "sampledata");
                      client.PutObject(bucketName, "fun/movie/001.avi", "sampledata");
                      client.PutObject(bucketName, "fun/movie/007.avi", "sampledata");
          
                      // Create the request of ListobjectsRequest
                      ListObjectsRequest listObjectsRequest = new ListObjectsRequest() { BucketName = bucketName };
          
                      // 1.Inquire simply, list all files under bucket 
                      ListObjectsResponse listObjectsResponse = client.ListObjects(listObjectsRequest);
          
                      // Output:    
                      // Objects:
                      // bos.jpg
                      // fun/
                      // fun/movie/001.avi
                      // fun/movie/007.avi
                      // fun/test.jpg
                      
                      Console.WriteLine("Objects:");
                      foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents)
                      {
                          Console.WriteLine("ObjectKey: " + objectSummary.Key);
                      }
                      
                      // 2. 2.List all files in batch using NextMarker
                      listObjectsRequest.MaxKeys = 2;
                      listObjectsResponse = client.ListObjects(listObjectsRequest);
          
                      // Output:    
                      // Objects:
                      // bos.jpg
                      // fun/
                      // fun/movie/001.avi
                      // fun/movie/007.avi
                      // fun/test.jpg
                      Console.WriteLine("Objects:");
                      while (listObjectsResponse.IsTruncated)
                      {
                          foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents)
                          {
                              Console.WriteLine("ObjectKey: " + objectSummary.Key);
                          }
                          listObjectsResponse = client.ListNextBatchOfObjects(listObjectsResponse);
                      }
                      foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents)
                      {
                          Console.WriteLine("ObjectKey: " + objectSummary.Key);
                      }
          
                      // 3.List all files and subfolders under "fun/" recursively 
                      listObjectsRequest.Prefix = "fun/";
                      listObjectsResponse = client.ListObjects(listObjectsRequest);
          
                      // Output:   
                      // Objects:
                      // fun/
                      // fun/movie/001.avi
                      // fun/movie/007.avi
                      // fun/test.jpg
                      
                      Console.WriteLine("Objects:");
                      foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents)
                      {
                          Console.WriteLine("ObjectKey: " + objectSummary.Key);
                      }
          
                      // 4.List all files and subfolders under "fun" 
                      listObjectsRequest.Delimiter = "/";
                      listObjectsResponse = client.ListObjects(listObjectsRequest);
          
                      // Output:
                      // Objects:
                      // fun/
                      // fun/test.jpg
                      
                      Console.WriteLine("Objects:");
                      foreach (BosObjectSummary objectSummary in listObjectsResponse.Contents)
                      {
                          Console.WriteLine("ObjectKey: " + objectSummary.Key);
                      }
          
                      // Traversing all CommonPrefix is equivalent to obtaining all subfolders under fun directory
                      // Output:   
                      // CommonPrefixs:
                      // fun/movie
                      
                      Console.WriteLine("\nCommonPrefixs:");
                      foreach (ObjectPrefix objectPrefix in listObjectsResponse.CommonPrefixes)
                      {
                          Console.WriteLine(objectPrefix.Prefix);
                      }
                  }
          
                  private static BosClient GenerateBosClient()
                  {
                      const string accessKeyId = <AccessKeyID>; // Your Access Key ID
                      const string secretAccessKey = <SecretAccessKey>; // Your Secret Access Key
                      const string endpoint = <EndPoint>; // Specify BOS service domain name 
          
                      // Initialize a BosClient 
                      BceClientConfiguration config = new BceClientConfiguration();
                      config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
                      config.Endpoint = endpoint;
          
                      return new BosClient(config);
                  }
              }
          }

          Access Object

          Access Object Easily

          • Basic procedure

            1.Create an instance of the BOSClient class. 2.Execute BOSClient.Getobject() method to return to the specified BosObject. 3.Read BosObject into the stream and then operate on the stream.

          • Sample Code

              public void GetObject(BosClient client, String BucketName, String ObjectKey) 
              { 
              
                // Get Object and return BosObject. 
                BosObject bosObject = client.Getobject(bucketName, objectKey); 
              
                // Get objectMeta. 
                ObjectMetadata meta = bosobject.ObjectMetadata; 
              
                // Get the input stream of object. 
                Stream ObjectContent = bosobject.ObjectContent; 
              
                // Process object. 
                ...
              
                // Close stream. 
                ObjectContent.Close(); 
              } 

            Note:

            • BosObject contains various information of object, including bucket of object, name of object, MetaData and an input stream,
            • Users can read the contents of object to file or memory by operating input stream.
            • ObjectMetadata contains the ETag defined when the object is uploaded, the Http Header, and the custom metadata.
            • Through the getObjectContent () method of BosObject, you can also get the input stream that returns the object, and read the input stream to manipulate the contents of the object.
          • Complete example

            See [complete example](#complete example-2).

          Get Object through GetObjectRequest

          For more features, you can get object by using GetObjectRequest.

          • Basic procedure

            1.Create an instance of the GetObjectRequest class. 2.Execute setRange( ) on GetObjectRequest to get the required byte data in object. 3.Execute Getobject( ) operation.

          • Sample Code

              // Create a GetObjectRequest
               GetObjectRequest getObjectRequest = new GetObjectRequest() {BucketName = bucketName, Key = objectKey}; 
              
              // Get data in the range of 0-100 bytes. 
              getObjectRequest.SetRange(0, 100); 
              
              // Get Object and return BosObject. 
              BosObject bosobject = client.GetObject(getObjectRequest); 

            Note: You can set the range to return object through setRange( ) method of getObjectRequest. You can use this function for segmented download of file and breakpoint continued upload.

          • Complete example

            See [complete example](#complete example-2).

          Download Object to the Specified Path

          You can download object directly to the specified path through the following code.

          • Basic procedure

            1.Create an instance of the GetObjectRequest class. 2.Execute the client.getobject ( ). 3.object can be downloaded to the specified path.

          • Sample Code

              // Create a GetObjectRequest
              GetObjectRequest getObjectRequest = new GetObjectRequest() {BucketName = bucketName, Key = objectKey};
              
              // Download object to file
              ObjectMetadata objectMetadata = client.GetObject(getObjectRequest, new FileInfo("d:\\sample.txt"));

            Note: When the object is downloaded to the specified path using the above method, the method returns the ObjectMetadata object.

          • Complete example

            See [complete example](#complete example-2).

          Get ObjectMetadata Only

          The getObjectMetadata() method can only get the ObjectMetadata rather than the object's entity.

          • Sample Code

              ObjectMetadata objectMetadata = client.GetObjectMetadata(bucketName, objectKey);
          • Complete example

            See [complete example](#complete example-2).

          Get the URL of Object

          You can get the URL of the specified object through the following code. This feature is typically used for scenarios where you temporarily share the URL of an object with other users.

          • Basic procedure

            1.Create an instance of BOSClient class 2.Call BosClient.GeneratePresignedUrl() method, and when calling this method, you need to provide bucket name, object name, expiration time and other parameters. 3.Return an object URL.

          • Sample Code

              public string GeneratePresignedUrl(BosClient client, string bucketName, string objectKey,
                        int expirationInSeconds)
              {
                 //pecify name of bucket of object which users need to obtain, name of that object, time stamp and effective duration of URL
                 Uri url = client.GeneratePresignedUrl(bucketName, objectKey, expirationInSeconds);             
                 return url.AbsoluteUri;
              }

            Note: ExpirationInSeconds is the effective duration of the specified URL. It is an optional parameter calculated from the current time. When not configured, the default value of the system is 1,800 seconds. If you want to set the time to be permanent, you can set the ExpirationInSeconds parameter to -1, not to other negative numbers.

          • Complete example

            See [complete example](#complete example-2).

          Complete Example

          The following sample code demonstrates the complete process of simply obtaining object, obtaining object via GetObjectRequest, directly downloading object to the specified path, only obtaining ObjectMetadata and obtaining URL of object:

          using System;
          using System.Collections.Generic;
          using System.IO;
          using System.Linq;
          using System.Text;
          using BaiduBce;
          using BaiduBce.Auth;
          using BaiduBce.Services.Bos;
          using BaiduBce.Services.Bos.Model;
          
          namespace DotnetSample
          {
              internal class GetObjectsSample
              {
                  private static void Main(string[] args)
                  {
                      BosClient client = GenerateBosClient();
                      const string bucketName = <BucketName>; //您的Bucket名称
          
                      // Initialization, create bucket and object
                      client.CreateBucket(bucketName); 
                      string objectName = <ObjectKey>;
                      client.PutObject(bucketName, objectName, <SampleData>);
          
                      // Obtain BosObject object and obtain contents via the input stream of BosObject
                      BosObject bosObject = client.GetObject(bucketName, objectName);
                      Stream objectContent = bosObject.ObjectContent;
                      string content = new StreamReader(objectContent).ReadToEnd();
                      Console.WriteLine(content); // 您传入的<SampleData>
          
                      // Only obtain some data via GetObjectRequest 
                      GetObjectRequest getObjectRequest = new GetObjectRequest() {BucketName = bucketName, Key = objectName};
                      getObjectRequest.SetRange(0, 5);
                      bosObject = client.GetObject(getObjectRequest);
                      objectContent = bosObject.ObjectContent;
                      content = new StreamReader(objectContent).ReadToEnd();
                      Console.WriteLine(content); // Your<SampleData> 
          
                      // Obtain byte[]contents directly via GetObjectContent 
                      byte[] bytes = client.GetObjectContent(bucketName, objectName);
                      content = Encoding.Default.GetString(bytes);
                      Console.WriteLine(content); // 您传入的<SampleData>
          
                      // Download object contents to file
                      FileInfo fileInfo = new FileInfo("my file path and name");
                      client.GetObject(bucketName, objectName,fileInfo );
                      content = File.ReadAllText(fileInfo.FullName);
                      Console.WriteLine(content); // 您传入的<SampleData>
          
                      // Only obtain meta of object, but not the contents
                      ObjectMetadata objectMetadata = client.GetObjectMetadata(bucketName, objectName);
                      Console.WriteLine(objectMetadata.ContentLength); 
          
                      Console.ReadKey();
                      
                      // Generate url, and directly download and print the object contents via this url
                      string url = client.GeneratePresignedUrl(bucketName, objectName, 60).AbsoluteUri;
                      using (WebClient webClient = new WebClient())
                      {
                          using (Stream stream = webClient.OpenRead(url))
                          using (StreamReader streamReader = new StreamReader(stream))
                          {
                              string response = streamReader.ReadToEnd();
                              Console.WriteLine(response);  // Your <SampleData>
                          }
                      }
                  }
          
                  private static BosClient GenerateBosClient()
                  {
                      const string accessKeyId = <AccessKeyID>; // 您的Access Key ID
                      const string secretAccessKey = <SecretAccessKey>; // Your Secret Access Key
                      const string endpoint = <EndPoint>; // Specify BOS service domain name
          
                      // Initialize a BosClient
                      BceClientConfiguration config = new BceClientConfiguration();
                      config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
                      config.Endpoint = endpoint;
          
                      return new BosClient(config);
                  }
              }
          }

          Delete Object

          • Basic procedure

            1.Create an instance of BOSClient class 2.Call BosClient.Deleteobject() method. When calling this method, you need to provide bucket name, object name, etc.

          • Sample Code

              public void DeleteObject(BosClient client, string bucketName, string objectKey)
              {
              // Delete Object
              client.DeleteObject(bucketName, objectKey); 
              }
          • Complete example

              using System;
              using System.Collections.Generic;
              using System.IO;
              using System.Linq;
              using System.Net;
              using System.Text;
              using BaiduBce;
              using BaiduBce.Auth;
              using BaiduBce.Services.Bos;
              using BaiduBce.Services.Bos.Model;
              
              namespace DotnetSample
              {
                  internal class DeleteObjectSample
                  {
                      private static void Main(string[] args)
                      {
                          BosClient client = GenerateBosClient();
                          const string bucketName = <BucketName>;     //Designate the name of the bucket
              
                          // Initialization: Create sample bucket and object 
                          client.CreateBucket(bucketName); 
                          string objectName = <ObjectKey>;
                          client.PutObject(bucketName, objectName, <Sampledata>);
              
                          // Delete Object
                          client.DeleteObject(bucketName, objectName);
                      }
              
                      private static BosClient GenerateBosClient()
                      {
                          const string accessKeyId = <AccessKeyID>; // Your Access Key ID
                          const string secretAccessKey = <SecretAccessKey>; // Your Secret Access Key
                          const string endpoint = <EndPoint>; // Specify BOS service domain name
              
                          // Initialize a BosClient 
                          BceClientConfiguration config = new BceClientConfiguration();
                          config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
                          config.Endpoint = endpoint;
              
                          return new BosClient(config);
                      }
                  }
              }

          Copy Object

          Simply Copy Object

          • Basic procedure

            1.Create an instance of the BOSClient class. 2.Execute BOSClient.copyobject( ) method.

          • Sample Code

              public void CopyObject(BosClient client, String srcBucketName, String srcKey, String destBucketName,
              String destKey)
              {
                 // Copy Object
                 CopyObjectResponse copyObjectResponse = client.CopyObject(srcBucketName, srcKey, destBucketName, destKey);
              
                 // Print results
                 Console.WriteLine("ETag: " + copyObjectResponse.ETag + " LastModified: " + copyObjectResponse.LastModified);
              }

            Note: The copyobject method returns a 'CopyObjectResponse' object containing the ETag of the new object and the modification time.

          • Complete example

            See [complete example](#complete example-3).

          Copy Object through CopyObjectRequest

          You can also copy object through CopyObjectRequest. This feature is typically used in the following scenarios:

          • Copy an object but reset the meta.
          • Reset the meta of an existing object (set Src and Des to the same object).
          • Basic procedure

            1.Create an instance of the CopyObjectRequest class, introducing into the <SrcBucketName>, <SrcKey>, <DestBucketName> and <DestKey> parameters. 2.Create an instance of ObjectMetadata.

          • Sample Code

              // Initialize BosClient. 
              BosClient client = ...; 
                
              // Create CopyObjectRequest. 
              CopyObjectRequest copyObjectRequest = new CopyObjectRequest() 
              { 
                SourceBucketName = srcBucketName, 
                SourceKey = srcKey, 
                BucketName = destBucketName, 
                Key = destKey 
              }; 
              
              // Create Metadata. 
              Dictionary<String, String> userMetadata = new Dictionary<String, String>(); 
              userMetadata["usermetakey"] = "usermetavalue"; 
              ObjectMetadata ObjectMetadata = new ObjectMetadata() 
              { 
                UserMetadata = userMetadata 
              }; 
              copyObjectRequest.NewObjectMetadata = ObjectMetadata; 
              
              // Copy object and print a new ETag 
              CopyObjectResponse copyObjectResponse = client.Copyobject(copyObjectRequest); 
              Console.WriteLine("ETag: " + copyObjectResponse.ETag + " LastModified: " + copyObjectResponse.LastModified); 

            Note: You can modify the objectMeta of object through CopyObjectRequest and set MatchingETagConstraints parameters.

          • Complete example

            See [complete example](#complete example-3).

          Complete Example

          The following sample code demonstrates the complete process of simply copying object and copying object via CopyObjectRequest:

          using System;
          using System.Collections.Generic;
          using System.IO;
          using System.Linq;
          using System.Net;
          using System.Text;
          using BaiduBce;
          using BaiduBce.Auth;
          using BaiduBce.Services.Bos;
          using BaiduBce.Services.Bos.Model;
          
          namespace DotnetSample
          {
              internal class CopyObjectSample
              {
                  private static void Main(string[] args)
                  {
                      BosClient client = GenerateBosClient();
                      const string bucketName = <SrcBucketName>; 
          
                      // Initialization, create sample bucket and object 
                      client.CreateBucket(bucketName); 
                      string objectName = <SrcObjectKey>;
                      client.PutObject(bucketName, objectName, <SrcSampleData>);
          
                      //Copy ordinarily and print results
                      string newObjectName = <DesObjectKey>;
                      CopyObjectResponse copyObjectResponse = client.CopyObject(bucketName, objectName, bucketName,
                          newObjectName);
                          
                      Console.WriteLine(Encoding.Default.GetString(client.GetObjectContent(bucketName, newObjectName)));
          
                      // Copy and set a new meta
                      newObjectName = <DesObjectKey>;
                      CopyObjectRequest copyObjectRequest = new CopyObjectRequest()
                      {
                          SourceBucketName = bucketName,
                          SourceKey = objectName,
                          BucketName = bucketName,
                          Key = newObjectName
                      };
                      Dictionary<String, String> userMetadata = new Dictionary<String, String>();
                      userMetadata["usermetakey"] = "usermetavalue";
                      ObjectMetadata objectMetadata = new ObjectMetadata()
                      {
                          UserMetadata = userMetadata
                      };
                      copyObjectRequest.NewObjectMetadata = objectMetadata;
                      client.CopyObject(copyObjectRequest);
                      // usermetavalue
                      Console.WriteLine(client.GetObjectMetadata(bucketName, newObjectName).UserMetadata["usermetakey"]);
                  }
          
                  private static BosClient GenerateBosClient()
                  {
                      const string accessKeyId = <AccessKeyID>; // Your Access Key ID
                      const string secretAccessKey = <SecretAccessKey>; // Your Secret Access Key
                      const string endpoint = <EndPoint>; // Specify BOS service domain name 
          
                      // Initialize a BosClient 
                      BceClientConfiguration config = new BceClientConfiguration();
                      config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
                      config.Endpoint = endpoint;
          
                      return new BosClient(config);
                  }
              }
          }

          Multipart Upload of Object

          In addition to uploading files to BOS through putObject () method, BOS also provides another upload mode: Multipart Upload. You can use the Multipart Upload mode in the following application scenarios (but not limited to this), such as:

          • Breakpoint upload support is required.
          • The file to upload is larger than 5 GB.
          • The network conditions are poor, and the connection with BOS servers is often disconnected.
          • The file needs to be uploaded streaming.
          • The size of the uploaded file cannot be determined before uploading it.

          Complete Multipart Upload

          Assume a file local path of d:\\sample.txt , and because the file is large, it is transmitted to BOS in multipart.

          • Basic procedure

            1.Create an instance of BOSClient class 2.Call BosClient.InitiateMultipartUpload() method. When calling this method, you need to provide bucket name and object name, and this method returns an UploadId, which is needed in the subsequent steps. 3.Call BosClient.UploadPart method for many times, and upload the files in multipart for many times. When calling this method, you need to provide bucket name, object name, UploadId, multipart number, multipart size, multipart contents, etc. Calling this method returns the multipart number and ETag each time, which will be used in the subsequent steps. 4.Call BosClient.CompleteMultipartUpload() method to complete the multipart upload. When calling this method, you need to provide bucket name, object name, UploadId and serial number and ETag of each part. 5.During upload, you can obtain all parts uploaded in the specified UploadId using BosClient.ListParts() method; you can also obtain the uncompleted UploadId in the specified bucket with BosClient.ListMultipartUploads() method.

          Initialize Multipart Upload

          Use the initiateMultipartUpload method to initialize a Multipart Upload event:

          • Sample Code

              // Start Multipart Upload
              InitiateMultipartUploadRequest initiateMultipartUploadRequest =
                       new InitiateMultipartUploadRequest() {BucketName = bucketName, Key = objectKey};
              InitiateMultipartUploadResponse initiateMultipartUploadResponse =
                       client.InitiateMultipartUpload(initiateMultipartUploadRequest);
              
              // Print UploadId
              Console.WriteLine("UploadId: " + initiateMultipartUploadResponse.UploadId);

            Note: The returned result of initiateMultipartUpload contains UploadId . It is the unique identification to distinguish the Multipart Upload events. We will use it in later operations.

          Upload in parts

          Upload the files in multipart.

          • Sample Code

              // Set each part to 5Mb. 
              long partSize = 1024 * 1024 * 5L; 
              
              FileInfo partFile = new FileInfo("my file"); 
              
              // Calculate the number of part. 
              int partCount = (int) (partFile.Length / partSize); 
              if (partFile.Length % partSize != 0) 
              { 
                 partCount++; 
              } 
              
              // Create a List to save the ETag and PartNumber uploaded in parts. 
              List<PartETag> partETags = new List<PartETag>(); 
              
              for (int i = 0; i< partCount; i++) 
              { 
                 // Get file stream. 
                 Stream stream = partFile.OpenRead(); 
              
                 // Skip to the beginning of each part. 
                 long skipBytes = partSize * i; 
                 stream.Seek(skipBytes, SeekOrigin.Begin); 
              
                 // Calculate the size of each part. 
                 long size = Math.Min(partSize, partFile.Length - skipBytes); 
              
                 // Create UploadPartRequest, and upload part. 
                 uploadPartRequest uploadPartRequest = new UploadPartRequest(); 
                 uploadPartRequest.BucketName = BucketName; 
                 uploadPartRequest.Key = ObjectKey; 
                 uploadPartRequest.UploadId = initiateMultipartUploadResponse.UploadId; 
                 uploadPartRequest.InputStream = stream; 
                 uploadPartRequest.PartSize = size; 
                 uploadPartRequest.PartNumber = i + 1; 
                 UploadPartResponse uploadPartResponse = client.UploadPart(uploadPartRequest); 
              
                 // Save the returned PartETag to the List. 
                 partETags.Add(new PartETag() 
                 { 
                    ETag = uploadPartResponse.ETag, 
                    PartNumber = uploadPartResponse.PartNumber 
                  }); 
              
                 // Close the file. 
                 stream.Close(); 
                 } 

            Note:

            The core of the code above is to call UploadPart method to upload each part, but you should pay attention to the following:

            • Multipart upload requires that except for the final Part, other Parts should be more than or equal to 5MB. However, the interface of Upload Part will not check the size of Part uploaded; the size will be checked only when Multipart Upload is completed.
            • To ensure no error occurs to data in the network transmission process, it is recommended that you use the Content-MD5 value returned by each part BOS to verify the correctness of the uploaded part data respectively after UploadPart. When all part data is combined into one object, it no longer contains the MD5 value.
            • Part numbers range from 1 to 10,000. If this range is exceeded, BOS will return the error code of InvalidArgument.
            • Every time you upload a Part, locate the stream to the location corresponding to the beginning of the uploaded part.
            • Every time a Part is uploaded, the returned result of BOS will contain a PartETag object. It is a combination of the Etag and the PartNumber of the uploaded part. It will be used in the next steps to complete Multipart Upload, so it needs to be saved. In general, these PartETag objects will be saved to the List.

          Complete Multipart Upload

          • Sample Code

              CompleteMultipartUploadRequest completeMultipartUploadRequest =
              new CompleteMultipartUploadRequest()
              {
                 BucketName = bucketName,
                 Key = objectKey,
                 UploadId = initiateMultipartUploadResponse.UploadId,
                 PartETags = partETags
              };
              
              // Complete Multipart Upload
              CompleteMultipartUploadResponse completeMultipartUploadResponse =
              client.CompleteMultipartUpload(completeMultipartUploadRequest);
              
              // Print the ETag of object
              Console.WriteLine(completeMultipartUploadResponse.ETag);

            Note: The partETags in the code above are list of partETag saved in the Step 2, and after receiving the list of Part submitted by the users, BOS verifies the validity of each data part one by one. When all data Parts are verified, BOS will combine these data parts into a complete object.

          Cancel Multipart Upload Event

          You can use the abortMultipartUpload method to cancel Multipart Upload.

          • Sample Code

              AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest()
              {
                  BucketName = bucketName,
                  Key = objectKey,
                  UploadId = initiateMultipartUploadResponse.UploadId,
              };
              
              // Cancel Multipart Upload
              client.AbortMultipartUpload(abortMultipartUploadRequest);	 

          Get Unfinished Multipart Upload Event

          You can use the ListMultipartUploads method to get unfinished Multipart Upload events in the bucket.

          • Sample Code

              ListMultipartUploadsRequest listMultipartUploadsRequest =
                   new ListMultipartUploadsRequest() {BucketName = bucketName};
              
              // Get all upload events in the bucket
              ListMultipartUploadsResponse listMultipartUploadsResponse =
                   client.ListMultipartUploads(listMultipartUploadsRequest);
              
              // Traverse all uploaded events
              foreach (MultipartUploadSummary multipartUpload in listMultipartUploadsResponse.Uploads)
              {
                 Console.WriteLine("Key: " + multipartUpload.Key + " UploadId: " + multipartUpload.UploadId);
              }

            Note:

            • By default, if the number of multipart upload events in bucket is more than 1,000, only 1,000 events are returned, and the value of IsTruncated in the returned result is True, and meanwhile, NextKeyMarker is returned as the starting point of the next reading.
            • For more Multipart Upload events, you can use the KeyMarker parameter to read them by several times.

          Get All Uploaded Part Information

          You can use the ListParts method to get all uploaded parts in an uploaded event.

          • Sample Code

              ListPartsRequest listPartsRequest = new ListPartsRequest() 
              { 
                  BucketName = BucketName, 
                  Key = ObjectKey, 
                  UploadId = initiateMultipartUploadResponse.UploadId, 
              }; 
              
              // Get information about all uploaded Parts. 
              ListPartsResponse listPartsResponse = client.ListParts(listPartsRequest); 
              
              // Traverse all Parts. 
              foreach (PartSummary part in listPartsResponse.Parts) 
              { 
                   Console.WriteLine("PartNumber: " + part.PartNumber + " ETag: " + part.ETag); 
              } 

            Note:

            • By default, if the number of multipart upload events in bucket is more than 1,000, only 1,000 parts are returned, and the value of IsTruncated in the returned result is True, and meanwhile, NextPartNumberMarker is returned as the starting point of the next reading.
            • For more uploaded part information, you can use the PartNumberMarker parameter to read it by several times.

          Complete Example

          The following sample code demonstrates the complete process of multipart upload:

          using System; 
          using System.Collections.Generic; 
          using System.IO; 
          using System.Linq; 
          using System.Text; 
          using BaiduBce; 
          using BaiduBce.Auth; 
          using BaiduBce.Services.Bos; 
          using BaiduBce.Services.Bos.Model; 
          
          namespace DotnetSample 
          { 
              internal class MultiUploadSample 
              { 
                  private static void Main(string[] args) 
                  { 
                      BosClient client = GenerateBosClient(); 
                      const string bucketName =<BucketName>;    //bucket name 
          
                      // Initialization: Create a sample bucket 
                      client.Createbucket(bucketName); 
                      string ObjectKey =<ObjectKey>; 
          
                      // Start Multipart Upload. 
                      InitiateMultipartUploadRequest initiateMultipartUploadRequest = 
                          new InitiateMultipartUploadRequest() { BucketName = bucketName, Key = objectKey }; 
                      InitiateMultipartUploadResponse initiateMultipartUploadResponse = 
                          client.InitiateMultipartUpload(initiateMultipartUploadRequest); 
          
                      // Get Multipart Upload inside the bucket 
                      ListMultipartUploadsRequest listMultipartUploadsRequest = 
                          new ListMultipartUploadsRequest() { BucketName = bucketName };       
                      ListMultipartUploadsResponse listMultipartUploadsResponse = 
                          client.ListMultipartUploads(listMultipartUploadsRequest); 
                      foreach (MultipartUploadSummary multipartUpload in listMultipartUploadsResponse.Uploads) 
                      { 
                          Console.WriteLine("Key: " + multipartUpload.Key + " UploadId: " + multipartUpload.UploadId); 
                      } 
          
                      // Multipart upload, and set each part as 5Mb first. 
                      long partSize = 1024 * 1024 * 5L; 
                      FileInfo partFile = new FileInfo("d:\\lzb\\sample"); 
                      
                      // Calculate the number of part. 
                      int partCount = (int)(partFile.Length / partSize); 
                      if (partFile.Length % partSize != 0) 
                      { 
                          partCount++; 
                      } 
                      
                      // Create a List to save the ETag and PartNumber uploaded in parts. 
                      List<PartETag> partETags = new List<PartETag>(); 
                      for (int i = 0; i< partCount; i++) 
                      { 
                          // Get file stream 
                          Stream stream = partFile.OpenRead(); 
                          
                          // Skip to the beginning of each part. 
                          long skipBytes = partSize * i; 
                          stream.Seek(skipBytes, SeekOrigin.Begin); 
                          
                          // Calculate the size of each part. 
                          long size = Math.Min(partSize, partFile.Length - skipBytes); 
                          
                          // Create UploadPartRequest, and upload part. 
                          UploadPartRequest uploadPartRequest = new UploadPartRequest(); 
                          uploadPartRequest.BucketName = BucketName; 
                          uploadPartRequest.Key = ObjectKey; 
                          uploadPartRequest.UploadId = initiateMultipartUploadResponse.UploadId; 
                          uploadPartRequest.InputStream = stream; 
                          uploadPartRequest.PartSize = size; 
                          uploadPartRequest.PartNumber = i + 1; 
                          UploadPartResponse uploadPartResponse = client.UploadPart(uploadPartRequest); 
                          
                          // Save the returned PartETag to the List. 
                          partETags.Add(new PartETag() 
                          { 
                              ETag = uploadPartResponse.ETag, 
                              PartNumber = uploadPartResponse.PartNumber 
                          }); 
                          
                          // Close the file. 
                          stream.Close(); 
                      } 
          
                      // Obtain all Upload Parts of UploadId 
                      ListPartsRequest listPartsRequest = new ListPartsRequest() 
                      { 
                          BucketName = bucketName, 
                          Key = objectKey, 
                          UploadId = initiateMultipartUploadResponse.UploadId, 
                      }; 
                      
                      // Get information about all uploaded Parts. 
                      ListPartsResponse listPartsResponse = client.ListParts(listPartsRequest); 
                      
                      // Traverse all Parts. 
                      foreach (PartSummary part in listPartsResponse.Parts) 
                      { 
                          Console.WriteLine("PartNumber: " + part.PartNumber + " ETag: " + part.ETag); 
                      } 
          
                      // Complete Multipart Upload. 
                      CompleteMultipartUploadRequest completeMultipartUploadRequest = 
                          new CompleteMultipartUploadRequest() 
                          { 
                              BucketName = bucketName, 
                              Key = objectKey, 
                              UploadId = initiateMultipartUploadResponse.UploadId, 
                              PartETags = partETags 
                          }; 
                      CompleteMultipartUploadResponse completeMultipartUploadResponse = 
                          client.CompleteMultipartUpload(completeMultipartUploadRequest); 
                      Console.WriteLine(completeMultipartUploadResponse.ETag);                  
                      Console.ReadKey(); 
                  } 
          
                  private static BosClient GenerateBosClient() 
                  { 
                      const string accessKeyId =<AccessKeyID>; // Your Access Key ID 
                      const string secretAccessKey =<SecretAccessKey>; // Your Secret Access Key 
                      const string endpoint =<EndPoint>; // Specify BOS service domain name 
          
                      // Initialize a BosClient 
                      BceClientConfiguration config = new BceClientConfiguration(); 
                      config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey); 
                      config.Endpoint = endpoint; 
          
                      return new BosClient(config); 
                  } 
              } 
          }
          Previous
          Bucket Management
          Next
          Exception Handling