Skip to content

Commit 7525162

Browse files
liyanzhang505huiguangjun
authored andcommitted
list objects v2.
1 parent 1cd0681 commit 7525162

File tree

13 files changed

+1560
-18
lines changed

13 files changed

+1560
-18
lines changed

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

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,85 @@ public void setBucketVersioning(SetBucketVersioningRequest setBucketVersioningRe
561561
* @throws ClientException
562562
*/
563563
public ObjectListing listObjects(ListObjectsRequest listObjectsRequest) throws OSSException, ClientException;
564-
564+
565+
/**
566+
* Lists all objects under the specified {@link Bucket} in the parameter of
567+
* {@link ListObjectsRequest}
568+
*
569+
* @param listObjectsV2Request
570+
* The {@link ListObjectsRequest} instance that defines the
571+
* bucket name as well as the criteria such as prefix, marker,
572+
* maxKeys, delimiter, etc.
573+
*
574+
* @return A {@link ListObjectsV2Result} instance that has the objects meet the
575+
* criteria
576+
*
577+
* @throws OSSException
578+
* @throws ClientException
579+
*/
580+
public ListObjectsV2Result listObjectsV2(ListObjectsV2Request listObjectsV2Request) throws OSSException, ClientException;
581+
582+
/**
583+
* Lists all objects under the specified {@link Bucket} in the parameter of
584+
* {@link ListObjectsRequest}
585+
*
586+
* @param bucketName
587+
* The bucket name.
588+
*
589+
* @return A {@link ListObjectsV2Result} instance that has the listing result.
590+
*
591+
* @throws OSSException
592+
* @throws ClientException
593+
*/
594+
public ListObjectsV2Result listObjectsV2(String bucketName) throws OSSException, ClientException;
595+
596+
/**
597+
* Lists all objects under the specified {@link Bucket} in the parameter of
598+
* {@link ListObjectsRequest}
599+
*
600+
* @param bucketName
601+
* bucket name.
602+
* @param prefix
603+
* The prefix restricting the objects listing.
604+
*
605+
* @return A {@link ListObjectsV2Result} instance that has the listing result.
606+
*
607+
* @throws OSSException
608+
* @throws ClientException
609+
*/
610+
public ListObjectsV2Result listObjectsV2(String bucketName, String prefix) throws OSSException, ClientException;
611+
612+
/**
613+
* Lists all objects under the specified {@link Bucket} in the parameter of
614+
* {@link ListObjectsRequest}
615+
*
616+
* @param bucketName
617+
* The bucket name.
618+
* @param prefix
619+
* The prefix restricting the objects listing.
620+
* @param continuationToken
621+
* The continuation token allows list to be continued from a specific point.
622+
* It values the last result {@link ListObjectsV2Result#getNextContinuationToken()}.
623+
* @param startAfter
624+
* Where you want oss to start the object listing from.
625+
* @param delimiter
626+
* The delimiter for condensing common prefixes in the returned listing results.
627+
* @param maxKeys
628+
* The maximum number of results to return.
629+
* @param encodingType
630+
* the encoding method to be applied on the response.
631+
* @param fetchOwner
632+
* Whether to get the owner filed in the response or not.
633+
*
634+
* @return A {@link ListObjectsV2Result} instance that has the listing result.
635+
*
636+
* @throws OSSException
637+
* @throws ClientException
638+
*/
639+
public ListObjectsV2Result listObjectsV2(String bucketName, String prefix, String continuationToken,
640+
String startAfter, String delimiter, Integer maxKeys, String encodingType, boolean fetchOwner)
641+
throws OSSException, ClientException;
642+
565643
/**
566644
* <p>
567645
* Returns a list of summary information about the versions in the specified

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,31 @@ public ObjectListing listObjects(String bucketName, String prefix) throws OSSExc
460460
public ObjectListing listObjects(ListObjectsRequest listObjectsRequest) throws OSSException, ClientException {
461461
return bucketOperation.listObjects(listObjectsRequest);
462462
}
463-
464-
@Override
463+
464+
@Override
465+
public ListObjectsV2Result listObjectsV2(ListObjectsV2Request listObjectsV2Request) throws OSSException, ClientException {
466+
return bucketOperation.listObjectsV2(listObjectsV2Request);
467+
}
468+
469+
@Override
470+
public ListObjectsV2Result listObjectsV2(String bucketName) throws OSSException, ClientException {
471+
return bucketOperation.listObjectsV2(new ListObjectsV2Request(bucketName));
472+
}
473+
474+
@Override
475+
public ListObjectsV2Result listObjectsV2(String bucketName, String prefix) throws OSSException, ClientException {
476+
return bucketOperation.listObjectsV2(new ListObjectsV2Request(bucketName, prefix));
477+
}
478+
479+
@Override
480+
public ListObjectsV2Result listObjectsV2(String bucketName, String prefix, String continuationToken,
481+
String startAfter, String delimiter, Integer maxKeys,
482+
String encodingType, boolean fetchOwner) throws OSSException, ClientException {
483+
return bucketOperation.listObjectsV2(new ListObjectsV2Request(bucketName, prefix, continuationToken, startAfter,
484+
delimiter, maxKeys, encodingType, fetchOwner));
485+
}
486+
487+
@Override
465488
public VersionListing listVersions(String bucketName, String prefix) throws OSSException, ClientException {
466489
return listVersions(new ListVersionsRequest(bucketName, prefix, null, null, null, null));
467490
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,9 @@ public interface OSSErrorCode {
276276
* The exsiting bucket without inventory.
277277
*/
278278
static final String NO_SUCH_INVENTORY = "NoSuchInventory";
279+
280+
/**
281+
* The part is not upload sequentially
282+
*/
283+
static final String PART_NOT_SEQUENTIAL = "PartNotSequential";
279284
}

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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import static com.aliyun.oss.internal.ResponseParsers.getBucketVersioningResponseParser;
6767
import static com.aliyun.oss.internal.ResponseParsers.listBucketResponseParser;
6868
import static com.aliyun.oss.internal.ResponseParsers.listObjectsReponseParser;
69+
import static com.aliyun.oss.internal.ResponseParsers.listObjectsV2ResponseParser;
6970
import static com.aliyun.oss.internal.ResponseParsers.listVersionsReponseParser;
7071
import static com.aliyun.oss.internal.ResponseParsers.getBucketImageResponseParser;
7172
import static com.aliyun.oss.internal.ResponseParsers.getImageStyleResponseParser;
@@ -127,6 +128,8 @@
127128
import com.aliyun.oss.model.GetBucketReplicationProgressRequest;
128129
import com.aliyun.oss.model.GetBucketRequestPaymentResult;
129130
import com.aliyun.oss.model.ImageProcess;
131+
import com.aliyun.oss.model.ListObjectsV2Request;
132+
import com.aliyun.oss.model.ListObjectsV2Result;
130133
import com.aliyun.oss.model.ReplicationRule;
131134
import com.aliyun.oss.model.ServerSideEncryptionConfiguration;
132135
import com.aliyun.oss.model.SetBucketEncryptionRequest;
@@ -459,7 +462,31 @@ public ObjectListing listObjects(ListObjectsRequest listObjectsRequest) throws O
459462

