Skip to content

Commit 5a9a578

Browse files
authored
Merge pull request #94 from aliyun/does_object_exist
fix doesObjectExist
2 parents 98d5626 + 2fbbbb3 commit 5a9a578

File tree

8 files changed

+78
-16
lines changed

8 files changed

+78
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The recommended way to use the Aliyun OSS SDK for Java in your project is to con
2424
<dependency>
2525
<groupId>com.aliyun.oss</groupId>
2626
<artifactId>aliyun-sdk-oss</artifactId>
27-
<version>2.7.0</version>
27+
<version>2.8.0</version>
2828
</dependency>
2929
```
3030

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>com.aliyun.oss</groupId>
1313
<artifactId>aliyun-sdk-oss</artifactId>
14-
<version>2.7.0</version>
14+
<version>2.8.0</version>
1515
<packaging>jar</packaging>
1616
<name>Aliyun OSS SDK for Java</name>
1717
<description>The Aliyun OSS SDK for Java used for accessing Aliyun Object Storage Service</description>

src/main/java/com/aliyun/oss/OSS.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsReque
584584
throws OSSException, ClientException;
585585

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

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

608+
/**
609+
* 判断Object是否存在,并指定是否受镜像/302等其它跳转的影响。
610+
* @param bucketName Bucket名称。
611+
* @param key Object Key。
612+
* @param isOnlyInOSS true 不受镜像/302等其它跳转的影响,只检查Object是否在OSS中;
613+
* false 受镜像/302跳转的影响,如果OSS中不存在,会根据镜像/302的配置检查Object是否存在。
614+
* @return 如果存在返回true,不存在则返回false。
615+
*/
616+
public boolean doesObjectExist(String bucketName, String key, boolean isOnlyInOSS);
617+
608618
/**
609619
* 判断指定的{@link OSSObject}是否存在。
610620
* @param headObjectRequest

src/main/java/com/aliyun/oss/OSSClient.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,15 @@ public boolean doesObjectExist(String bucketName, String key)
610610
return doesObjectExist(new GenericRequest(bucketName, key));
611611
}
612612

613+
@Override
614+
public boolean doesObjectExist(String bucketName, String key, boolean isOnlyInOSS) {
615+
if (isOnlyInOSS) {
616+
return doesObjectExist(bucketName, key);
617+
} else {
618+
return objectOperation.doesObjectExistWithRedirect(bucketName, key);
619+
}
620+
}
621+
613622
@Deprecated
614623
@Override
615624
public boolean doesObjectExist(HeadObjectRequest headObjectRequest)
@@ -620,16 +629,7 @@ public boolean doesObjectExist(HeadObjectRequest headObjectRequest)
620629
@Override
621630
public boolean doesObjectExist(GenericRequest genericRequest)
622631
throws OSSException, ClientException {
623-
try {
624-
getSimplifiedObjectMeta(genericRequest);
625-
return true;
626-
} catch (OSSException e) {
627-
if (e.getErrorCode().equals(OSSErrorCode.NO_SUCH_BUCKET)
628-
|| e.getErrorCode().equals(OSSErrorCode.NO_SUCH_KEY)) {
629-
return false;
630-
}
631-
throw e;
632-
}
632+
return objectOperation.doesObjectExist(genericRequest);
633633
}
634634

635635
@Override

src/main/java/com/aliyun/oss/internal/OSSObjectOperation.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,44 @@ public GenericResult processObject(ProcessObjectRequest processObjectRequest)
730730

731731
return doOperation(request, ResponseParsers.processObjectResponseParser, bucketName, key, true);
732732
}
733+
734+
public boolean doesObjectExist(GenericRequest genericRequest)
735+
throws OSSException, ClientException {
736+
try {
737+
this.getSimplifiedObjectMeta(genericRequest);
738+
return true;
739+
} catch (OSSException e) {
740+
if (e.getErrorCode().equals(OSSErrorCode.NO_SUCH_BUCKET)
741+
|| e.getErrorCode().equals(OSSErrorCode.NO_SUCH_KEY)) {
742+
return false;
743+
}
744+
throw e;
745+
}
746+
}
747+
748+
public boolean doesObjectExistWithRedirect(String bucketName, String key)
749+
throws OSSException, ClientException {
750+
OSSObject ossObject = null;
751+
try {
752+
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
753+
ossObject = this.getObject(getObjectRequest);
754+
return true;
755+
} catch (OSSException e) {
756+
if (e.getErrorCode() == OSSErrorCode.NO_SUCH_BUCKET
757+
|| e.getErrorCode() == OSSErrorCode.NO_SUCH_KEY) {
758+
return false;
759+
}
760+
throw e;
761+
} finally {
762+
if (ossObject != null) {
763+
try {
764+
ossObject.forcedClose();
765+
} catch (IOException e) {
766+
logException("Forced close failed: ", e);
767+
}
768+
}
769+
}
770+
}
733771

734772
private static enum MetadataDirective {
735773

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=2.7.0
1+
version=2.8.0

src/test/java/com/aliyun/oss/common/utils/VersionUtilTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class VersionUtilTest {
2828
@Test
2929
public void testGetDefaultUserAgent() {
3030
String userAgent = VersionInfoUtils.getDefaultUserAgent();
31-
assertTrue(userAgent.startsWith("aliyun-sdk-java/2.7.0("));
31+
assertTrue(userAgent.startsWith("aliyun-sdk-java/2.8.0("));
3232
assertEquals(userAgent.split("/").length, 4);
3333
assertEquals(userAgent.split(";").length, 2);
3434
assertEquals(userAgent.split("\\(").length, 2);

src/test/java/com/aliyun/oss/integrationtests/DoesObjectExistTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ public void testExistingBucketAndObject() {
5757
Assert.fail(e.getMessage());
5858
}
5959

60+
try {
61+
boolean exist = ossClient.doesObjectExist(bucketName, existingKey, true);
62+
Assert.assertTrue(exist);
63+
} catch (Exception e) {
64+
Assert.fail(e.getMessage());
65+
}
66+
67+
try {
68+
boolean exist = ossClient.doesObjectExist(bucketName, existingKey, false);
69+
Assert.assertTrue(exist);
70+
} catch (Exception e) {
71+
Assert.fail(e.getMessage());
72+
}
73+
6074
// Test another overrided interface
6175
try {
6276
boolean exist = ossClient.doesObjectExist(new HeadObjectRequest(bucketName, existingKey));

0 commit comments

Comments
 (0)