OBS JAVA SDK 实践6:转换已存在对象存储类型
1 背景介绍 & 效果展示
OBS提供三种存储类型:标准存储、低频访问存储或归档存储。低频访问存储的对象可以修改为标准存储或归档存储的对象。归档存储的对象要先恢复,才可以修改为标准存储或低频访问存储。本文通过JAVA SDK实现将对象从标准存储转换为归档存储,以及归档存储到低频访问存储的转换,效果如下。
注:有关标准存储、低频访问、归档存储三种存储类型的更多信息,参考链接:https://support.huaweicloud.com/usermanual-obs/zh-cn_topic_0050321036.html
图1 原始对象
图2 对象从标准存储转换为归档存储
图3 对象从归档存储转换为低频访问存储
2 java代码参考(对象从标准存储转换为归档存储)
前提:
需要先下载OBS最新的JAVA SDK,参考官网链接:https://developer.huaweicloud.com/sdk?OBS,并在自己的项目中导入SDK中的JAR包。
public static void main(String[] args) throws IOException {
// 需要将endPoint/ak/sk更新为实际信息
String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// 创建ObsClient实例
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
try
{
String bucketName = "bucketXXX"; // 需要将bucketName更新为实际信息
String objectName = "objectXXX"; // 需要将objectName更新为实际信息
CopyObjectRequest request = new CopyObjectRequest(bucketName, objectName, bucketName, objectName);
request.setReplaceMetadata(true);
ObjectMetadata newObjectMetadata = new ObjectMetadata();
newObjectMetadata.setContentType("image/jpeg"); // 修改对象的ContentType
newObjectMetadata.setObjectStorageClass(StorageClassEnum.COLD); // 转换为归档存储
request.setNewObjectMetadata(newObjectMetadata);
CopyObjectResult result = obsClient.copyObject(request);
System.out.println("\t" + result.getStatusCode());
}
catch (ObsException e)
{
System.out.println("Response Code: " + e.getResponseCode());
System.out.println("Error Message: " + e.getErrorMessage());
System.out.println("Error Code: " + e.getErrorCode());
System.out.println("Request ID: " + e.getErrorRequestId());
System.out.println("Host ID: " + e.getErrorHostId());
}
}
3 java代码参考(对象从归档存储转换为低频访问存储)
前提:
需要先下载OBS最新的JAVA SDK,参考官网链接:https://developer.huaweicloud.com/sdk?OBS,并在自己的项目中导入SDK中的JAR包。
public static void main(String[] args) throws IOException {
// 需要将endPoint/ak/sk更新为实际信息
String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// 创建ObsClient实例
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
try
{
String bucketName = "bucketXXX"; // 需要将bucketName更新为实际信息
String objectName = "objectXXX"; // 需要将objectName更新为实际信息
// 快速取回归档对象
RestoreObjectRequest request1 = new RestoreObjectRequest();
request1.setBucketName(bucketName);
request1.setObjectKey(objectName);
request1.setDays(1);
request1.setRestoreTier(RestoreTierEnum.EXPEDITED);
obsClient.restoreObjectV2(request1);
// 等待对象取回
Thread.sleep(60 * 6 * 1000);
CopyObjectRequest request = new CopyObjectRequest(bucketName, objectName, bucketName, objectName);
ObjectMetadata newObjectMetadata = new ObjectMetadata();
newObjectMetadata.setObjectStorageClass(StorageClassEnum.WARM); // 转换为低频访问存储
request.setNewObjectMetadata(newObjectMetadata);
CopyObjectResult result = obsClient.copyObject(request);
System.out.println("\t" + result.getStatusCode());
}
catch (ObsException e)
{
System.out.println("Response Code: " + e.getResponseCode());
System.out.println("Error Message: " + e.getErrorMessage());
System.out.println("Error Code: " + e.getErrorCode());
System.out.println("Request ID: " + e.getErrorRequestId());
System.out.println("Host ID: " + e.getErrorHostId());
}
}
4 附录链接
OBS JAVA SDK使用方法,官网参考链接:https://support.huaweicloud.com/sdk-java-devg-obs/zh-cn_topic_0142815558.html
- 点赞
- 收藏
- 关注作者
评论(0)