日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

Java轉換文件存儲類型

OSS提供標準、低頻訪問、歸檔、冷歸檔和深度冷歸檔多種存儲類型,全面覆蓋從熱到冷的各種數據存儲場景。本文主要介紹如何轉換文件(Object)的存儲類型。

注意事項

  • 本文以華東1(杭州)外網Endpoint為例。如果您希望通過與OSS同地域的其他阿里云產品訪問OSS,請使用內網Endpoint。關于OSS支持的RegionEndpoint的對應關系,請參見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