460463
return doOperation(request, listObjectsReponseParser, bucketName, null, true);
461464
}
462-
465+
466+
/**
467+
* List objects under the specified bucket.
468+
*/
469+
public ListObjectsV2Result listObjectsV2(ListObjectsV2Request listObjectsV2Request) throws OSSException, ClientException {
470+
471+
assertParameterNotNull(listObjectsV2Request, "listObjectsRequest");
472+
473+
String bucketName = listObjectsV2Request.getBucketName();
474+
assertParameterNotNull(bucketName, "bucketName");
475+
ensureBucketNameValid(bucketName);
476+
477+
Map<String, String> params = new LinkedHashMap<String, String>();
478+
populateListObjectsV2RequestParameters(listObjectsV2Request, params);
479+
480+
Map<String, String> headers = new HashMap<String, String>();
481+
populateRequestPayerHeader(headers, listObjectsV2Request.getRequestPayer());
482+
483+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
484+
.setMethod(HttpMethod.GET).setBucket(bucketName).setHeaders(headers).setParameters(params)
485+
.setOriginalRequest(listObjectsV2Request).build();
486+
487+
return doOperation(request, listObjectsV2ResponseParser, bucketName, null, true);
488+
}
489+
463490
/**
464491
* List versions under the specified bucket.
465492
*/
@@ -1771,6 +1798,42 @@ private static void populateListObjectsRequestParameters(ListObjectsRequest list
17711798
}
17721799
}
17731800

