補充說明
如果您需要使用阿里雲SDK批量解凍檔案,需要對範例程式碼進行部分修改,由於範例程式碼只展示單檔案解凍操作的相關流程,在需要批量解凍時,首先將過濾出需要解凍的檔案的路徑資訊放入一個清單中,用清單替換樣本中的objectName參數,之後遞迴進行解凍檔案操作即可,具體可參考如下樣本:
說明 此樣本僅展示Java語言批量解凍歸檔檔案相關操作,如需查看文檔解凍是否完成,請參見如何查看OSS檔案解凍是否完成?
如果您需要解凍冷歸檔、深度冷歸檔類型的檔案,請跳轉至SDK簡介,根據需要使用的程式設計語言,查詢對應語言的解凍檔案文檔內容。
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華北2(北京)為例,其它Region請按實際情況填寫。
String endpoint = "oss-cn-beijing.aliyuncs.com";
// 填寫Endpoint對應的Region資訊,例如cn-hangzhou。
String region = "cn-hangzhou";
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫Bucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 建立清單,並添加不包含Bucket名稱在內的歸檔類型Object的完整路徑。
List<String> filePaths = new ArrayList<>();
filePaths.add("filePath1");
filePaths.add("filePath2");
// 建立OSSClient執行個體。
// 當OSSClient執行個體不再使用時,調用shutdown方法以釋放資源。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
// 顯式聲明使用 V4 簽名演算法
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
for (String filePath : filePaths) {
ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, filePath);
// 校正Object是否為歸檔類型Object。
StorageClass storageClass = objectMetadata.getObjectStorageClass();
if (storageClass == StorageClass.Archive) {
// 解凍Object。
ossClient.restoreObject(bucketName, filePath);
}
System.out.println(filePath+" start thawing.");
}
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}