Skip to content

Commit 28fbbfa

Browse files
committed
fix doesObjectExist
1 parent 98d5626 commit 28fbbfa

File tree

7 files changed

+53
-6
lines changed

7 files changed

+53
-6
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: 23 additions & 0 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 doesObjectExistWithRedirect(bucketName, key);
619+
}
620+
}
621+
613622
@Deprecated
614623
@Override
615624
public boolean doesObjectExist(HeadObjectRequest headObjectRequest)
@@ -632,6 +641,20 @@ public boolean doesObjectExist(GenericRequest genericRequest)
632641
}
633642
}
634643

644+
private boolean doesObjectExistWithRedirect(String bucketName, String key) {
645+
try {
646+
HeadObjectRequest headObjectRequest = new HeadObjectRequest(bucketName, key);
647+
this.objectOperation.headObject(headObjectRequest);
648+
return true;
649+
} catch (OSSException e) {
650+
if (e.getErrorCode() == OSSErrorCode.NO_SUCH_BUCKET
651+
|| e.getErrorCode() == OSSErrorCode.NO_SUCH_KEY) {
652+
return false;
653+
}
654+
throw e;
655+
}
656+
}
657+
635658
@Override
636659
public void setObjectAcl(String bucketName, String key, CannedAccessControlList cannedACL)
637660
throws OSSException, ClientException {
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)