1801+
private static void populateListObjectsV2RequestParameters(ListObjectsV2Request listObjectsV2Request,
1802+
Map<String, String> params) {
1803+
1804+
params.put(LIST_TYPE, "2");
1805+
1806+
if (listObjectsV2Request.getPrefix() != null) {
1807+
params.put(PREFIX, listObjectsV2Request.getPrefix());
1808+
}
1809+
1810+
if (listObjectsV2Request.getDelimiter() != null) {
1811+
params.put(DELIMITER, listObjectsV2Request.getDelimiter());
1812+
}
1813+
1814+
if (listObjectsV2Request.getMaxKeys() != null) {
1815+
params.put(MAX_KEYS, Integer.toString(listObjectsV2Request.getMaxKeys()));
1816+
}
1817+
1818+
if (listObjectsV2Request.getEncodingType() != null) {
1819+
params.put(ENCODING_TYPE, listObjectsV2Request.getEncodingType());
1820+
}
1821+
1822+
if (listObjectsV2Request.getStartAfter() != null) {
1823+
params.put(START_AFTER, listObjectsV2Request.getStartAfter());
1824+
}
1825+
1826+
if (listObjectsV2Request.isFetchOwner()) {
1827+
params.put(FETCH_OWNER, Boolean.toString(listObjectsV2Request.isFetchOwner()));
1828+
}
1829+
1830+
if (listObjectsV2Request.getContinuationToken() != null) {
1831+
params.put(SUBRESOURCE_CONTINUATION_TOKEN, listObjectsV2Request.getContinuationToken());
1832+
}
1833+
1834+
}
1835+
1836+
17741837
private static void populateListVersionsRequestParameters(ListVersionsRequest listVersionsRequest,
17751838
Map<String, String> params) {
17761839

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,9 @@ public final class RequestParameters {
129129

130130
public static final String VPCIP = "vpcip";
131131
public static final String VIP = "vip";
132+
133+
/* listObjectsV2 params */
134+
public static final String LIST_TYPE = "list-type";
135+
public static final String START_AFTER = "start-after";
136+
public static final String FETCH_OWNER = "fetch-owner";
132137
}

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

Lines changed: 114 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.aliyun.oss.model.InventoryOSSBucketDestination;
4646
import com.aliyun.oss.model.InventorySchedule;
4747
import com.aliyun.oss.model.InventoryServerSideEncryptionKMS;
48+
import com.aliyun.oss.model.ListObjectsV2Result;
4849
import org.jdom.Document;
4950
import org.jdom.Element;
5051
import org.jdom.input.JDOMParseException;
@@ -183,6 +184,7 @@ public final class ResponseParsers {
183184
public static final GetBucketInventoryConfigurationParser getBucketInventoryConfigurationParser = new GetBucketInventoryConfigurationParser();
184185
public static final ListBucketInventoryConfigurationsParser listBucketInventoryConfigurationsParser = new ListBucketInventoryConfigurationsParser();
185186
public static final ListObjectsReponseParser listObjectsReponseParser = new ListObjectsReponseParser();
187+
public static final ListObjectsV2ResponseParser listObjectsV2ResponseParser = new ListObjectsV2ResponseParser();
186188
public static final ListVersionsReponseParser listVersionsReponseParser = new ListVersionsReponseParser();
187189
public static final PutObjectReponseParser putObjectReponseParser = new PutObjectReponseParser();
188190
public static final PutObjectProcessReponseParser putObjectProcessReponseParser = new PutObjectProcessReponseParser();
@@ -747,6 +749,21 @@ public ObjectListing parse(ResponseMessage response) throws ResponseParseExcepti
747749
}
748750

749751
}
752+
753+
public static final class ListObjectsV2ResponseParser implements ResponseParser<ListObjectsV2Result> {
754+
755+
@Override
756+
public ListObjectsV2Result parse(ResponseMessage response) throws ResponseParseException {
757+
try {
758+
ListObjectsV2Result result = parseListObjectsV2(response.getContent());
759+
result.setRequestId(response.getRequestId());
760+
return result;
761+
} finally {
762+
safeCloseResponse(response);
763+
}
764+
}
765+
766+
}
750767

751768
public static final class ListVersionsReponseParser implements ResponseParser<VersionListing> {
752769

@@ -1194,6 +1211,10 @@ public static ObjectListing parseListObjects(InputStream responseBody) throws Re
11941211
ossObjectSummary.setStorageClass(elem.getChildText("StorageClass"));
11951212
ossObjectSummary.setBucketName(objectListing.getBucketName());
11961213

1214+
if (elem.getChild("Type") != null) {
1215+
ossObjectSummary.setType(elem.getChildText("Type"));
1216+
}
1217+
11971218
String id = elem.getChild("Owner").getChildText("ID");
11981219
String displayName = elem.getChild("Owner").getChildText("DisplayName");
11991220
ossObjectSummary.setOwner(new Owner(id, displayName));
@@ -1217,6 +1238,93 @@ public static ObjectListing parseListObjects(InputStream responseBody) throws Re
12171238
}
12181239

