Object management
Upload Object
Simplest upload
-
Basic workflow
- Instantiate the BOSClient class.
- The BOSClient.putObject() method allows objects to be uploaded in four ways: file, data stream, binary string, and plain string.
- For the returned PutObjectResponse instance, call getETag() to get the successfully uploaded ETag.
-
Example code
Java1// Get specified file 2 File file = new File(<FilePath>); //Specify the file path 3 // Upload object as a file 4PutObjectResponse putObjectFromFileResponse = 5 client.putObject(<BucketName>, <ObjectKey>, file); 6 // Obtain data stream 7InputStream inputStream = new FileInputStream(<FilePath>); 8 // Upload an object in the form of a data stream 9PutObjectResponse putObjectResponseFromInputStream = 10 client.putObject(<BucketName>, <ObjectKey>, inputStream); 11 // Upload object as binary string 12PutObjectResponse putObjectResponseFromByte = 13 client.putObject(<BucketName>, <ObjectKey>, <byte>); 14 // Upload object in string form 15PutObjectResponse putObjectResponseFromString = 16 client.putObject(<BucketName>, <ObjectKey>, <string>); 17 // Print ETag 18System.out.println(putObjectFromFileResponse.getETag());Plain Text1> **Note**: Objects are uploaded to BOS as files, and the putObject function supports uploading objects with a size not exceeding 5GB. After the putObject request is processed successfully, BOS will return the ETag of the object in the Header as the file identifier. -
Complete example
Java1import java.io.File; 2import java.io.FileInputStream; 3import java.io.FileNotFoundException; 4import java.io.InputStream; 5import android.app.Activity; 6import android.os.Bundle; 7import com.baidubce.BceClientException; 8import com.baidubce.BceServiceException; 9import com.baidubce.auth.DefaultBceCredentials; 10import com.baidubce.development.R; 11import com.baidubce.services.bos.BosClient; 12import com.baidubce.services.bos.BosClientConfiguration; 13import com.baidubce.services.bos.model.PutObjectResponse; 14 15public class ExampleActivity extends Activity { 16 private String bucketName = <BucketName>; 17 private String objectKey = <ObjectKey>; 18 byte[] b = null; 19 String str = <PutString>; 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 new Thread(new Runnable() { 25 @Override 26 public void run() { 27 try { 28 BosClientConfiguration config = new BosClientConfiguration(); 29 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 30 config.setEndpoint(<EndPoint>); 31 BosClient client = new BosClient(config); 32 // Get specified file 33 File file = new File("/path/to/file.zip"); 34 // Upload object as a file 35 PutObjectResponse putObjectFromFileResponse = client.putObject(<BucketName>, <ObjectKey>, file); 36 // Obtain data stream 37 InputStream inputStream = new FileInputStream("/path/to/test.zip"); 38 // Upload an object in the form of a data stream 39 PutObjectResponse putObjectResponseFromInputStream = client.putObject(<BucketName>, <ObjectKey>, inputStream); 40 // Upload object as binary string 41 PutObjectResponse putObjectResponseFromByte = client.putObject(<BucketName>, <ObjectKey>, b); 42 // Upload object in string form 43 PutObjectResponse putObjectResponseFromString = client.putObject(<BucketName>, <ObjectKey>, str); 44 // Print ETag 45 System.out.println(putObjectFromFileResponse.getETag()); 46 } catch (BceServiceException e) { 47 System.out.println("Error ErrorCode: " + e.getErrorCode()); 48 System.out.println("Error RequestId: " + e.getRequestId()); 49 System.out.println("Error StatusCode: " + e.getStatusCode()); 50 System.out.println("Error Message: " + e.getMessage()); 51 System.out.println("Error ErrorType: " + e.getErrorType()); 52 } catch (BceClientException e) { 53 System.out.println("Error Message: " + e.getMessage()); 54 } catch (FileNotFoundException e) { 55 // TODO Auto-generated catch block 56 e.printStackTrace(); 57 } 58 } 59 }).start(); 60}}
Set the Copy attributes of an object
The CopyObject API copies an existing object to a new one. During the process, it checks the source object's ETag or modification status to decide whether to proceed with the operation. The detailed parameters are as follows:
| Name | Types | Description | Whether required |
|---|---|---|---|
| x-bce-copy-source-if-match | String | If the ETag value of the source object matches the ETag value provided by the user, the copy operation is performed; otherwise, it fails. | No |
| x-bce-copy-source-if-none-match | String | If the ETag value of the source object does not match the ETag value provided by the user, the copy operation is performed; otherwise, it fails. | No |
| x-bce-copy-source-if-unmodified-since | String | If the source object has not been modified since x-bce-copy-source-if-unmodified-since, the copy operation will proceed; otherwise, it will fail. | No |
| x-bce-copy-source-if-modified-since | String | If the source object has been modified since x-bce-copy-source-if-modified-since, the copy operation will proceed; otherwise, it will fail. | No |
-
Example code
Java1// Initialize BosClient 2BosClient client = ...; 3 // Create CopyObjectRequest object 4CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketName, srcKey, destBucketName, destKey); 5 // Set new Metadata 6Map<String, String> userMetadata = new HashMap<String, String>(); 7userMetadata.put("<user-meta-key>","<user-meta-value>"); 8ObjectMetadata meta = new ObjectMetadata(); 9meta.setUserMetadata(userMetadata); 10copyObjectRequest.setNewObjectMetadata(meta); 11//copy-source-if-match 12copyObjectRequest.withETag("111111111183bf192b57a4afc76fa632"); 13//copy-source-if-none-match 14copyObjectRequest.withNoMatchingETagConstraint("111111111183bf192b57a4afc76fa632"); 15Date modifiedSinceConstraint = new Date(); 16SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK); 17df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT")); 18String date = df.format(modifiedSinceConstraint); 19//copy-source-if-modified-since 20copyObjectRequest.withModifiedSinceConstraint(date); 21//copy-source-if-unmodified-since 22copyObjectRequest.withUnmodifiedSinceConstraint(date); 23 // Copy Object 24CopyObjectResponse copyObjectResponse = client.copyObject(copyObjectRequest);
Set object's HTTP Header
BOS allows setting Http Headers when uploading an object.
-
Basic workflow
- Create an ObjectMetadata instance.
- Use methods like setContentLength() and setContentType() to configure Http Headers.
- Pass the metadata as a parameter when calling client.putObject().
-
Example code
Java1// Create an instance of the ObjectMetadata class 2ObjectMetadata meta = new ObjectMetadata(); 3 // Set ContentLength size 4meta.setContentLength(<Length>); 5 // Set ContentType 6meta.setContentType("application/json"); 7client.putObject(<BucketName>, <ObjectKey>, content, meta);Plain Text1> **Note:** Headers can be set with attributes such as "Cache-Control", "Content-Encoding", "Content-Disposition", and "Expires" -
Complete example
Java1import java.io.FileInputStream; 2import java.io.FileNotFoundException; 3import java.io.InputStream; 4import android.app.Activity; 5import android.os.Bundle; 6import com.baidubce.BceClientException; 7import com.baidubce.BceServiceException; 8import com.baidubce.auth.DefaultBceCredentials; 9import com.baidubce.demo.R; 10import com.baidubce.services.bos.BosClient; 11import com.baidubce.services.bos.BosClientConfiguration; 12import com.baidubce.services.bos.model.ObjectMetadata; 13import com.baidubce.services.bos.model.PutObjectResponse; 14public class ExampleActivity extends Activity { 15private String bucketName = <BucketName>; 16private String objectKey = <ObjectKey>; 17@Override 18protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 new Thread(new Runnable() { 22 @Override 23 public void run() { 24 try { 25 BosClientConfiguration config = new BosClientConfiguration(); 26 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 27 config.setEndpoint(<EndPoint>); 28 BosClient client = new BosClient(config); 29 // Obtain data stream 30 InputStream inputStream = new FileInputStream("/path/to/test.zip"); 31 // Create an instance of the ObjectMetadata class 32 ObjectMetadata meta = new ObjectMetadata(); 33 // Set ContentLength size 34 meta.setContentLength(1000); 35 // Set ContentType 36 meta.setContentType("application/json"); 37 PutObjectResponse putObjectResponseFromInputStream = client.putObject(bucketName, objectKey, inputStream, meta); 38 // Print ETag 39 System.out.println(putObjectResponseFromInputStream.getETag()); 40 } catch (BceServiceException e) { 41 System.out.println("Error ErrorCode: " + e.getErrorCode()); 42 System.out.println("Error RequestId: " + e.getRequestId()); 43 System.out.println("Error StatusCode: " + e.getStatusCode()); 44 System.out.println("Error Message: " + e.getMessage()); 45 System.out.println("Error ErrorType: " + e.getErrorType()); 46 } catch (BceClientException e) { 47 System.out.println("Error Message: " + e.getMessage()); 48 } catch (FileNotFoundException e) { 49 // TODO Auto-generated catch block 50 e.printStackTrace(); 51 } 52 } 53 }).start(); 54}}
User-defined metadata
BOS supports user-defined metadata for object descriptions.
-
Basic workflow
- Create an ObjectMetadata instance.
- Use the addUserMetadata() method to add custom metadata to the Http Headers.
- Pass the metadata as a parameter when calling client.putObject().
-
Example code
Java1// Set the value of custom metadata name to my-data 2meta.addUserMetadata("name", "my-data"); 3 // Upload Object 4client.putObject(<BucketName>, <ObjectKey>, <Content>, meta);Plain Text1> **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
Java1import java.io.FileInputStream; 2import java.io.FileNotFoundException; 3import java.io.InputStream; 4import android.app.Activity; 5import android.os.Bundle; 6import com.baidubce.BceClientException; 7import com.baidubce.BceServiceException; 8import com.baidubce.auth.DefaultBceCredentials; 9import com.baidubce.demo.R; 10import com.baidubce.services.bos.BosClient; 11import com.baidubce.services.bos.BosClientConfiguration; 12import com.baidubce.services.bos.model.ObjectMetadata; 13import com.baidubce.services.bos.model.PutObjectResponse; 14public class ExampleActivity extends Activity { 15private String bucketName = <BucketName>; 16private String objectKey = <ObjectKey>; 17@Override 18protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 new Thread(new Runnable() { 22 @Override 23 public void run() { 24 try { 25 BosClientConfiguration config = new BosClientConfiguration(); 26 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 27 config.setEndpoint(<EndPoint>); 28 BosClient client = new BosClient(config); 29 // Obtain data stream 30 InputStream inputStream = new FileInputStream("/path/to/test.zip"); 31 // Create an instance of the ObjectMetadata class 32 ObjectMetadata meta = new ObjectMetadata(); 33 // Custom metadata 34 meta.addUserMetadata("name", "my-data"); 35 PutObjectResponse putObjectResponseFromInputStream = client.putObject(<BucketName>, <ObjectKey>, inputStream, meta); 36 // Print ETag 37 System.out.println(putObjectResponseFromInputStream.getETag()); 38 } catch (BceServiceException e) { 39 System.out.println("Error ErrorCode: " + e.getErrorCode()); 40 System.out.println("Error RequestId: " + e.getRequestId()); 41 System.out.println("Error StatusCode: " + e.getStatusCode()); 42 System.out.println("Error Message: " + e.getMessage()); 43 System.out.println("Error ErrorType: " + e.getErrorType()); 44 } catch (BceClientException e) { 45 System.out.println("Error Message: " + e.getMessage()); 46 } catch (FileNotFoundException e) { 47 // TODO Auto-generated catch block 48 e.printStackTrace(); 49 } 50 } 51 }).start(); 52}}
Upload object in append mode
BOS provides an AppendObject feature for uploading files in append mode, making it ideal for scenarios like log appending, live streaming, and real-time video uploads. Objects created via AppendObject belong to the Appendable Objects category, allowing additional data to be appended. In contrast, objects uploaded using PutObject are classified as Normal Objects and do not support data appending. An AppendObject has a size limit between 0 and 5GB.
-
Example code
Java1public void AppendObject(BosClient client, String bucketName, String objectKey, byte[] byte1, String string1) { 2 // Get specified file 3 File file = new File("/path/to/file.zip"); 4 // Obtain data stream 5 InputStream inputStream = new FileInputStream("/path/to/test.zip"); 6 // Upload object as a file 7 AppendObjectResponse appendObjectFromFileResponse = client.appendObject(bucketName, objectKey, file); 8 // Upload an object in the form of a data stream 9 AppendObjectResponse appendObjectResponseFromInputStream = client.appendObject(bucketName, objectKey, inputStream); 10 // Upload object as binary string 11 AppendObjectResponse appendObjectResponseFromByte = client.appendObject(bucketName, objectKey, byte1); 12 // Upload object in string form 13 AppendObjectResponse appendObjectResponseFromString = client.appendObject(bucketName, objectKey, string1); 14 // Append content to AppendFile 15 Long nextOffset = appendObjectFromFileResponse.getNextAppendOffset(); 16 AppendObjectRequest request =new AppendObjectRequest(bucketName, objectKey, 17 RestartableInputStream.wrap(string1.getBytes())); 18 request.withOffset(nextOffset); 19 AppendObjectResponse appendResponse = client.appendObject(request); 20}
Obtain upload progress
The Android SDK supports providing real-time upload progress information during the upload. It currently supports four APIs: PutObject, AppendObject, UploadPart and PutSuperObjectFromFile Using the progress upload interface requires constructing the corresponding Request (PutObjectRequest, AppendObjectRequest, UploadPartRequest and PutSuperObjectRequest).
The SDK offers an upload progress callback API that enables you to define operations to execute during uploads, such as updating the UI.
1public interface BceProgressCallback<T extends AbstractBceRequest> {
2 // request is the upload request
3 // currentSize is the current upload size (unit: byte)
4 // totalSize is the total size to be uploaded in this request (unit: byte)
5 void onProgress(T request, long currentSize, long totalSize);
6}
-
PutObject example code:
Java1PutObjectRequest request = new PutObjectRequest(this.bucketName, "test", file); 2ObjectMetadata objectMetadata = new ObjectMetadata(); 3objectMetadata.setContentType("text/plain"); 4request.setObjectMetadata(objectMetadata); 5request.setProgressCallback(new BosProgressCallback<PutObjectRequest>() { 6 @Override 7 public void onProgress(PutObjectRequest request, long currentSize, long totalSize) { 8 Log.e(currentSize + "", totalSize + ""); 9 } 10}); 11String eTag = this.client.putObject(request).getETag(); -
AppendObject example code:
Java1ObjectMetadata objectMetadata = new ObjectMetadata(); 2objectMetadata.setContentType("text/plain"); 3AppendObjectRequest request = new AppendObjectRequest(this.bucketName, "test", file); 4request.setObjectMetadata(objectMetadata); 5AppendObjectResponse response = this.client.appendObject(request); 6Long nextOffset = response.getNextAppendOffset(); 7request.withOffset(nextOffset); 8request.setProgressCallback(new BosProgressCallback<AppendObjectRequest>() { 9 @Override 10 public void onProgress(AppendObjectRequest request, long currentSize, long totalSize) { 11 Log.e(currentSize + "", totalSize + ""); 12 } 13}); 14response = this.client.appendObject(request); -
UploadPart example code:
Java1UploadPartRequest request = new UploadPartRequest().withBucketName(this.bucketName) 2 .withKey("test").withUploadId(uploadId).withPartNumber(1).withPartSize(8000) 3 .withInputStream(fis); 4request.setProgressCallback(new BosProgressCallback<UploadPartRequest>() { 5 @Override 6 public void onProgress(UploadPartRequest request, long currentSize, long totalSize) { 7 Log.e(currentSize + "", totalSize + ""); 8 } 9}); 10UploadPartResponse response = this.client.uploadPart(request); -
PutSuperObjectFromFile example code:
Java1PutSuperObjectRequest request = new PutSuperObjectRequest().withBucketName(this.bucketName) 2 .withKey("test").withPartSize(1024*1024*2L).withFile(file); 3request.setProgressCallback(new BosProgressCallback<PutSuperObjectRequest>() { 4 @Override 5 public void onProgress(PutSuperObjectRequest request, long currentSize, long totalSize) { 6 Log.e(currentSize + "", totalSize + ""); 7 } 8}); 9PutSuperObjectResponse response = this.client.putSuperObjectFromFile(request);By default, the SDK’s upload progress callback granularity is 2,048 bytes. You can adjust the granularity to better suit your application using the provided methods.
BosClientConfiguration config=new BosClientConfiguration(); config.setUploadSegmentPart(1024);
Note: This value must be between 1-8192. If outside this range, Android SDK will forcibly set it to 2048.
Cancellation interface
Android SDK supports canceling this request during the request process. The remaining data in the upload request will not be uploaded to BOS. Canceling the operation in the download request will close the connection, and the executing operation will throw Request is canceled! exception.
All BOS operations support cancellation interfaces, but completed requests cannot be canceled, and no exceptions will be thrown for completed operations
1// Take the upload interface cancellation as an example
2final PutObjectRequest req = new PutObjectRequest(<BucketName>, <ObjectName>,
3 new FileInputStream(<FilePath>));
4Runnable cancelTask = new Runnable() {
5 @Override
6 public void run() {
7 try {
8 Thread.sleep(100);
9 } catch (InterruptedException e) {
10 e.printStackTrace()
11 }
12 // Cancel the current upload request
13 req.cancel();
14 }
15};
16new Thread(cancelTask).start();
17client.putObject(req);
Synchronous callback
The Android SDK supports the BOS server-side synchronous callback API. By setting the process parameter in the PutObjectRequest, the BOS server can actively trigger the callback API after upload completion to notify the client.
1PutObjectRequest request = new PutObjectRequest({bucket}, {object}, {inputStream});
2 // Set the x-bce-process parameter
3request.setProcess({x-bce-process});
4PutObjectResponse response = client.putObject(request);
5 // Obtain the returned http status code
6int statusCode = response.getHttpResponse().getStatusCode();
7 // Obtain the data returned by the callback API
8string callbackString = response.getServerCallbackReturnBody();
View the object in the bucket.
Simple query
View the list of objects in a bucket.
-
Basic workflow
- Instantiate the BOSClient class.
- Calling the BOSClient.listObjects(bucketName) method returns an instance of the ListObjectsResponse class.
- For the ListObjectsResponse class, methods like getBuckets(), getOwner(), and getMetadata() can be executed.
-
Example code
Java1// Obtain all object information under the specified bucket 2ListObjectsResponse listing = client.listObjects(<BucketName>); 3 // Traverse all objects 4for (BosObjectSummary objectSummary : listing.getContents()) { 5 System.out.println("ObjectKey: " + objectSummary.getKey()); 6}Plain Text1> **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. 2> 3> - 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. 4> 5> 6> - To retrieve more objects, use the Marker parameter for reading in batches. Refer to [Extended Query](#Extended query). -
Complete example
Java1import android.app.Activity; 2import android.os.Bundle; 3import com.baidubce.BceClientException; 4import com.baidubce.BceServiceException; 5import com.baidubce.auth.DefaultBceCredentials; 6import com.baidubce.demo.R; 7import com.baidubce.services.bos.BosClient; 8import com.baidubce.services.bos.BosClientConfiguration; 9import com.baidubce.services.bos.model.BosObjectSummary; 10import com.baidubce.services.bos.model.ListObjectsResponse; 11public class ExampleActivity extends Activity { 12private String bucketName = <BucketName>; 13@Override 14protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 new Thread(new Runnable() { 18 @Override 19 public void run() { 20 try { 21 BosClientConfiguration config = new BosClientConfiguration(); 22 config.setCredentials(new DefaultBceCredentials(<AccessKeyId>, <SecretAccessKey>)); 23 config.setEndpoint(<EndPoint>); 24 BosClient client = new BosClient(config); 25 // Obtain all object information under the specified bucket 26 ListObjectsResponse listing = client.listObjects(<BucketName>); 27 // Traverse all objects 28 for (BosObjectSummary objectSummary : listing.getContents()) { 29 System.out.println("ObjectKey: " + objectSummary.getKey()); 30 } 31 } catch (BceServiceException e) { 32 System.out.println("Error ErrorCode: " + e.getErrorCode()); 33 System.out.println("Error RequestId: " + e.getRequestId()); 34 System.out.println("Error StatusCode: " + e.getStatusCode()); 35 System.out.println("Error Message: " + e.getMessage()); 36 System.out.println("Error ErrorType: " + e.getErrorType()); 37 } catch (BceClientException e) { 38 System.out.println("Error Message: " + e.getMessage()); 39 } 40 } 41 }).start(); 42}}
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
- Create an instance of the ListObjectsRequest class.
- Use methods like setDelimiter(), setMarker(), or setPrefix() in ListObjectsRequest to enable more advanced query functionalities.
- Create an instance of the BOSClient class and invoke the listObjects(listObjectsRequest) method.
-
Example code
Java1// Construct a ListObjectsRequest request 2ListObjectsRequest listObjectsRequest = new ListObjectsRequest(<BucketName>); 3 // Set extended query parameters 4listObjectsRequest.setDelimiter(<Delimiter>); 5listObjectsRequest.setMarker(<Marker>); 6... 7ListObjectsResponse listing = client.listObjects(listObjectsRequest);Plain Text1> **Note:** The above code calls an overloaded method from `listObjects` by passing a `ListObjectsRequest` to complete the request. -
Complete example
Example 1:
Java1import android.app.Activity; 2import android.os.Bundle; 3import com.baidubce.BceClientException; 4import com.baidubce.BceServiceException; 5import com.baidubce.auth.DefaultBceCredentials; 6import com.baidubce.demo.R; 7import com.baidubce.services.bos.BosClient; 8import com.baidubce.services.bos.BosClientConfiguration; 9import com.baidubce.services.bos.model.BosObjectSummary; 10import com.baidubce.services.bos.model.ListObjectsRequest; 11import com.baidubce.services.bos.model.ListObjectsResponse; 12public class ExampleActivity extends Activity { 13private String bucketName = <BucketName>; 14@Override 15protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 new Thread(new Runnable() { 19 @Override 20 public void run() { 21 try { 22 BosClientConfiguration config = new BosClientConfiguration(); 23 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 24 config.setEndpoint(<EndPoint>); 25 BosClient client = new BosClient(config); 26 // Construct a ListObjectsRequest request 27 ListObjectsRequest listObjectsRequest = new ListObjectsRequest(<BucketName>); 28 // Set parameters 29 listObjectsRequest.setDelimiter("/"); 30 listObjectsRequest.setMarker("123"); 31 listObjectsRequest.setMaxKeys(100); 32 listObjectsRequest.setPrefix("fun"); 33 // Obtain all eligible object information under the specified bucket 34 ListObjectsResponse listing = client.listObjects(listObjectsRequest); 35 // Traverse all objects 36 for (BosObjectSummary objectSummary : listing.getContents()) { 37 System.out.println("ObjectKey: " + objectSummary.getKey()); 38 } 39 } catch (BceServiceException e) { 40 System.out.println("Error ErrorCode: " + e.getErrorCode()); 41 System.out.println("Error RequestId: " + e.getRequestId()); 42 System.out.println("Error StatusCode: " + e.getStatusCode()); 43 System.out.println("Error Message: " + e.getMessage()); 44 System.out.println("Error ErrorType: " + e.getErrorType()); 45 } catch (BceClientException e) { 46 System.out.println("Error Message: " + e.getMessage()); 47 } 48 } 49 }).start(); 50}}Example II: A complete example using the NextMarker.
Java1import android.app.Activity; 2import android.os.Bundle; 3import com.baidubce.BceClientException; 4import com.baidubce.BceServiceException; 5import com.baidubce.auth.DefaultBceCredentials; 6import com.baidubce.demo.R; 7import com.baidubce.services.bos.BosClient; 8import com.baidubce.services.bos.BosClientConfiguration; 9import com.baidubce.services.bos.model.BosObjectSummary; 10import com.baidubce.services.bos.model.ListObjectsRequest; 11import com.baidubce.services.bos.model.ListObjectsResponse; 12public class ExampleActivity extends Activity { 13private String bucketName = <BucketName>; 14@Override 15protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 new Thread(new Runnable() { 19 @Override 20 public void run() { 21 try { 22 BosClientConfiguration config = new BosClientConfiguration(); 23 config.setCredentials(new DefaultBceCredentials(<AccessKeyId>, <SecretAccessKey>); 24 config.setEndpoint(<EndPoint>); 25 BosClient client = new BosClient(config); 26 ListObjectsRequest listObjectsRequest = new ListObjectsRequest(<BucketName>); 27 listObjectsRequest.setMarker(""); 28 listObjectsRequest.setMaxKeys(20); 29 listObjectsRequest.setDelimiter(BceConfig.BOS_DELIMITER); 30 ListObjectsResponse listObjectsResponse = client.listObjects(listObjectsRequest); 31 String nextMark = ""; 32 if(listObjectsResponse.isTruncated()){ 33 nextMark = listObjectsResponse.getNextMarker(); 34 } 35 ListObjectsRequest listObjectsRequest2 = new ListObjectsRequest(<BucketName>); 36 listObjectsRequest2.setMarker(nextMark); 37 listObjectsRequest2.setMaxKeys(20); 38 listObjectsRequest2.setDelimiter(BceConfig.BOS_DELIMITER); 39 ListObjectsResponse listing = client.listObjects(listObjectsRequest2); 40 for (BosObjectSummary objectSummary : listing.getContents()) { 41 System.out.println("ObjectKey: " + objectSummary.getKey()); 42 } 43 } catch (BceServiceException e) { 44 System.out.println("Error ErrorCode: " + e.getErrorCode()); 45 System.out.println("Error RequestId: " + e.getRequestId()); 46 System.out.println("Error StatusCode: " + e.getStatusCode()); 47 System.out.println("Error Message: " + e.getMessage()); 48 System.out.println("Error ErrorType: " + e.getErrorType()); 49 } catch (BceClientException e) { 50 System.out.println("Error Message: " + e.getMessage()); 51 } 52 } 53 }).start(); 54}}
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 under a simulated folder by setting the Prefix parameter:
1// Construct a ListObjectsRequest request
2ListObjectsRequest listObjectsRequest = new ListObjectsRequest(<BucketName>);
3 // Recursively list all files under the fun directory
4listObjectsRequest.setPrefix("fun/");
5ListObjectsResponse listing = client.listObjects(listObjectsRequest);
6 // Traverse all objects
7System.out.println("Objects:");
8for (BosObjectSummary objectSummary : listing.getContents()) {
9 System.out.println(objectSummary.getKey());
10}
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>);
3 // Specify "/" as the delimiter for simulated folder
4listObjectsRequest.setDelimiter("/");
5 // List all files and subfolders under the “fun” folder
6listObjectsRequest.setPrefix("fun/");
7ListObjectsResponse listing = client.listObjects(listObjectsRequest);
8 // Traverse all objects
9System.out.println("Objects:");
10for (BosObjectSummary objectSummary : listing.getContents()) {
11 System.out.println(objectSummary.getKey());
12}
13 // Traverse all CommonPrefix
14System.out.println("\nCommonPrefixs:");
15for (String commonPrefix : listing.getCommonPrefixes()) {
16 System.out.println(commonPrefix);
17}
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.
Get Object
Simple object get
Users can load the object into a stream by using the following code.
-
Basic workflow
- Instantiate the BOSClient class.
- By calling the BOSClient.getObject() method, the specified BosObject will be returned.
- Load the BosObject into a stream for further processing.
-
Example code
Java1//Obtain the object, with returned result BosObject object 2BosObject object = client.getObject(<BucketName>, <ObjectKey>); 3 // Retrieve ObjectMeta 4ObjectMetadata meta = object.getObjectMetadata(); 5 //Obtain the object's input stream 6InputStream objectContent = object.getObjectContent(); 7 // Process object 8... 9 // Close stream 10objectContent.close();Plain Text1> **Note:** 2> 3> - The BosObject includes various details about the object, such as the bucket it belongs to, the object’s name, metadata, and an input stream. Users can read the object's content into a file or memory by interacting with the input stream. 4> - ObjectMetadata contains information such as the ETag defined during object upload, HTTP headers, and custom metadata. 5> - 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
Java1import java.io.BufferedReader; 2import java.io.IOException; 3import java.io.InputStream; 4import java.io.InputStreamReader; 5import android.app.Activity; 6import android.os.Bundle; 7import com.baidubce.BceClientException; 8import com.baidubce.BceServiceException; 9import com.baidubce.auth.DefaultBceCredentials; 10import com.baidubce.demo.R; 11import com.baidubce.services.bos.BosClient; 12import com.baidubce.services.bos.BosClientConfiguration; 13import com.baidubce.services.bos.model.BosObject; 14import com.baidubce.services.bos.model.ObjectMetadata; 15public class ExampleActivity extends Activity { 16private String bucketName = <BucketName>; 17private String objectKey = <ObjectKey>; 18@Override 19protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 new Thread(new Runnable() { 23 @Override 24 public void run() { 25 try { 26 BosClientConfiguration config = new BosClientConfiguration(); 27 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 28 config.setEndpoint(<EndPoint>); 29 BosClient client = new BosClient(config); 30 //Obtain the object, with returned result BosObject object 31 BosObject object = client.getObject(<BucketName>, <ObjectKey>); 32 // Retrieve ObjectMeta 33 ObjectMetadata meta = object.getObjectMetadata(); 34 //Obtain the object's input stream 35 InputStream objectContent = object.getObjectContent(); 36 // Process object 37 FileOutputStream fos=new FileOutputStream(android.os.Environment.getExternalStorageDirectory()+"/1/file"); 38 byte[] buffer=new byte[<bufferSize>]; 39 int count=0; 40 while ((count=objectContent.read(buffer))>=0) { 41 fos.write(buffer,0,count); 42 } 43 System.out.println(meta.getETag()); 44 System.out.println(meta.getContentLength()); 45 // Close stream 46 objectContent.close(); 47 fos.close(); 48 } catch (BceServiceException e) { 49 System.out.println("Error ErrorCode: " + e.getErrorCode()); 50 System.out.println("Error RequestId: " + e.getRequestId()); 51 System.out.println("Error StatusCode: " + e.getStatusCode()); 52 System.out.println("Error Message: " + e.getMessage()); 53 System.out.println("Error ErrorType: " + e.getErrorType()); 54 } catch (BceClientException e) { 55 System.out.println("Error Message: " + e.getMessage()); 56 } catch (IOException e) { 57 // TODO Auto-generated catch block 58 e.printStackTrace(); 59 } 60 } 61 }).start(); 62}}
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.
- Execute the client.getObject() operation.
-
Example code
Java1// Create GetObjectRequest 2GetObjectRequest getObjectRequest = new GetObjectRequest(<BucketName>, <ObjectKey>); 3 // Obtain data within the 0~100 byte range 4getObjectRequest.setRange(0, 100); 5 //Obtain the object, with returned result BosObject object 6BosObject object = client.getObject(getObjectRequest);Plain Text1> **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.
Obtain download progress
Users can track download progress via a callback function.
The SDK offers a download progress callback API, which allows you to define necessary operations during the upload process, such as updating the API.
1public interface BceProgressCallback<T extends AbstractBceRequest> {
2 // request is the download request
3 // currentSize is the current download size (unit: byte)
4 // totalSize is the total size to be downloaded in this request (unit: byte)
5 void onProgress(T request, long currentSize, long totalSize);
6 }
-
Example code
Java1GetObjectRequest request = new GetObjectRequest() 2 .withBucketName(this.bucketName) 3 .withKey(objectKey) 4 .withRange(0, 5); 5request.setProgressCallback(new BosProgressCallback<GetObjectRequest>() { 6 @Override 7 public void onProgress(GetObjectRequest request, long currentSize, long totalSize) { 8 Log.e(currentSize + "", totalSize + ""); 9 } 10}); 11InputStream input = this.client.getObject(request).getObjectContent();Plain Text1> **Note:** The download progress interface differs from the upload progress interface. It is not possible to specify the fragment size of the increase in the download volume during each callback.
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
Java1// Create GetObjectRequest 2GetObjectRequest getObjectRequest = new GetObjectRequest(<BucketName>, <ObjectKey>); 3 // Download object to file 4ObjectMetadata objectMetadata = client.getObject(getObjectRequest, new File("/path/to/file","w+"));Plain Text1> **Note:** When using the above method to directly download an object to a specified path, the method returns an ObjectMetadata object.
Obtain the storageClass of an object
The storage classes of an object are categorized as STANDARD (standard storage), STANDARD_IA (infrequent access storage), COLD (cold storage), and ARCHIVE (archive storage).
Example code
1public void getObjectStorageClass(){
2 //...
3ObjectMetadata meta = client.getObjectMetadata(bucketName, key);
4String storageClass = meta.getStorageClass();
5}
Obtain only ObjectMetadata
The getObjectMetadata() method retrieves only the ObjectMetadata, without obtaining the object itself.
Example code
1ObjectMetadata objectMetadata = client.getObjectMetadata(<BucketName>, <ObjectKey>);
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
- Instantiate the BOSClient class.
- Call the BOSClient.generatePresignedUrl() method.
- The method returns the URL of the object.
-
Example code
Java1URL url = client.generatePresignedUrl(<BucketName>, <ObjectKey>, <ExpirationInSeconds>);Plain Text1> **Note:**` ExpirationInSeconds` is 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, the `ExpirationInSeconds` parameter can be set to -1. You cannot set it to any other negative value. -
Complete example
Java1import java.net.URL; 2import android.app.Activity; 3import android.content.Intent; 4import android.net.Uri; 5import android.os.Bundle; 6import com.baidubce.BceClientException; 7import com.baidubce.BceServiceException; 8import com.baidubce.auth.DefaultBceCredentials; 9import com.baidubce.demo.R; 10import com.baidubce.services.bos.BosClient; 11import com.baidubce.services.bos.BosClientConfiguration; 12public class ExampleActivity extends Activity { 13private String bucketName = <BucketName>; 14private String objectKey = <ObjectKey>; 15@Override 16protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 new Thread(new Runnable() { 20 @Override 21 public void run() { 22 try { 23 BosClientConfiguration config = new BosClientConfiguration(); 24 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 25 config.setEndpoint(<EndPoint>); 26 BosClient client = new BosClient(config); 27 //Obtain the object URL 28 URL url = client.generatePresignedUrl(<BucketName>, <ObjectKey>, 2000); 29 Uri uri = Uri.parse(url.toString()); 30 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 31 // Execute URL 32 startActivity(intent); 33 } catch (BceServiceException e) { 34 System.out.println("Error ErrorCode: " + e.getErrorCode()); 35 System.out.println("Error RequestId: " + e.getRequestId()); 36 System.out.println("Error StatusCode: " + e.getStatusCode()); 37 System.out.println("Error Message: " + e.getMessage()); 38 System.out.println("Error ErrorType: " + e.getErrorType()); 39 } catch (BceClientException e) { 40 System.out.println("Error Message: " + e.getMessage()); 41 } 42 } 43 }).start(); 44}}
Object permission control
Set access permission for an object.
Currently, BOS provides two ways to configure ACLs. The first is to use Canned ACL. During PutObjectAcl, object permissions are set through the headers "x-bce-acl" or "x-bce-grant-permission". Available permission options include private and public-read. Note that these two headers cannot be included in the same request. The second method involves uploading an ACL file.
For details, refer to Setting Object Permission Control..
- Set object access permission using the headers "x-bce-acl" or "x-bce-grant-permission"
- set object acl First method (Set via request headers)
1SetObjectAclRequest setObjectAclRequest = new SetObjectAclRequest("yourBucketName","objectKey",CannedAccessControlList.PublicRead);
2client.setObjectAcl(setObjectAclRequest);
- set object acl First method (Set xBceGrantRead via request headers)
1String xBceGrantRead = "id=\"user_id1\""+",id=\"user_id2\"";
2SetObjectAclRequest setObjectAclRequest = new SetObjectAclRequest();
3setObjectAclRequest.withBucketName("yourBucketName");
4setObjectAclRequest.withKey("objectKey");
5setObjectAclRequest.setxBceGrantRead(xBceGrantRead);
6client.setObjectAcl(setObjectAclRequest);
- set object acl First method (Set xBceGrantFullControl via request headers)
1String xBceGrantFullControl = "id=\"user_id1\""+",id=\"user_id2\"";
2SetObjectAclRequest setObjectAclRequest = new SetObjectAclRequest();
3setObjectAclRequest.withBucketName("yourBucketName");
4setObjectAclRequest.withKey("objectKey");
5setObjectAclRequest.setxBceGrantFullControl(xBceGrantFullControl);
6client.setObjectAcl(setObjectAclRequest);
- Set object access permission via setObjectAcl
- set object acl Second Method (JSON string)
1String jsonObjectAcl = "{\"accessControlList\":["+ "{\"grantee\":[{\"id\":\"*\"}], "+ "\"permission\":[\"FULL_CONTROL\"]"+"}]}";
2SetObjectAclRequest setObjectAclRequest = new SetObjectAclRequest("yourBucketName","objectKey",jsonObjectAcl);
3client.setObjectAcl(setObjectAclRequest);
- set object acl Second Method, by which users only need to specify the specified parameters
1List<Grant> grants = new ArrayList<Grant>();
2List<Grantee> grantees = new ArrayList<Grantee>();
3List<Permission> permissions = new ArrayList<Permission>();
4 // Grant permission to specific user
5grantees.add(new Grantee("user_id1"));
6grantees.add(new Grantee("user_id2"));
7grantees.add(new Grantee("user_id3"));
8 // Set permissions
9permissions.add(Permission.READ);
10grants.add(new Grant().withGrantee(grantees).withPermission(permissions));
11SetObjectAclRequest setObjectAclRequest = new SetObjectAclRequest("yourBucketName","objectKey", grants);
12client.setObjectAcl(setObjectAclRequest);
View object permissions
The following code can be used to check the object permissions:
1GetObjectAclRequest getObjectRequest = new GetObjectAclRequest();
2getObjectRequest.withBucketName("yourBucketName");
3getObjectRequest.withKey("objectKey");
4GetObjectAclResponse response = client.getObjectAcl(getObjectRequest);
The parameters available for calling in the resolution class returned by the getObjectAcl method are as follows:
| Parameters | Description |
|---|---|
| accessControlList | Identify the permission list of the object |
| grantee | Identify the grantee |
| -id | Authorized person ID |
| permission | Identify the grantee permissions |
Delete object permissions
The following code can be used to delete the object permissions:
1DeleteObjectAclRequest deleteObjectAclRequest = new DeleteObjectAclRequest("yourBucketName","objectKey");
2client.deleteObjectAcl(deleteObjectAclRequest);
Delete Object
-
Basic workflow
- Instantiate the BOSClient class.
- Invoke the BOSClient.deleteObject() method.
- If the operation fails, an exception is thrown; if successful, no return value is provided.
-
Example code
Java1// Delete Object 2 client.deleteObject(<BucketName>, <ObjectKey>); //Specify the name of the bucket where the object to be deleted is located and the name of the object -
Complete example
Java1import android.app.Activity; 2import android.os.Bundle; 3import com.baidubce.BceClientException; 4import com.baidubce.BceServiceException; 5import com.baidubce.auth.DefaultBceCredentials; 6import com.baidubce.demo.R; 7import com.baidubce.services.bos.BosClient; 8import com.baidubce.services.bos.BosClientConfiguration; 9public class ExampleActivity extends Activity { 10private String bucketName = <BucketName>; 11private String objectKey = <ObjectKey>; 12@Override 13protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 new Thread(new Runnable() { 17 @Override 18 public void run() { 19 try { 20 BosClientConfiguration config = new BosClientConfiguration(); 21 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 22 config.setEndpoint(<EndPoint>); 23 BosClient client = new BosClient(config); 24 // Delete Object 25 client.deleteObject(<BucketName>, <ObjectKey>); //Specify the name of the bucket where the object to be deleted is located and the name of the object 26 } catch (BceServiceException e) { 27 System.out.println("Error ErrorCode: " + e.getErrorCode()); 28 System.out.println("Error RequestId: " + e.getRequestId()); 29 System.out.println("Error StatusCode: " + e.getStatusCode()); 30 System.out.println("Error Message: " + e.getMessage()); 31 System.out.println("Error ErrorType: " + e.getErrorType()); 32 } catch (BceClientException e) { 33 System.out.println("Error Message: " + e.getMessage()); 34 } 35 } 36 }).start(); 37}}
Copy Object
Simply Copy Object
-
Basic workflow
- Instantiate the BOSClient class.
- Call the BOSClient.copyObject() method.
- The method returns a CopyObjectResponse instance. The eTag and last modification time can be obtained through getETag() and getLastModified().
-
Example code
Java1// Copy Object 2 CopyObjectResponse copyObjectResponse = client.copyObject(<SrcBucketName>, <SrcKey>, <DestBucketName>, <DestKey>); //SrcBucketName, SrcKey are the source addresses,DestBucketName, DestKey are the destination addresses to which the copy is made 3 // Print results 4System.out.println("ETag: " + copyObjectResponse.getETag() + " LastModified: " + copyObjectResponse.getLastModified()); -
Complete example
Java1import android.app.Activity; 2import android.os.Bundle; 3import com.baidubce.BceClientException; 4import com.baidubce.BceServiceException; 5import com.baidubce.auth.DefaultBceCredentials; 6import com.baidubce.demo.R; 7import com.baidubce.services.bos.BosClient; 8import com.baidubce.services.bos.BosClientConfiguration; 9import com.baidubce.services.bos.model.CopyObjectResponse; 10public class ExampleActivity extends Activity { 11private String srcBucketName = <SrcBucketName>; 12private String srcKey = <SrcKey>; 13private String destBucketName = <DestBucketName>; 14private String destKey = <DestKey>; 15@Override 16protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 new Thread(new Runnable() { 20 @Override 21 public void run() { 22 try { 23 BosClientConfiguration config = new BosClientConfiguration(); 24 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 25 config.setEndpoint(<EndPoint>); 26 BosClient client = new BosClient(config); 27 // Copy Object 28 CopyObjectResponse copyObjectResponse = client.copyObject(<SrcBucketName>, <SrcKey>, <DestBucketName>, <DestKey>); 29 // Print results 30 System.out.println("ETag: " + copyObjectResponse.getETag() + " LastModified: " + copyObjectResponse.getLastModified()); 31 } catch (BceServiceException e) { 32 System.out.println("Error ErrorCode: " + e.getErrorCode()); 33 System.out.println("Error RequestId: " + e.getRequestId()); 34 System.out.println("Error StatusCode: " + e.getStatusCode()); 35 System.out.println("Error Message: " + e.getMessage()); 36 System.out.println("Error ErrorType: " + e.getErrorType()); 37 } catch (BceClientException e) { 38 System.out.println("Error Message: " + e.getMessage()); 39 } 40 } 41 }).start(); 42}}Plain Text1> **Note:** The copyObject method returns a `CopyObjectResponse` object containing the new object's ETag and modification time.
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.
- The method returns a CopyObjectResponse instance. The eTag and last modification time can be obtained through getETag() and getLastModified().
- Create an instance of the CopyObjectRequest class, and pass in the parameters
-
Example code
Java1// Create CopyObjectRequest object 2CopyObjectRequest copyObjectRequest = new CopyObjectRequest(<SrcBucketName>, <SrcKey>, <DestBucketName>, <DestKey>); 3 // Set new Metadata 4Map<String, String> userMetadata = new HashMap<String, String>(); 5userMetadata.put(<UserMetaKey>,<UserMetaValue>); 6meta.setUserMetadata(userMetadata); 7copyObjectRequest.setNewObjectMetadata(meta); 8 // Copy Object 9CopyObjectResponse copyObjectResponse = client.copyObject(copyObjectRequest); 10System.out.println("ETag: " + copyObjectResponse.getETag() + " LastModified: " + copyObjectResponse.getLastModified());Plain Text1> **Note:**` CopyObjectRequest` allows users to modify the ObjectMeta of the target object and also provides the setting of the `MatchingETagConstraints` parameter.
Multipart upload of objects
Multipart Upload Scenarios
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.
Multipart Upload Process
Suppose there is a file with the local path /path/to/file.zip. Since the file is large, multipart upload is used to transmit it to BOS.
-
Basic workflow
- Start a multipart upload.
- Upload individual parts.
- Finalize the multipart upload.
Initialize Multipart Upload
Use initiateMultipartUpload method to initialize a multipart upload event:
-
Example code
Java1// Initiate Multipart Upload 2InitiateMultipartUploadRequest initiateMultipartUploadRequest = 3 new InitiateMultipartUploadRequest(<BucketName>, <ObjectKey>); 4InitiateMultipartUploadResponse initiateMultipartUploadResponse = 5 client.initiateMultipartUpload(initiateMultipartUploadRequest); 6 // Print UploadId 7System.out.println("UploadId: " + initiateMultipartUploadResponse.getUploadId());Plain Text1> **Note:** The return result of `initiateMultipartUpload` contains `UploadId`, 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
Java1// Set each part to 5MB 2final long partSize = 1024 * 1024 * 5L; 3File partFile = new File("/path/to/file.zip"); 4 // Calculate the count of parts 5int partCount = (int) (partFile.length() / partSize); 6if (partFile.length() % partSize != 0) { 7 partCount++; 8} 9 // Create a list to save the ETag and PartNumber of each uploaded part 10List<PartETag> partETags = new ArrayList<PartETag>(); 11 // Get file stream 12FileInputStream fis = new FileInputStream(partFile); 13for (int i = 0; i < partCount; i++) { 14 // Calculate the size of each part 15 long skipBytes = partSize * i; 16 long size = partSize < partFile.length() - skipBytes ? 17 partSize : partFile.length() - skipBytes; 18 byte[] buf = new byte[(int)size]; 19 int offset = 0; 20 while (true) { 21 int byteRead = fis.read(buf, offset, (int)size); 22 offset += byteRead; 23 if (byteRead < 0 || offset >= size) { 24 break; 25 } 26 } 27 ByteArrayInputStream bufStream = new ByteArrayInputStream(buf); 28 // Create UploadPartRequest to upload parts 29 UploadPartRequest uploadPartRequest = new UploadPartRequest(); 30 uploadPartRequest.setBucketName(bucketName); 31 uploadPartRequest.setKey(objectkey); 32 uploadPartRequest.setUploadId(initiateMultipartUploadResponse.getUploadId()); 33 uploadPartRequest.setInputStream(bufStream); 34 uploadPartRequest.setPartSize(size); 35 uploadPartRequest.setPartNumber(i + 1); 36 // Upload progress callback 37 uploadPartRequest.setProgressCallback(new BosProgressCallback<UploadPartRequest>() { 38 @Override 39 public void onProgress(UploadPartRequest request, long currentSize, long totalSize) { 40 Log.e(currentSize + "", totalSize + ""); 41 } 42 }); 43 UploadPartResponse uploadPartResponse = client.uploadPart(uploadPartRequest); 44 // Save the returned PartETag to the List. 45 partETags.add(uploadPartResponse.getPartETag()); 46 System.out.println(uploadPartResponse.getPartETag()); 47 } 48 // Close the file 49 fis.close();Plain Text1> **Note:** The core of the above code is to call the `UploadPart` method to upload each part concurrently, but the following points should be noted: 2> 3> - The UploadPart method requires the size of each part to either be an integer multiple of 1MB or greater than 5MB. However, the Upload Part interface does not validate the part size immediately; this check happens only during the finalization of the multipart upload. 4> - To ensure no errors during network transmission, it is recommended to use the Content-MD5 value returned by BOS for each part after `UploadPart` to verify the correctness of the uploaded part data. When all part data is combined into one Object, it no longer contains the MD5 value. 5> - 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. 6> - For each uploaded part, the stream must be positioned at the beginning of the respective part. 7> - After each Part upload, the return result of BOS will include a `PartETag` object, 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, these `PartETag` objects will be saved in a List. 8> - For details on using the progress callback API, refer to the chapter titled "Obtain Upload Progress."
Complete multipart upload
-
Example code
Java1CompleteMultipartUploadRequest completeMultipartUploadRequest = 2 new CompleteMultipartUploadRequest(<BucketName>, <ObjectKey>, 3 initiateMultipartUploadResponse.getUploadId(), partETags); 4 // Complete multipart upload 5CompleteMultipartUploadResponse completeMultipartUploadResponse = 6 client.completeMultipartUpload(completeMultipartUploadRequest); 7 // Print Object's ETag 8System.out.println(completeMultipartUploadResponse.getETag());Plain Text1> **Note:** The `partETags` in 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. -
Complete example
Java1import java.io.File; 2import java.io.FileInputStream; 3import java.io.IOException; 4import java.util.ArrayList; 5import java.util.List; 6import org.json.JSONException; 7import android.app.Activity; 8import android.os.Bundle; 9import com.baidubce.BceClientException; 10import com.baidubce.BceServiceException; 11import com.baidubce.auth.DefaultBceCredentials; 12import com.baidubce.demo.R; 13import com.baidubce.services.bos.BosClient; 14import com.baidubce.services.bos.BosClientConfiguration; 15import com.baidubce.services.bos.model.CompleteMultipartUploadRequest; 16import com.baidubce.services.bos.model.CompleteMultipartUploadResponse; 17import com.baidubce.services.bos.model.InitiateMultipartUploadRequest; 18import com.baidubce.services.bos.model.InitiateMultipartUploadResponse; 19import com.baidubce.services.bos.model.PartETag; 20import com.baidubce.services.bos.model.UploadPartRequest; 21import com.baidubce.services.bos.model.UploadPartResponse; 22public class ExampleActivity extends Activity { 23private String bucketName = <BucketName>; 24private String objectKey = <ObjectKey>; 25 26@Override 27protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main); 30 new Thread(new Runnable() { 31 @Override 32 public void run() { 33 try { 34 BosClientConfiguration config = new BosClientConfiguration(); 35 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 36 config.setEndpoint(<EndPoint>); 37 BosClient client = new BosClient(config); 38 // Initiate Multipart Upload 39 InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(<BucketName>, <ObjectKey>); 40 InitiateMultipartUploadResponse initiateMultipartUploadResponse = 41 client.initiateMultipartUpload(initiateMultipartUploadRequest); 42 // Print UploadId 43 System.out.println("UploadId: " + initiateMultipartUploadResponse.getUploadId()); 44 // Set each part to 5MB 45 final long partSize = 1024 * 1024 * 5L; 46 File partFile = new File("/path/to/file.zip"); 47 // Calculate the count of parts 48 int partCount = (int) (partFile.length() / partSize); 49 if (partFile.length() % partSize != 0) { 50 partCount++; 51 } 52 // Create a list to save the ETag and PartNumber of each uploaded part 53 List<PartETag> partETags = new ArrayList<PartETag>(); 54 // Get file stream 55 FileInputStream fis = new FileInputStream(partFile); 56 for (int i = 0; i < partCount; i++) { 57 // Calculate the size of each part 58 long skipBytes = partSize * i; 59 long size = partSize < partFile.length() - skipBytes ? 60 partSize : partFile.length() - skipBytes; 61 byte[] buf = new byte[(int)size]; 62 int offset = 0; 63 while (true) { 64 int byteRead = fis.read(buf, offset, (int)size); 65 offset += byteRead; 66 if (byteRead < 0 || offset >= size) { 67 break; 68 } 69 } 70 ByteArrayInputStream bufStream = new ByteArrayInputStream(buf); 71 // Create UploadPartRequest to upload parts 72 UploadPartRequest uploadPartRequest = new UploadPartRequest(); 73 uploadPartRequest.setBucketName(<BucketName>); 74 uploadPartRequest.setKey(<ObjectKey>); 75 uploadPartRequest.setUploadId(initiateMultipartUploadResponse.getUploadId()); 76 uploadPartRequest.setInputStream(fis); 77 uploadPartRequest.setPartSize(size); 78 uploadPartRequest.setPartNumber(i + 1); 79 UploadPartResponse uploadPartResponse = client.uploadPart(uploadPartRequest); 80 // Save the returned PartETag to the List. 81 partETags.add(uploadPartResponse.getPartETag()); 82 System.out.println(uploadPartResponse.getPartETag()); 83 } 84 // Close the file 85 fis.close(); 86 CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(<BucketName>, <ObjectKey>, initiateMultipartUploadResponse.getUploadId(), partETags); 87 // Complete multipart upload 88 CompleteMultipartUploadResponse completeMultipartUploadResponse = 89 client.completeMultipartUpload(completeMultipartUploadRequest); 90 // Print Object's ETag 91 System.out.println(completeMultipartUploadResponse.getETag()); 92 } catch (BceServiceException e) { 93 System.out.println("Error ErrorCode: " + e.getErrorCode()); 94 System.out.println("Error RequestId: " + e.getRequestId()); 95 System.out.println("Error StatusCode: " + e.getStatusCode()); 96 System.out.println("Error Message: " + e.getMessage()); 97 System.out.println("Error ErrorType: " + e.getErrorType()); 98 } catch (BceClientException e) { 99 System.out.println("Error Message: " + e.getMessage()); 100 } catch (IOException e) { 101 // TODO Auto-generated catch block 102 e.printStackTrace(); 103 } catch (JSONException e) { 104 // TODO Auto-generated catch block 105 e.printStackTrace(); 106 } 107 } 108 }).start(); 109}}
Cancel multipart upload
Users can cancel multipart uploads by using the abortMultipartUpload method.
-
Example code
Java1AbortMultipartUploadRequest abortMultipartUploadRequest = 2 new AbortMultipartUploadRequest(<BucketName>, <ObjectKey>, <UploadId>); 3 // Cancel multipart upload 4client.abortMultipartUpload(abortMultipartUploadRequest);
Retrieve unfinished multipart uploads
Users can obtain the unfinished multipart upload events in the bucket by the listMultipartUploads method.
-
Basic workflow
- Create an instance of the listMultipartUploadsRequest class, and pass in the parameter
<BucketName>. - Create a BOSClient instance and invoke the BOSClient.listMultipartUploads() method.
- The listMultipartUploads() method provides details about all uploaded parts.
- Create an instance of the listMultipartUploadsRequest class, and pass in the parameter
-
Example code
Java1ListMultipartUploadsRequest listMultipartUploadsRequest = 2 new ListMultipartUploadsRequest(<BucketName>); 3 // Retrieve all upload events within the bucket 4ListMultipartUploadsResponse list = client.listMultipartUploads(listMultipartUploadsRequest); 5 6 // Traverse all upload events 7for (MultipartUploadSummary multipartUpload : list.getMultipartUploads()) { 8 System.out.println("Key: " + multipartUpload.getKey() + " UploadId: " + multipartUpload.getUploadId()); 9}Plain Text1> **Note:** 2> 3> - By default, if the number of multipart upload events in a bucket surpasses 1,000, only 1,000 records will be returned. In such cases, the IsTruncated value in the response will be True, and the NextKeyMarker will indicate the starting point for the next query. 4> - To fetch more multipart upload events, use the KeyMarker parameter to retrieve data in batches. -
Complete example
Java1import android.app.Activity; 2import android.os.Bundle; 3import com.baidubce.BceClientException; 4import com.baidubce.BceServiceException; 5import com.baidubce.auth.DefaultBceCredentials; 6import com.baidubce.demo.R; 7import com.baidubce.services.bos.BosClient; 8import com.baidubce.services.bos.BosClientConfiguration; 9import com.baidubce.services.bos.model.ListMultipartUploadsRequest; 10import com.baidubce.services.bos.model.ListMultipartUploadsResponse; 11import com.baidubce.services.bos.model.MultipartUploadSummary; 12public class ExampleActivity extends Activity { 13private String bucketName = <BucketName>; 14@Override 15protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 new Thread(new Runnable() { 19 @Override 20 public void run() { 21 try { 22 BosClientConfiguration config = new BosClientConfiguration(); 23 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 24 config.setEndpoint(<EndPoint>); 25 BosClient client = new BosClient(config); 26 ListMultipartUploadsRequest listMultipartUploadsRequest = 27 new ListMultipartUploadsRequest(<BucketName>); 28 // Retrieve all upload events within the bucket 29 ListMultipartUploadsResponse listing = client.listMultipartUploads(listMultipartUploadsRequest); 30 // Traverse all upload events 31 for (MultipartUploadSummary multipartUpload : listing.getMultipartUploads()) { 32 System.out.println("Key: " + multipartUpload.getKey() + " UploadId: " + multipartUpload.getUploadId()); 33 } 34 } catch (BceServiceException e) { 35 System.out.println("Error ErrorCode: " + e.getErrorCode()); 36 System.out.println("Error RequestId: " + e.getRequestId()); 37 System.out.println("Error StatusCode: " + e.getStatusCode()); 38 System.out.println("Error ErrorType: " + e.getErrorType()); 39 System.out.println("Error Message: " + e.getMessage()); 40 } catch (BceClientException e) { 41 System.out.println("Error Message: " + e.getMessage()); 42 } 43 } 44 }).start(); 45}}
Get all uploaded part information
Users can obtain all uploaded parts in an upload event by the listParts method.
-
Basic workflow
- Create an instance of the ListPartsRequest class, and pass in the parameters
<BucketName>,<ObjectKey>and<UploadId> - Create a BOSClient instance and invoke the BOSClient.listParts() method.
- The listParts() method provides details about all uploaded parts.
- Create an instance of the ListPartsRequest class, and pass in the parameters
-
Example code
Java1ListPartsRequest listPartsRequest = new ListPartsRequest(<BucketName>, <ObjectKey>, <UploadId>); 2 3 // Retrieve all uploaded part information 4ListPartsResponse partListing = client.listParts(listPartsRequest); 5 6 // Traverse all parts 7for (PartSummary part : partListing.getParts()) { 8 System.out.println("PartNumber: " + part.getPartNumber() + " ETag: " + part.getETag()); 9}Plain Text1> **Note:** 2> 3> - By default, if the number of multipart upload events in a bucket surpasses 1,000, only 1,000 records will be returned. In such cases, the IsTruncated value in the response will be True, and the NextPartNumberMarker will indicate the starting point for the next query. 4> - To retrieve more information about uploaded parts, use the PartNumberMarker parameter to fetch data in batches. -
Complete example
Java1import android.app.Activity; 2import android.os.Bundle; 3import com.baidubce.BceClientException; 4import com.baidubce.BceServiceException; 5import com.baidubce.auth.DefaultBceCredentials; 6import com.baidubce.demo.R; 7import com.baidubce.services.bos.BosClient; 8import com.baidubce.services.bos.BosClientConfiguration; 9import com.baidubce.services.bos.model.ListPartsRequest; 10import com.baidubce.services.bos.model.ListPartsResponse; 11import com.baidubce.services.bos.model.PartSummary; 12public class ExampleActivity extends Activity { 13private String bucketName = <BucketName>; 14private String objectKey = <ObjectKey>; 15private String uploadId = <UploadId>; 16@Override 17protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 new Thread(new Runnable() { 21 @Override 22 public void run() { 23 try { 24 BosClientConfiguration config = new BosClientConfiguration(); 25 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 26 config.setEndpoint(<EndPoint>); 27 BosClient client = new BosClient(config); 28 ListPartsRequest listPartsRequest = new ListPartsRequest(<BucketName>, <ObjectKey>, <UploadId>); 29 listPartsRequest.setMaxParts(100); 30 listPartsRequest.setPartNumberMarker(50); 31 // Retrieve all uploaded part information 32 ListPartsResponse partListing = client.listParts(listPartsRequest); 33 // Traverse all parts 34 for (PartSummary part : partListing.getParts()) { 35 System.out.println("PartNumber: " + part.getPartNumber() + " ETag: " + part.getETag()); 36 } 37 } catch (BceServiceException e) { 38 System.out.println("Error ErrorCode: " + e.getErrorCode()); 39 System.out.println("Error RequestId: " + e.getRequestId()); 40 System.out.println("Error StatusCode: " + e.getStatusCode()); 41 System.out.println("Error ErrorType: " + e.getErrorType()); 42 System.out.println("Error Message: " + e.getMessage()); 43 } catch (BceClientException e) { 44 System.out.println("Error Message: " + e.getMessage()); 45 } 46 } 47 }).start(); 48}}
Encapsulate multipart upload
In the Android SDK, BOS provides the putSuperObjectFromFile interface, which consolidates the three main methods for multipart uploads: initiateMultipartUpload, UploadPart, and completeMultipartUpload. Using this interface, users can complete a multipart upload with built-in support for progress synchronization callbacks.
-
Simple example:
Java1File file = new File("/path/to/file.zip"); 2PutSuperObjectRequest request = new PutSuperObjectRequest(bucketName, objectKey, file); 3bosClient.putSuperObjectFromFile(request); -
Example: Set the part size to 2MB, the thread count to 4, and synchronize progress callbacks every 1,024 bytes
Java1BosClientConfiguration config=new BosClientConfiguration(); 2config.setUploadSegmentPart(1024); 3File file = new File("/path/to/file.zip"); 4PutSuperObjectRequest request = new PutSuperObjectRequest(bucketName, objectKey, 5 file, 1024 * 1024 * 2L, 4); 6request.setProgressCallback(new BosProgressCallback<PutSuperObjectRequest>() { 7 @Override 8 public void onProgress(PutSuperObjectRequest request, long currentSize, long totalSize) { 9 Log.e(currentSize + "", totalSize + ""); 10 } 11}); 12bosClient.putSuperObjectFromFile(request);
Note:
- By default, the part size is 5MB, the thread count is 5, and the progress callback period is every 2,048 bytes.
- If a large file takes a long time to upload and the user wants to end the multipart upload, they can call the cancel() method in PutSuperObjectRequest.
