全部產品
Search
文件中心

Object Storage Service:判斷檔案是否存在

更新時間:Aug 30, 2018

以下代碼用於判斷指定的檔案是否存在:

  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. // 判斷檔案是否存在。
  9. boolean found = ossClient.doesObjectExist("<yourBucketName>", "<yourObjectName>");
  10. System.out.println(found);
  11. // 關閉OSSClient。
  12. ossClient.shutdown();