全部产品
Search
文档中心

对象存储 OSS:Java转换文件存储类型

更新时间:Oct 24, 2024

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

注意事项

  • 本文以华东1(杭州)外网Endpoint为例。如果您希望通过与OSS同地域的其他阿里云产品访问OSS,请使用内网Endpoint。关于OSS支持的Region与Endpoint的对应关系,请参见OSS访问域名、数据中心、开放端口

  • 本文以从环境变量读取访问凭证为例。如何配置访问凭证,请参见Java配置访问凭证

  • 本文以OSS域名新建OSSClient为例。如果您希望通过自定义域名、STS等方式新建OSSClient,请参见新建OSSClient

  • 要转换文件存储类型,您必须具有oss:GetObjectoss:PutObjectoss:RestoreObject权限。具体操作,请参见为RAM用户授权自定义的权限策略

示例代码

重要

当低频访问、归档、冷归档或者深度冷归档存储类型Object在存储不足规定时长时转换了存储类型或者提前删除时,需要收取不足规定时长容量费用。更多信息,请参见Object在存储不足规定时长时如何计费?

将标准或低频访问转换为归档、冷归档或者深度冷归档类型

以下代码用于将Object的存储类型从标准或低频访问转换为归档、冷归档或者深度冷归档类型。

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.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";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 本示例中的Bucket与Object需提前创建好, 且Object类型为标准或低频访问存储类型。
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "examplebucket";
        // 填写不包含Bucket名称在内的Object完整路径,例如exampleobject.txt。
        String objectName = "exampleobject.txt";
        // 填写Bucket所在地域。以华东1(杭州)为例,Region填写为cn-hangzhou。
        String region = "cn-hangzhou";

        // 创建OSSClient实例。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);        
        OSS ossClient = OSSClientBuilder.create()
        .endpoint(endpoint)
        .credentialsProvider(credentialsProvider)
        .clientConfiguration(clientBuilderConfiguration)
        .region(region)               
        .build();

        try {
            // 创建CopyObjectRequest对象。
            CopyObjectRequest request = new CopyObjectRequest(bucketName, objectName, bucketName, objectName) ;

            // 创建ObjectMetadata对象。
            ObjectMetadata objectMetadata = new ObjectMetadata();

            // 将Object存储类型转换为归档类型。
            objectMetadata.setHeader("x-oss-storage-class", StorageClass.Archive);
            // 将Object存储类型转换为冷归档类型。
            // objectMetadata.setHeader("x-oss-storage-class", StorageClass.ColdArchive);
            // 将Object存储类型转换为深度冷归档类型。
            // objectMetadata.setHeader("x-oss-storage-class", StorageClass.DeepColdArchive);
            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.common.auth.*;
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";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 本示例中的Bucket与Object需提前创建好, 且Object类型为归档存储类型。
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "examplebucket";
        // 填写不包含Bucket名称在内的Object完整路径,例如exampleobject.txt。
        String objectName = "exampleobject.txt";
        // 填写Bucket所在地域。以华东1(杭州)为例,Region填写为cn-hangzhou。
        String region = "cn-hangzhou";

        // 创建OSSClient实例。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);        
        OSS ossClient = OSSClientBuilder.create()
        .endpoint(endpoint)
        .credentialsProvider(credentialsProvider)
        .clientConfiguration(clientBuilderConfiguration)
        .region(region)               
        .build();

        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();

            // 将Object存储类型转换为低频访问类型。
            objectMetadata.setHeader("x-oss-storage-class", StorageClass.IA);
            // 将Object存储类型转换为标准存储类型。
            // objectMetadata.setHeader("x-oss-storage-class", StorageClass.Standard);
            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