全部產品
Search
文件中心

Object Storage Service:初始化

更新時間:Aug 30, 2018

OSSClient是OSS的Java用戶端,用於管理儲存空間和檔案等OSS資源。使用Java SDK發起OSS請求,您需要初始化一個OSSClient執行個體,並根據需要修改ClientConfiguration的預設配置項。

新建OSSClient

新建OSSClient時,需要指定Endpoint。有關Endpoint的更多資訊,請參見訪問網域名稱和資料中心自訂訪問網域名稱

使用OSS網域名稱新建OSSClient

以下代碼用於使用OSS網域名稱新建OSSClient:

  1. // Endpoint以杭州為例,其它Region請按實際情況填寫。
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號。
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 建立OSSClient執行個體。
  7. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  8. // 關閉OSSClient。
  9. ossClient.shutdown();

您的工程中可以有一個或多個OSSClient。OSSClient可以並發使用。

使用自訂網域名新建OSSClient

以下代碼用於使用自訂網域名新建OSSClient:

  1. String endpoint = "<yourEndpoint>";
  2. // 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號。
  3. String accessKeyId = "<yourAccessKeyId>";
  4. String accessKeySecret = "<yourAccessKeySecret>";
  5. // 建立ClientConfiguration執行個體,按照您的需要修改預設參數。
  6. ClientConfiguration conf = new ClientConfiguration();
  7. // 開啟支援CNAME。CNAME是指將自訂網域名綁定到儲存空間上。
  8. conf.setSupportCname(true);
  9. // 建立OSSClient執行個體。
  10. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf);
  11. // 關閉OSSClient。
  12. ossClient.shutdown();

說明:使用自訂網域名時無法使用ossClient.listBuckets方法。

專有雲/專有域環境新建OSSClient

以下代碼用於使用專有雲或專有域環境新建OSSClient:

  1. // Endpoint以杭州為例,其它Region請按實際情況填寫。
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號。
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 建立ClientConfiguration執行個體,按照您的需要修改預設參數。
  7. ClientConfiguration conf = new ClientConfiguration();
  8. // 關閉CNAME選項。
  9. conf.setSupportCname(false);
  10. // 建立OSSClient執行個體。
  11. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf);
  12. // 關閉OSSClient。
  13. ossClient.shutdown();

使用IP新建OSSClient

以下代碼用於使用IP新建OSSClient:

  1. // 某些特殊情況(比如專有域)下,您需要將IP地址做作為Endpoint,請按照實際IP地址填寫。
  2. String endpoint = "http://10.10.10.10";
  3. // 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號。
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 建立ClientConfiguration。
  7. ClientConfiguration conf = new ClientConfiguration();
  8. // 開啟次層網域訪問OSS,預設不開啟。OSS Java SDK 2.1.2及以前的版本需要設定此值,OSS Java SDK 2.1.2以後的版本會自動檢測到IP地址,不需要再設定此值。
  9. conf.setSLDEnabled(true);
  10. // 建立OSSClient執行個體。
  11. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf);
  12. // 關閉OSSClient。
  13. ossClient.shutdown();

使用STS新建OSSClient

以下代碼用於使用STS新建一個OSSClient:

  1. // Endpoint以杭州為例,其它Region請按實際情況填寫。
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號。
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String securityToken = "<yourSecurityToken>";
  7. // 建立OSSClient執行個體。
  8. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret, securityToken);
  9. // 關閉OSSClient。
  10. ossClient.shutdown();

更多資訊請參見RAM和STS介紹授權訪問

配置OSSClient

ClientConfiguration是OSSClient的配置類,您可通過此類來配置代理、連接逾時、最大連接數等參數。可設定的參數如下:

參數 描述 方法
MaxConnections 允許開啟的最大HTTP連接數。預設為1024 ClientConfiguration.setMaxConnections
SocketTimeout Socket層傳輸資料的逾時時間(單位:毫秒)。預設為50000毫秒 ClientConfiguration.setSocketTimeout
ConnectionTimeout 建立連接的逾時時間(單位:毫秒)。預設為50000毫秒 ClientConfiguration.setConnectionTimeout
ConnectionRequestTimeout 從連接池中獲取連接的逾時時間(單位:毫秒)。預設不逾時 ClientConfiguration.setConnectionRequestTimeout
IdleConnectionTime 連接空閑逾時時間,逾時則關閉連接(單位:毫秒,預設為60000毫秒 ClientConfiguration.setIdleConnectionTime
MaxErrorRetry 請求失敗後最大的重試次數。預設3次 ClientConfiguration.setMaxErrorRetry
SupportCname 是否支援CNAME作為Endpoint,預設支援CNAME ClientConfiguration.setSupportCname
SLDEnabled 是否開啟次層網域(Second Level Domain)的訪問方式,預設不開啟 ClientConfiguration.setSLDEnabled
Protocol 連接OSS所採用的協議(HTTP/HTTPS),預設為HTTP ClientConfiguration.setProtocol
UserAgent 使用者代理程式,指HTTP的User-Agent頭。預設為”aliyun-sdk-java” ClientConfiguration.setUserAgent
ProxyHost Proxy 伺服器主機地址 ClientConfiguration.setProxyHost
ProxyPort Proxy 伺服器通信埠 ClientConfiguration.setProxyPort
ProxyUsername Proxy 伺服器驗證的使用者名 ClientConfiguration.setProxyUsername
ProxyPassword Proxy 伺服器驗證的密碼 ClientConfiguration.setProxyPassword

以下代碼用於使用ClientConfiguration設定OSSClient參數:

  1. // Endpoint以杭州為例,其它Region請按實際情況填寫。
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號。
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 建立ClientConfiguration。ClientConfiguration是OSSClient的配置類,可配置代理、連接逾時、最大連接數等參數。
  7. ClientConfiguration conf = new ClientConfiguration();
  8. // 設定OSSClient允許開啟的最大HTTP連接數,預設為1024個。
  9. conf.setMaxConnections(200);
  10. // 設定Socket層傳輸資料的逾時時間,預設為50000毫秒。
  11. conf.setSocketTimeout(10000);
  12. // 設定建立連接的逾時時間,預設為50000毫秒。
  13. conf.setConnectionTimeout(10000);
  14. // 設定從連接池中獲取連接的逾時時間(單位:毫秒),預設不逾時。
  15. conf.setConnectionRequestTimeout(1000);
  16. // 設定連接空閑逾時時間。逾時則關閉連接,預設為60000毫秒。
  17. conf.setIdleConnectionTime(10000);
  18. // 設定失敗請求重試次數,預設為3次。
  19. conf.setMaxErrorRetry(5);
  20. // 設定是否支援將自訂網域名作為Endpoint,預設支援。
  21. conf.setSupportCname(true);
  22. // 設定是否開啟次層網域的訪問方式,預設不開啟。
  23. conf.setSLDEnabled(true);
  24. // 設定連接OSS所使用的協議(HTTP/HTTPS),預設為HTTP。
  25. conf.setProtocol(Protocol.HTTP);
  26. // 設定使用者代理程式,指HTTP的User-Agent頭,預設為aliyun-sdk-java。
  27. conf.setUserAgent("aliyun-sdk-java");
  28. // 設定Proxy 伺服器通信埠。
  29. conf.setProxyHost("<yourProxyHost>");
  30. // 設定Proxy 伺服器驗證的使用者名。
  31. conf.setProxyUsername("<yourProxyUserName>");
  32. // 設定Proxy 伺服器驗證的密碼。
  33. conf.setProxyPassword("<yourProxyPassword>");
  34. // 建立OSSClient執行個體。
  35. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf);
  36. // 關閉OSSClient。
  37. ossClient.shutdown();

更多資訊請參見阿里雲OSS Java SDK逾時時間設定