OSS提供标准、低频访问、归档和冷归档四种存储类型,全面覆盖从热到冷的各种数据存储场景。本文主要介绍如何转换文件(Object)的存储类型。

示例代码

以下提供了详细的示例代码用于Object存储类型的相互转换。
  • 以下代码用于将Object的存储类型从标准或低频访问转换为归档类型:
    import com.aliyun.oss.ClientException;
    import com.aliyun.oss.OSS;
    import com.aliyun.oss.OSSClientBuilder;
    import com.aliyun.oss.OSSException;
    import com.aliyun.oss.model.CopyObjectRequest;
    import com.aliyun.oss.model.CopyObjectResult;
    import com.aliyun.oss.model.ObjectMetadata;
    import com.aliyun.oss.model.StorageClass;
    
    public class Demo {
        public static void main(String[] args) throws Exception {
            // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
            String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
            // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
            String accessKeyId = "yourAccessKeyId";
            String accessKeySecret = "yourAccessKeySecret";
            // 本示例中的Bucket与Object需提前创建好, 且Object类型为标准或低频访问存储类型。
            // 填写Bucket名称,例如examplebucket。
            String bucketName = "examplebucket";
            // 填写不包含Bucket名称在内的Object完整路径,例如exampleobject.txt。
            String objectName = "exampleobject.txt";
    
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
            try {
                // 创建CopyObjectRequest对象。
                CopyObjectRequest request = new CopyObjectRequest(bucketName, objectName, bucketName, objectName) ;
    
                // 创建ObjectMetadata对象。
                ObjectMetadata objectMetadata = new ObjectMetadata();
    
                // 封装header,此处以设置存储类型为归档类型为例。
                objectMetadata.setHeader("x-oss-storage-class", StorageClass.Archive);
                request.setNewObjectMetadata(objectMetadata);
    
                // 更改文件存储类型。
                CopyObjectResult result = ossClient.copyObject(request);
            } 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();
                }
            }
        }
    }
  • 以下代码用于将Object的存储类型从归档转换为低频访问类型:
    package com.aliyun.oss.demo;
    
    import com.aliyun.oss.ClientException;
    import com.aliyun.oss.OSS;
    import com.aliyun.oss.OSSClientBuilder;
    import com.aliyun.oss.OSSException;
    import com.aliyun.oss.model.CopyObjectRequest;
    import com.aliyun.oss.model.CopyObjectResult;
    import com.aliyun.oss.model.ObjectMetadata;
    import com.aliyun.oss.model.StorageClass;
    
    public class Demo {
        public static void main(String[] args) throws Exception {
            // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
            String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
            // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
            String accessKeyId = "yourAccessKeyId";
            String accessKeySecret = "yourAccessKeySecret";
            // 本示例中的Bucket与Object需提前创建好, 且Object类型为归档存储类型。
            // 填写Bucket名称,例如examplebucket。
            String bucketName = "examplebucket";
            // 填写不包含Bucket名称在内的Object完整路径,例如exampleobject.txt。
            String objectName = "exampleobject.txt";
    
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
            try {
                // 获取文件元信息。
                ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, objectName);
    
                // 检查目标文件是否为归档类型。如果是,则需要先解冻才能更改存储类型。
                StorageClass storageClass = objectMetadata.getObjectStorageClass();
                System.out.println("storage type:" + storageClass);
                if (storageClass == StorageClass.Archive) {
                    // 解冻文件。
                    ossClient.restoreObject(bucketName, objectName);
    
                    // 等待解冻完成。
                    do {
                        Thread.sleep(1000);
                        objectMetadata = ossClient.getObjectMetadata(bucketName, objectName);
                    } while (!objectMetadata.isRestoreCompleted());
                }
    
                // 创建CopyObjectRequest对象。
                CopyObjectRequest request = new CopyObjectRequest(bucketName, objectName, bucketName, objectName) ;
    
                // 创建ObjectMetadata对象。
                objectMetadata = new ObjectMetadata();
    
                // 封装header,此处以设置存储类型为低频访问类型为例。
                objectMetadata.setHeader("x-oss-storage-class", StorageClass.IA);
                request.setNewObjectMetadata(objectMetadata);
    
                // 更改文件存储类型。
                CopyObjectResult result = ossClient.copyObject(request);
    
            } 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();
                }
            }
        }
    }

相关文档

  • 关于转换文件存储类型的完整示例代码,请参见GitHub示例
  • 关于转换文件存储类型的API接口说明,请参见CopyObject