全部產品
Search
文件中心

Object Storage Service:Java重新命名檔案

更新時間:Oct 25, 2024

本文介紹如何重新命名檔案(Object)。

注意事項

  • 本文以華東1(杭州)外網Endpoint為例。如果您希望通過與OSS同地區的其他阿里雲產品訪問OSS,請使用內網Endpoint。關於OSS支援的Region與Endpoint的對應關係,請參見OSS訪問網域名稱、資料中心、開放連接埠

  • 本文以從環境變數讀取存取憑證為例。如何配置訪問憑證,請參見Java配置訪問憑證

  • 本文以OSS網域名稱建立OSSClient為例。如果您希望通過自訂網域名、STS等方式建立OSSClient,請參見建立OSSClient

  • 在Bucket開啟階層命名空間的情況下,OSS支援直接調用Rename介面對Object進行重新命名。

    關於儲存空間開啟階層命名空間的具體操作,請參見建立儲存空間

  • 在Bucket未開啟階層命名空間的情況下,OSS不支援直接對Object進行重新命名。如果您需要在同一個Bucket內對Object進行重新命名,您可以通過CopyObject介面將源Object拷貝至目標Object,然後通過DeleteObject介面刪除源Object。

範例程式碼

以下代碼用於將examplebucket中的srcobject.txt檔案重新命名為destobject.txt。

  • 重新命名樣本(Bucket已開啟階層命名空間)

    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.RenameObjectRequest;
    
    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名稱。
            String bucketName = "examplebucket";
            // 填寫源Object的完整路徑,完整路徑中不能包含Bucket名稱。
            String sourceObject = "srcobject.txt";
            // 填寫與源Object處於同一Bucket中的目標Object絕對路徑。Object絕對路徑中不能包含Bucket名稱。
            String destinationObject = "destobject.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 {
                // 將儲存空間中的源Object絕對路徑重新命名為目標Object絕對路徑。
                RenameObjectRequest renameObjectRequest = new RenameObjectRequest(bucketName, sourceObject, destinationObject);
                ossClient.renameObject(renameObjectRequest);
            } 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();
                }
            }
        }
    }
  • 重新命名樣本(Bucket未開啟階層命名空間)

    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;
    
    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名稱。
            String bucketName = "examplebucket";
            // 填寫源Object的完整路徑,完整路徑中不能包含Bucket名稱。
            String sourceKey = "srcobject.txt";
            // 填寫目標Object的完整路徑。Object完整路徑中不能包含Bucket名稱。
            String destinationKey = "destobject.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 {
                // 將examplebucket下的srcobject.txt拷貝至同一Bucket下的destobject.txt。
                ossClient.copyObject(bucketName, sourceKey, bucketName, destinationKey);
    
                // 刪除srcobject.txt。
                ossClient.deleteObject(bucketName, sourceKey);
            } 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();
                }
            }
        }
    }
    說明

    OSS也不支援直接對目錄進行重新命名。如果需要重新命名目錄,您可以參考以上樣本對該目錄下的子目錄和Object逐個進行重新命名操作。

相關文檔

關於重新命名檔案涉及的API介面說明,請分別參見CopyObjectDeleteObject

關於重新命名的API介面說明,請參見Rename