补充说明
如果您需要使用阿里云SDK批量解冻文件,需要对示例代码进行部分修改,由于示例代码只展示单文件解冻操作的相关流程,在需要批量解冻时,首先将过滤出需要解冻的文件的路径信息放入一个清单中,用清单替换示例中的objectName参数,之后递归进行解冻文件操作即可,具体可参考如下示例:
说明 此示例仅展示Java语言批量解冻归档文件相关操作,如需查看文档解冻是否完成,请参见如何查看OSS文件解冻是否完成?
如果您需要解冻冷归档、深度冷归档类型的文件,请跳转至SDK简介,根据需要使用的编程语言,查询对应语言的解冻文件文档内容。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
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";
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量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实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
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();
}
}
}
}