How Do I Perform Batch Operations Using the SDK?
Currently, the SDK does not provide such APIs. You need to encapsulate the service codes for uploading, downloading, copying, or restoring objects in a batch by yourself. The procedure is as follows:
- Call listObjects to list all objects to be downloaded, copied, or restored. For details about the sample code, see Listing Objects. For batch upload, you need to traverse the local files to be uploaded.
- Call the API for uploading, downloading, copying, or restoring a single object for the listed objects.
You can use multiple threads to concurrently upload, download, copy, or restore objects to improve efficiency.
Sample Code 1: Uploading Objects in Batches
The following code shows how to upload objects in batches.
// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
// In this example, the AK and SK are stored in environment variables. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// Obtain an AK/SK pair on the management console.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an ObsClient instance.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
try {
String bucketName = "examplebucket";
// Define the prefix of objects in a bucket.
String objectPre = "object/";
// Folder to be uploaded
String localDirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/localDirPath";
// Traverse the local folder and upload files one by one.
File localDir = new File(localDirPath);
if (localDir != null && localDir.isDirectory()) {
File[] files = localDir.listFiles();
if (files != null) {
int succeedCount = 0;
int failCount = 0;
for (File file : files) {
if (file.isFile()) {
try {
String objectKey = objectPre + file.getName();
PutObjectResult result = obsClient.putObject(bucketName, objectKey, file);
Log.i("BatchUpload", "Upload succeeded: " + objectKey);
succeedCount++;
} catch (ObsException e) {
Log.e("BatchUpload", "Upload failed: " + file.getName()
+ ", Error Code: " + e.getErrorCode()
+ ", Error Message: " + e.getErrorMessage());
failCount++;
}
}
}
Log.i("BatchUpload", "Batch upload completed. Succeed: " + succeedCount + ", Failed: " + failCount);
}
}
} catch (ObsException e) {
Log.e("BatchUpload", "Response Code: " + e.getResponseCode());
Log.e("BatchUpload", "Error Message: " + e.getErrorMessage());
Log.e("BatchUpload", "Error Code: " + e.getErrorCode());
Log.e("BatchUpload", "Request ID: " + e.getErrorRequestId());
Log.e("BatchUpload", "Host ID: " + e.getErrorHostId());
} catch (Exception e) {
Log.e("BatchUpload", "batch upload failed: " + e.getMessage());
}
- Network operations must be performed in child threads. Do not call the preceding code in the main thread (UI thread). Otherwise, the NetworkOnMainThreadException exception will be thrown.
- If there are a large number of local files or the files are large, you are advised to use multiple threads to upload files concurrently to improve efficiency.
- To upload files in subfolders, you need to recursively traverse the directory structure.
- If the size of the file to be uploaded is large, you can use the resumable upload API (uploadFile) to replace the object upload API (putObject).
Sample Code 2: Downloading Objects in Batches
The following code shows how to download objects in batches.
// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
// In this example, the AK and SK are stored in environment variables. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// Obtain an AK/SK pair on the management console.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an ObsClient instance.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);
try {
String bucketName = "examplebucket";
String objectPre = "object/";
// Local download directory
String localDownloadDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/download";
// 1. List all objects with a specified prefix in the bucket.
ListObjectsRequest listRequest = new ListObjectsRequest(bucketName);
listRequest.setPrefix(objectPre);
ObjectListing result = obsClient.listObjects(listRequest);
// 2. Download objects one by one.
for (ObsObject obsObject : result.getObjects()) {
String objectKey = obsObject.getObjectKey();
// Skip folders (objects whose names end with a slash (/)).
if (objectKey.endsWith("/")) {
continue;
}
// Generate local file paths.
String localFilePath = localDownloadDir + "/" + objectKey.substring(objectPre.length());
DownloadFileRequest downloadRequest = new DownloadFileRequest(bucketName, objectKey);
downloadRequest.setDownloadFile(localFilePath);
obsClient.downloadFile(downloadRequest);
Log.i("BatchDownload", "Downloaded: " + objectKey);
}
Log.i("BatchDownload", "Batch download completed");
} catch (ObsException e) {
Log.e("BatchDownload", "Response Code: " + e.getResponseCode());
Log.e("BatchDownload", "Error Message: " + e.getErrorMessage());
Log.e("BatchDownload", "Error Code: " + e.getErrorCode());
Log.e("BatchDownload", "Request ID: " + e.getErrorRequestId());
Log.e("BatchDownload", "Host ID: " + e.getErrorHostId());
} catch (Exception e) {
Log.e("BatchDownload", "Batch download failed: " + e.getMessage());
}
- Network operations must be performed in child threads. Do not call the preceding code in the main thread (UI thread). Otherwise, the NetworkOnMainThreadException exception will be thrown.
- You can use multiple threads to concurrently download objects to improve efficiency.
- If there are a large number of objects in the bucket, you need to list all objects in pagination mode. For details, see Listing Objects.
Sample Code 3: Copying Objects in Batches
The following code shows how to copy objects in batches.
// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
// In this example, the AK and SK are stored in environment variables. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// Obtain an AK/SK pair on the management console.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an ObsClient instance.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
try {
String sourceBucketName = "examplebucket";
String destBucketName = "destbucket";
String objectPre = "object/";
// 1. List all objects with a specified prefix in the source bucket.
ListObjectsRequest listRequest = new ListObjectsRequest(sourceBucketName);
listRequest.setPrefix(objectPre);
ObjectListing result = obsClient.listObjects(listRequest);
// 2. Copy objects one by one.
for (ObsObject obsObject : result.getObjects()) {
String objectKey = obsObject.getObjectKey();
// Skip folders.
if (objectKey.endsWith("/")) {
continue;
}
CopyObjectRequest copyRequest = new CopyObjectRequest(sourceBucketName, objectKey, destBucketName, objectKey);
obsClient.copyObject(copyRequest);
Log.i("BatchCopy", "Copied: " + objectKey);
}
Log.i("BatchCopy", "Batch copy completed");
} catch (ObsException e) {
Log.e("BatchCopy", "Response Code: " + e.getResponseCode());
Log.e("BatchCopy", "Error Message: " + e.getErrorMessage());
Log.e("BatchCopy", "Error Code: " + e.getErrorCode());
Log.e("BatchCopy", "Request ID: " + e.getErrorRequestId());
Log.e("BatchCopy", "Host ID: " + e.getErrorHostId());
} catch (Exception e) {
Log.e("BatchCopy", "Batch copy failed: " + e.getMessage());
}
- Network operations must be performed in child threads. Do not call the preceding code in the main thread (UI thread). Otherwise, the NetworkOnMainThreadException exception will be thrown.
- If there are a large number of objects in the bucket, you need to list all objects in pagination mode. For details, see Listing Objects.
Sample Code 4: Restoring Objects in Batches
The following code shows how to restore objects with a specified prefix in batches.
// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
// In this example, the AK and SK are stored in environment variables. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// Obtain an AK/SK pair on the management console.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an ObsClient instance.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
try {
String bucketName = "examplebucket";
String objectPre = "object/";
// 1. List all objects with a specified prefix in the bucket.
ListObjectsRequest listRequest = new ListObjectsRequest(bucketName);
listRequest.setPrefix(objectPre);
ObjectListing listing = obsClient.listObjects(listRequest);
// 2. Restore Archive or Deep Archive objects one by one.
int succeedCount = 0;
int failCount = 0;
for (ObsObject obsObject : listing.getObjects()) {
String objectKey = obsObject.getObjectKey();
// Skip folders (objects whose names end with a slash (/)).
if (objectKey.endsWith("/")) {
continue;
}
try {
RestoreObjectRequest request = new RestoreObjectRequest();
request.setBucketName(bucketName);
request.setObjectKey(objectKey);
// Set the retention period of the restored objects.
request.setRestoreDays(7);
// Set the restoration speed to the standard mode.
request.setTier(RestoreTierEnum.STANDARD);
obsClient.restoreObject(request);
Log.i("BatchRestore", "Restore succeeded: " + objectKey);
succeedCount++;
} catch (ObsException e) {
Log.e("BatchRestore", "Restore failed: " + objectKey
+ ", Error Code: " + e.getErrorCode()
+ ", Error Message: " + e.getErrorMessage());
failCount++;
}
}
Log.i("BatchRestore", "Batch restore completed. Succeed: " + succeedCount + ", Failed: " + failCount);
} catch (ObsException e) {
Log.e("BatchRestore", "Response Code: " + e.getResponseCode());
Log.e("BatchRestore", "Error Message: " + e.getErrorMessage());
Log.e("BatchRestore", "Error Code: " + e.getErrorCode());
Log.e("BatchRestore", "Request ID: " + e.getErrorRequestId());
Log.e("BatchRestore", "Host ID: " + e.getErrorHostId());
} catch (Exception e) {
Log.e("BatchRestore", "batch restore failed: " + e.getMessage());
} The following code shows how to restore objects in batches by object name list.
// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
// In this example, the AK and SK are stored in environment variables. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// Obtain an AK/SK pair on the management console.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an ObsClient instance.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
try {
String bucketName = "examplebucket";
// List of objects to be restored
String[] objectKeys = {"objectname1", "objectname2", "objectname3"};
int succeedCount = 0;
int failCount = 0;
for (String objectKey : objectKeys) {
try {
RestoreObjectRequest request = new RestoreObjectRequest();
request.setBucketName(bucketName);
request.setObjectKey(objectKey);
// Set the retention period of the restored objects.
request.setRestoreDays(1);
// Set the restoration speed to the expedited mode.
request.setTier(RestoreTierEnum.EXPEDITED);
obsClient.restoreObject(request);
Log.i("BatchRestore", "Restore succeeded: " + objectKey);
succeedCount++;
} catch (ObsException e) {
Log.e("BatchRestore", "Restore failed: " + objectKey
+ ", Error Code: " + e.getErrorCode()
+ ", Error Message: " + e.getErrorMessage());
failCount++;
}
}
Log.i("BatchRestore", "Batch restore completed. Succeed: " + succeedCount + ", Failed: " + failCount);
} catch (Exception e) {
Log.e("BatchRestore", "batch restore failed: " + e.getMessage());
}
- Network operations must be performed in child threads. Do not call the preceding code in the main thread (UI thread). Otherwise, the NetworkOnMainThreadException exception will be thrown.
- If there are a large number of objects in the bucket, you need to list all objects in pagination mode. For details, see Listing Objects.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot