Skip to content

Commit 2fbbbb3

Browse files
committed
fix doesObjectExist
1 parent 28fbbfa commit 2fbbbb3

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

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

Lines changed: 3 additions & 3 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}。不受镜像/302跳转的影响
587+
* 判断指定{@link Bucket}下是否存在指定的{@link OSSObject}。不受镜像/302跳转或者其它跳转功能的影响
588588
* @param bucketName
589589
* Bucket名称。
590590
* @param key
@@ -606,10 +606,10 @@ public boolean doesObjectExist(GenericRequest genericRequest)
606606
throws OSSException, ClientException;
607607

608608
/**
609-
* 判断Object是否存在,并指定是否受镜像/302跳转的影响
609+
* 判断Object是否存在,并指定是否受镜像/302等其它跳转的影响
610610
* @param bucketName Bucket名称。
611611
* @param key Object Key。
612-
* @param isOnlyInOSS true 不受镜像/302跳转的影响,只检查Object是否在OSS中;
612+
* @param isOnlyInOSS true 不受镜像/302等其它跳转的影响,只检查Object是否在OSS中;
613613
* false 受镜像/302跳转的影响,如果OSS中不存在,会根据镜像/302的配置检查Object是否存在。
614614
* @return 如果存在返回true,不存在则返回false。
615615
*/

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

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ public boolean doesObjectExist(String bucketName, String key, boolean isOnlyInOS
615615
if (isOnlyInOSS) {
616616
return doesObjectExist(bucketName, key);
617617
} else {
618-
return doesObjectExistWithRedirect(bucketName, key);
618+
return objectOperation.doesObjectExistWithRedirect(bucketName, key);
619619
}
620620
}
621621

@@ -629,30 +629,7 @@ public boolean doesObjectExist(HeadObjectRequest headObjectRequest)
629629
@Override
630630
public boolean doesObjectExist(GenericRequest genericRequest)
631631
throws OSSException, ClientException {
632-
try {
633-
getSimplifiedObjectMeta(genericRequest);
634-
return true;
635-
} catch (OSSException e) {
636-
if (e.getErrorCode().equals(OSSErrorCode.NO_SUCH_BUCKET)
637-
|| e.getErrorCode().equals(OSSErrorCode.NO_SUCH_KEY)) {
638-
return false;
639-
}
640-
throw e;
641-
}
642-
}
643-
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-
}
632+
return objectOperation.doesObjectExist(genericRequest);
656633
}
657634

658635
@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

0 commit comments

Comments
 (0)