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

Java靜態網站托管(鏡像回源)

您可以將存儲空間(Bucket)設置為靜態網站托管模式并設置鏡像回源的跳轉規則(RoutingRule)。靜態網站托管模式配置生效后,訪問網站相當于訪問Bucket,并且能夠自動跳轉至指定的索引頁面和錯誤頁面。鏡像回源的跳轉規則配置生效后,可用于數據無縫遷移到OSS的場景。

注意事項

  • 本文以華東1(杭州)外網Endpoint為例。如果您希望通過與OSS同地域的其他阿里云產品訪問OSS,請使用內網Endpoint。關于OSS支持的RegionEndpoint的對應關系,請參見OSS訪問域名、數據中心、開放端口。

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

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

  • 要設置靜態網站托管或者鏡像回源,您必須有oss:PutBucketWebsite權限;要獲取靜態網站托管或者鏡像回源,您必須有oss:GetBucketWebsite權限;要刪除靜態網站托管或者鏡像回源,您必須有oss:DeleteBucketWebsite權限。具體操作,請參見RAM用戶授權自定義的權限策略。

靜態網站托管

  • 設置靜態網站托管

    以下代碼用于設置靜態網站托管:

    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.SetBucketWebsiteRequest;
    
    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_IDOSS_ACCESS_KEY_SECRET。
            EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
            // 填寫Bucket名稱,例如examplebucket。
            String bucketName = "examplebucket";
            // 填寫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 {
                // 填寫Bucket名稱。
                SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName);
                // 設置靜態網站托管的默認主頁。
                request.setIndexDocument("index.html");
                // 設置靜態網站托管的默認404頁。
                request.setErrorDocument("error.html");
                ossClient.setBucketWebsite(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();
                }
            }
        }
    }
  • 查看靜態網站托管配置

    以下代碼用于查看靜態網站托管配置:

    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.BucketWebsiteResult;
    
    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_IDOSS_ACCESS_KEY_SECRET。
            EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
            // 填寫Bucket名稱,例如examplebucket。
            String bucketName = "examplebucket";
            // 填寫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 {
                // 填寫Bucket名稱。
                BucketWebsiteResult result = ossClient.getBucketWebsite(bucketName);
                // 查看靜態網站托管配置的默認首頁和默認404頁。
                System.out.println(result.getIndexDocument());
                System.out.println(result.getErrorDocument());
            } 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();
                }
            }
        }
    }
  • 刪除靜態網站托管配置

    以下代碼用于刪除靜態網站托管配置:

    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_IDOSS_ACCESS_KEY_SECRET。
            EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
            // 填寫Bucket名稱,例如examplebucket。
            String bucketName = "examplebucket";
            // 填寫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 {
                // 填寫Bucket名稱。
                ossClient.deleteBucketWebsite(bucketName);
            } 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的場景。例如某服務已經在用戶建立的源站或者在其他云產品上運行,現因業務發展,需要將服務遷移至OSS,遷移時需保證服務的正常運行。您可以在遷移過程中使用鏡像回源規則獲取未遷移至OSS的部分數據,保證服務的正常運行。

  • 設置鏡像回源

    例如,當請求者訪問目標Bucket中不存在的文件時,可以通過指定回源條件和回源地址,從源站中獲取目標文件。例如您在華東1(杭州)有名為examplebucketBucket,您希望請求者訪問Bucket根目錄下examplefolder目錄中不存在的文件時,可以從https://www.example.com/站點的examplefolder目錄獲取目標文件。

    以下代碼用于設置符合上述場景的鏡像回源規則:

    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.RoutingRule;
    import com.aliyun.oss.model.SetBucketWebsiteRequest;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    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_IDOSS_ACCESS_KEY_SECRET。
            EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
            // 填寫Bucket名稱,例如examplebucket。
            String bucketName = "examplebucket";
            // 填寫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 {
                SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName);
                // 設置默認主頁后,訪問以非正斜線(/)結尾的Object,且該Object不存在時的行為。
                //request.setSubDirType(null);
                // 指定訪問子目錄時,是否支持轉到子目錄下的默認主頁。
                //request.setSupportSubDir(false);
    
                List<RoutingRule> routingRules = new ArrayList<RoutingRule>();
    
                RoutingRule rule = new RoutingRule();
                rule.setNumber(1);
                // 只有匹配此前綴的Object才能匹配此規則。
                rule.getCondition().setKeyPrefixEquals("examplebucket");
                // 訪問指定Object時,返回status 404才能匹配此規則。
                rule.getCondition().setHttpErrorCodeReturnedEquals(404);
                // 指定跳轉的類型。
                rule.getRedirect().setRedirectType(RoutingRule.RedirectType.Mirror);
                // 指定鏡像回源的源站地址。例如https://www.example.com/。
                rule.getRedirect().setMirrorURL("<yourMirrorURL>");
                //rule.getRedirect().setMirrorRole("AliyunOSSMirrorDefaultRole");
                // 指定執行跳轉或者鏡像回源規則時,是否攜帶請求參數。
                rule.getRedirect().setPassQueryString(true);
                // 與PassQueryString作用相同,優先級高于PassQueryString。只有設置RedirectTypeMirror時生效。
                rule.getRedirect().setMirrorPassQueryString(true);
                // 指定跳轉時返回的狀態碼。只有設置RedirectTypeExternal或者AliCDN時生效。
                //rule.getRedirect().setHttpRedirectCode(302);
                // 指定跳轉時的域名,域名需符合域名規范。
                //rule.getRedirect().setHostName("oss.aliyuncs.com");
                // 指定跳轉時的協議。只有設置RedirectTypeExternal或者AliCDN時才生效。
                //rule.getRedirect().setProtocol(RoutingRule.Protocol.Https);
                // RedirectObject名稱將替換成ReplaceKeyWith指定的值,ReplaceKeyWith支持設置變量。
                //rule.getRedirect().setReplaceKeyWith("${key}.jpg");
                // 如果設置此字段為true,則Object的前綴將被替換為ReplaceKeyPrefixWith指定的值。
                rule.getRedirect().setEnableReplacePrefix(true);
                // RedirectObject名稱的前綴將替換成該值。
                rule.getRedirect().setReplaceKeyPrefixWith("examplebucket");
                // 是否檢查回源bodyMD5。只有設置RedirectTypeMirror時生效。
                rule.getRedirect().setMirrorCheckMd5(true);
    
                RoutingRule.MirrorHeaders mirrorHeaders = new RoutingRule.MirrorHeaders();
                // 是否透傳除以下Header之外的其他Header到源站。只有設置RedirectTypeMirror時生效。
                mirrorHeaders.setPassAll(false);
                List passes = new ArrayList<String>();
                passes.add("cache-control");
                // 透傳指定的Header到源站。只有設置RedirectTypeMirror時生效。
                mirrorHeaders.setPass(passes);
                List removes = new ArrayList<String>();
                removes.add("content-type");
                // 禁止透傳指定的Header到源站。只有設置RedirectTypeMirror時生效。
                mirrorHeaders.setRemove(removes);
                List sets = new ArrayList<Map<String, String>>();
                Map header1 = new HashMap<String, String>();
                header1.put("Key", "key1");
                header1.put("Value", "value1");
                Map header2 = new HashMap<String, String>();
                header2.put("Key", "key2");
                header2.put("Value", "value2");
                sets.add(header1);
                sets.add(header2);
                // 設置傳到源站的Header。不管請求中是否攜帶這些指定的Header,回源時都會設置這些Header。
                mirrorHeaders.setSet(sets);
                // 指定回源時攜帶的Header。只有設置RedirectTypeMirror時才生效。
                rule.getRedirect().setMirrorHeaders(mirrorHeaders);
    
                routingRules.add(rule);
                request.setRoutingRules(routingRules);
                ossClient.setBucketWebsite(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();
                }
            }
        }
    }
  • 獲取鏡像回源配置

    以下代碼用于獲取鏡像回源配置:

    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.BucketWebsiteResult;
    
    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_IDOSS_ACCESS_KEY_SECRET。
            EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
            // 填寫Bucket名稱,例如examplebucket。
            String bucketName = "examplebucket";
            // 填寫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 {
                BucketWebsiteResult result = ossClient.getBucketWebsite(bucketName);
                result.getSubDirType();
                // 獲取匹配并執行跳轉規則或者鏡像回源規則的序號。
                System.out.println(result.getRoutingRules().get(0).getNumber());
                // 獲取規則匹配的前綴。
                System.out.println(result.getRoutingRules().get(0).getCondition().getKeyPrefixEquals());
                // 獲取HTTP狀態碼。
                System.out.println(result.getRoutingRules().get(0).getCondition().getHttpErrorCodeReturnedEquals());
                // 獲取規則匹配的后綴。
                System.out.println(result.getRoutingRules().get(0).getCondition().getKeySuffixEquals());
                // 獲取跳轉類型。
                System.out.println(result.getRoutingRules().get(0).getRedirect().getRedirectType());
                // 獲取攜帶的請求參數。
                System.out.println(result.getRoutingRules().get(0).getRedirect().getMirrorPassQueryString());
                // 獲取鏡像回源的源站地址。
                System.out.println(result.getRoutingRules().get(0).getRedirect().getMirrorURL());
                // 獲取跳轉時返回的狀態碼。
                System.out.println(result.getRoutingRules().get(0).getRedirect().getHttpRedirectCode());
                // 獲取透傳指定的Header。
                System.out.println(result.getRoutingRules().get(0).getRedirect().getMirrorHeaders().getPass().get(0));
                // 獲取禁止透傳指定的Header。
                System.out.println(result.getRoutingRules().get(0).getRedirect().getMirrorHeaders().getRemove().get(0));
                // 獲取跳轉時的協議。
                System.out.println(result.getRoutingRules().get(0).getRedirect().getProtocol());
                // 獲取跳轉時的域名。
                System.out.println(result.getRoutingRules().get(0).getRedirect().getHostName());
                // 獲取RedirectObject名稱的前綴替換值。如果前綴為空,則將這個字符串插入Object名稱的前面。
                System.out.println(result.getRoutingRules().get(0).getRedirect().getReplaceKeyPrefixWith());
                // 獲取RedirectObject名稱通過ReplaceKeyWith指定的替換值,ReplaceKeyWith支持變量。
                System.out.println(result.getRoutingRules().get(0).getRedirect().getReplaceKeyWith());
                // 獲取跳轉時返回的狀態碼。
                System.out.println(result.getRoutingRules().get(0).getRedirect().getHttpRedirectCode());
            } 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();
                }
            }
        }
    }
  • 刪除鏡像回源配置

    以下代碼用于刪除鏡像回源配置:

    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_IDOSS_ACCESS_KEY_SECRET。
            EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
            // 填寫Bucket名稱,例如examplebucket。
            String bucketName = "examplebucket";        
            // 填寫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 {
                // 填寫Bucket名稱。
                ossClient.deleteBucketWebsite(bucketName);
            } 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接口說明,請參見PutBucketWebsite。

  • 關于獲取靜態網站托管或者鏡像回源的API接口說明,請參見GetBucketWebsite。

  • 關于刪除靜態網站托管或者鏡像回源的API接口說明,請參見DeleteBucketWebsite。