12191240
}
1241+
1242+
/**
1243+
* Unmarshall list objects response body to ListObjectsV2Result.
1244+
*/
1245+
@SuppressWarnings("unchecked")
1246+
public static ListObjectsV2Result parseListObjectsV2(InputStream responseBody) throws ResponseParseException {
1247+
1248+
try {
1249+
Element root = getXmlRootElement(responseBody);
1250+
1251+
ListObjectsV2Result result = new ListObjectsV2Result();
1252+
result.setBucketName(root.getChildText("Name"));
1253+
result.setMaxKeys(Integer.valueOf(root.getChildText("MaxKeys")));
1254+
result.setTruncated(Boolean.valueOf(root.getChildText("IsTruncated")));
1255+
result.setKeyCount(Integer.valueOf(root.getChildText("KeyCount")));
1256+
1257+
if (root.getChild("Prefix") != null) {
1258+
String prefix = root.getChildText("Prefix");
1259+
result.setPrefix(isNullOrEmpty(prefix) ? null : prefix);
1260+
}
1261+
1262+
if (root.getChild("Delimiter") != null) {
1263+
String delimiter = root.getChildText("Delimiter");
1264+
result.setDelimiter(isNullOrEmpty(delimiter) ? null : delimiter);
1265+
}
1266+
1267+
if (root.getChild("ContinuationToken") != null) {
1268+
String continuationToken = root.getChildText("ContinuationToken");
1269+
result.setContinuationToken(isNullOrEmpty(continuationToken) ? null : continuationToken);
1270+
}
1271+
1272+
if (root.getChild("NextContinuationToken") != null) {
1273+
String nextContinuationToken = root.getChildText("NextContinuationToken");
1274+
result.setNextContinuationToken(isNullOrEmpty(nextContinuationToken) ? null : nextContinuationToken);
1275+
}
1276+
1277+
if (root.getChild("EncodingType") != null) {
1278+
String encodeType = root.getChildText("EncodingType");
1279+
result.setEncodingType(isNullOrEmpty(encodeType) ? null : encodeType);
1280+
}
1281+
1282+
if (root.getChild("StartAfter") != null) {
1283+
String startAfter = root.getChildText("StartAfter");
1284+
result.setStartAfter(isNullOrEmpty(startAfter) ? null : startAfter);
1285+
}
1286+
1287+
List<Element> objectSummaryElems = root.getChildren("Contents");
1288+
for (Element elem : objectSummaryElems) {
1289+
OSSObjectSummary ossObjectSummary = new OSSObjectSummary();
1290+
1291+
ossObjectSummary.setKey(elem.getChildText("Key"));
1292+
ossObjectSummary.setETag(trimQuotes(elem.getChildText("ETag")));
1293+
ossObjectSummary.setLastModified(DateUtil.parseIso8601Date(elem.getChildText("LastModified")));
1294+
ossObjectSummary.setSize(Long.valueOf(elem.getChildText("Size")));
1295+
ossObjectSummary.setStorageClass(elem.getChildText("StorageClass"));
1296+
ossObjectSummary.setBucketName(result.getBucketName());
1297+
1298+
if (elem.getChild("Type") != null) {
1299+
ossObjectSummary.setType(elem.getChildText("Type"));
1300+
}
1301+
1302+
if (elem.getChild("Owner") != null) {
1303+
String id = elem.getChild("Owner").getChildText("ID");
1304+
String displayName = elem.getChild("Owner").getChildText("DisplayName");
1305+
ossObjectSummary.setOwner(new Owner(id, displayName));
1306+
}
1307+
1308+
result.addObjectSummary(ossObjectSummary);
1309+
}
1310+
1311+
List<Element> commonPrefixesElems = root.getChildren("CommonPrefixes");
1312+
1313+
for (Element elem : commonPrefixesElems) {
1314+
String prefix = elem.getChildText("Prefix");
1315+
if (!isNullOrEmpty(prefix)) {
1316+
result.addCommonPrefix(prefix);
1317+
}
1318+
}
1319+
1320+
return result;
1321+
} catch (JDOMParseException e) {
1322+
throw new ResponseParseException(e.getPartialDocument() + ": " + e.getMessage(), e);
1323+
} catch (Exception e) {
1324+
throw new ResponseParseException(e.getMessage(), e);
1325+
}
1326+
1327+
}
12201328

12211329
/**
12221330
* Unmarshall list objects response body to object listing.
@@ -2392,12 +2500,14 @@ public static BucketInfo parseGetBucketInfo(InputStream responseBody) throws Res
23922500
bucketInfo.setBucket(bucket);
23932501

23942502
// comment
2395-
String comment = bucketElem.getChildText("Comment");
2396-
bucketInfo.setComment(comment);
2503+
if (bucketElem.getChild("Comment") != null) {
2504+
String comment = bucketElem.getChildText("Comment");
2505+
bucketInfo.setComment(comment);
2506+
}
23972507

23982508
// data redundancy type
2399-
String dataRedundancyString = bucketElem.getChildText("DataRedundancyType");
2400-
if (dataRedundancyString != null) {
2509+
if (bucketElem.getChild("DataRedundancyType") != null) {
2510+
String dataRedundancyString = bucketElem.getChildText("DataRedundancyType");
24012511
DataRedundancyType dataRedundancyType = DataRedundancyType.parse(dataRedundancyString);
24022512
bucketInfo.setDataRedundancyType(dataRedundancyType);
24032513
}

0 commit comments

Comments
 (0)