Skip to content

Commit

Permalink
Merge pull request #94 from aliyun/does_object_exist
Browse files Browse the repository at this point in the history
fix doesObjectExist
  • Loading branch information
baiyubin2020 authored Jul 7, 2017
2 parents 98d5626 + 2fbbbb3 commit 5a9a578
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The recommended way to use the Aliyun OSS SDK for Java in your project is to con
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.7.0</version>
<version>2.8.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.7.0</version>
<version>2.8.0</version>
<packaging>jar</packaging>
<name>Aliyun OSS SDK for Java</name>
<description>The Aliyun OSS SDK for Java used for accessing Aliyun Object Storage Service</description>
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/aliyun/oss/OSS.java
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsReque
throws OSSException, ClientException;

/**
* 判断指定{@link Bucket}下是否存在指定的{@link OSSObject}。
* 判断指定{@link Bucket}下是否存在指定的{@link OSSObject}。不受镜像/302跳转或者其它跳转功能的影响。
* @param bucketName
* Bucket名称。
* @param key
Expand All @@ -596,7 +596,7 @@ public boolean doesObjectExist(String bucketName, String key)
throws OSSException, ClientException;

/**
* 判断指定的{@link OSSObject}是否存在
* 判断指定的{@link OSSObject}在OSS上是否存在
* @param genericRequest
* 请求参数{@link GenericRequest}实例。
* @return
Expand All @@ -605,6 +605,16 @@ public boolean doesObjectExist(String bucketName, String key)
public boolean doesObjectExist(GenericRequest genericRequest)
throws OSSException, ClientException;

/**
* 判断Object是否存在,并指定是否受镜像/302等其它跳转的影响。
* @param bucketName Bucket名称。
* @param key Object Key。
* @param isOnlyInOSS true 不受镜像/302等其它跳转的影响,只检查Object是否在OSS中;
* false 受镜像/302跳转的影响,如果OSS中不存在,会根据镜像/302的配置检查Object是否存在。
* @return 如果存在返回true,不存在则返回false。
*/
public boolean doesObjectExist(String bucketName, String key, boolean isOnlyInOSS);

/**
* 判断指定的{@link OSSObject}是否存在。
* @param headObjectRequest
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/aliyun/oss/OSSClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,15 @@ public boolean doesObjectExist(String bucketName, String key)
return doesObjectExist(new GenericRequest(bucketName, key));
}

@Override
public boolean doesObjectExist(String bucketName, String key, boolean isOnlyInOSS) {
if (isOnlyInOSS) {
return doesObjectExist(bucketName, key);
} else {
return objectOperation.doesObjectExistWithRedirect(bucketName, key);
}
}

@Deprecated
@Override
public boolean doesObjectExist(HeadObjectRequest headObjectRequest)
Expand All @@ -620,16 +629,7 @@ public boolean doesObjectExist(HeadObjectRequest headObjectRequest)
@Override
public boolean doesObjectExist(GenericRequest genericRequest)
throws OSSException, ClientException {
try {
getSimplifiedObjectMeta(genericRequest);
return true;
} catch (OSSException e) {
if (e.getErrorCode().equals(OSSErrorCode.NO_SUCH_BUCKET)
|| e.getErrorCode().equals(OSSErrorCode.NO_SUCH_KEY)) {
return false;
}
throw e;
}
return objectOperation.doesObjectExist(genericRequest);
}

@Override
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/aliyun/oss/internal/OSSObjectOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,44 @@ public GenericResult processObject(ProcessObjectRequest processObjectRequest)

return doOperation(request, ResponseParsers.processObjectResponseParser, bucketName, key, true);
}

public boolean doesObjectExist(GenericRequest genericRequest)
throws OSSException, ClientException {
try {
this.getSimplifiedObjectMeta(genericRequest);
return true;
} catch (OSSException e) {
if (e.getErrorCode().equals(OSSErrorCode.NO_SUCH_BUCKET)
|| e.getErrorCode().equals(OSSErrorCode.NO_SUCH_KEY)) {
return false;
}
throw e;
}
}

public boolean doesObjectExistWithRedirect(String bucketName, String key)
throws OSSException, ClientException {
OSSObject ossObject = null;
try {
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
ossObject = this.getObject(getObjectRequest);
return true;
} catch (OSSException e) {
if (e.getErrorCode() == OSSErrorCode.NO_SUCH_BUCKET
|| e.getErrorCode() == OSSErrorCode.NO_SUCH_KEY) {
return false;
}
throw e;
} finally {
if (ossObject != null) {
try {
ossObject.forcedClose();
} catch (IOException e) {
logException("Forced close failed: ", e);
}
}
}
}

private static enum MetadataDirective {

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/versioninfo.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2.7.0
version=2.8.0
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class VersionUtilTest {
@Test
public void testGetDefaultUserAgent() {
String userAgent = VersionInfoUtils.getDefaultUserAgent();
assertTrue(userAgent.startsWith("aliyun-sdk-java/2.7.0("));
assertTrue(userAgent.startsWith("aliyun-sdk-java/2.8.0("));
assertEquals(userAgent.split("/").length, 4);
assertEquals(userAgent.split(";").length, 2);
assertEquals(userAgent.split("\\(").length, 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ public void testExistingBucketAndObject() {
Assert.fail(e.getMessage());
}

try {
boolean exist = ossClient.doesObjectExist(bucketName, existingKey, true);
Assert.assertTrue(exist);
} catch (Exception e) {
Assert.fail(e.getMessage());
}

try {
boolean exist = ossClient.doesObjectExist(bucketName, existingKey, false);
Assert.assertTrue(exist);
} catch (Exception e) {
Assert.fail(e.getMessage());
}

// Test another overrided interface
try {
boolean exist = ossClient.doesObjectExist(new HeadObjectRequest(bucketName, existingKey));
Expand Down

0 comments on commit 5a9a578

Please sign in to comment.