Skip to content

Commit

Permalink
Add arg builder to listIncompleteUpload API (minio#937)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinhaashish authored Jun 12, 2020
1 parent a912b64 commit 7a5cae1
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 113 deletions.
93 changes: 93 additions & 0 deletions api/src/main/java/io/minio/ListIncompleteUploadsArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2020 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.minio;

/** Argument class of @see #listIncompleteUploads(ListIncompleteUploadsArgs args). */
public class ListIncompleteUploadsArgs extends BucketArgs {
private String prefix;
private String delimiter;
private String keyMarker;
private Integer maxUploads;
private String uploadIdMarker;
private boolean recursive;

public String prefix() {
return prefix;
}

public String delimiter() {
return delimiter;
}

public String keyMarker() {
return keyMarker;
}

public Integer maxUploads() {
return maxUploads;
}

public String uploadIdMarker() {
return uploadIdMarker;
}

public boolean recursive() {
return recursive;
}

public static Builder builder() {
return new Builder();
}

/** Argument builder of @see #listIncompleteUploads(ListIncompleteUploadsArgs args). */
public static final class Builder extends BucketArgs.Builder<Builder, ListIncompleteUploadsArgs> {
public Builder prefix(String prefix) {
operations.add(args -> args.prefix = prefix);
return this;
}

public Builder delimitter(String delimiter) {
operations.add(args -> args.delimiter = delimiter);
return this;
}

public Builder keyMarker(String keyMarker) {
validateNullOrNotEmptyString(keyMarker, "keyMarker");
operations.add(args -> args.keyMarker = keyMarker);
return this;
}

public Builder maxUploads(int maxUploads) {
if (maxUploads < 1 || maxUploads > 1000) {
throw new IllegalArgumentException("maxUploads must be minimum 1 to maximum 1000");
}
operations.add(args -> args.maxUploads = maxUploads);
return this;
}

public Builder uploadIdMarker(String uploadIdMarker) {
validateNullOrNotEmptyString(uploadIdMarker, "uploadIdMarker");
operations.add(args -> args.uploadIdMarker = uploadIdMarker);
return this;
}

public Builder recursive(boolean recursive) {
operations.add(args -> args.recursive = recursive);
return this;
}
}
}
138 changes: 115 additions & 23 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5872,12 +5872,14 @@ public void deleteBucketNotification(DeleteBucketNotificationArgs args)
* }</pre>
*
* @param bucketName Name of the bucket.
* @return Iterable&ltResult&ltUpload&gt&gt - Lazy iterator contains object upload information.
* @see #listIncompleteUploads(String, String, boolean)
* @return Iterable&ltResult&ltUpload&gt;&gt; - Lazy iterator contains object upload information.
* @deprecated use {@link #listIncompleteUploads(ListIncompleteUploadsArgs)}
*/
@Deprecated
public Iterable<Result<Upload>> listIncompleteUploads(String bucketName)
throws XmlParserException {
return listIncompleteUploads(bucketName, null, true, true);
return listIncompleteUploads(
ListIncompleteUploadsArgs.builder().bucket(bucketName).recursive(true).build());
}

/**
Expand All @@ -5894,13 +5896,19 @@ public Iterable<Result<Upload>> listIncompleteUploads(String bucketName)
*
* @param bucketName Name of the bucket.
* @param prefix Object name starts with prefix.
* @return Iterable&ltResult&ltUpload&gt&gt - Lazy iterator contains object upload information.
* @return Iterable&ltResult&ltUpload&gt;&gt; - Lazy iterator contains object upload information.
* @throws XmlParserException upon parsing response xml
* @see #listIncompleteUploads(String, String, boolean)
* @deprecated use {@link #listIncompleteUploads(ListIncompleteUploadsArgs)}
*/
@Deprecated
public Iterable<Result<Upload>> listIncompleteUploads(String bucketName, String prefix)
throws XmlParserException {
return listIncompleteUploads(bucketName, prefix, true, true);
return listIncompleteUploads(
ListIncompleteUploadsArgs.builder()
.bucket(bucketName)
.prefix(prefix)
.recursive(true)
.build());
}

/**
Expand All @@ -5918,38 +5926,105 @@ public Iterable<Result<Upload>> listIncompleteUploads(String bucketName, String
* @param bucketName Name of the bucket.
* @param prefix Object name starts with prefix.
* @param recursive List recursively than directory structure emulation.
* @return Iterable&ltResult&ltUpload&gt&gt - Lazy iterator contains object upload information.
* @see #listIncompleteUploads(String bucketName)
* @see #listIncompleteUploads(String bucketName, String prefix)
* @return Iterable&ltResult&ltUpload&gt;&gt; - Lazy iterator contains object upload information.
* @deprecated use {@link #listIncompleteUploads(ListIncompleteUploadsArgs)}
*/
@Deprecated
public Iterable<Result<Upload>> listIncompleteUploads(
String bucketName, String prefix, boolean recursive) {
return listIncompleteUploads(bucketName, prefix, recursive, true);
return listIncompleteUploads(
ListIncompleteUploadsArgs.builder()
.bucket(bucketName)
.prefix(prefix)
.recursive(recursive)
.build());
}

/**
* Returns Iterable<Result<Upload>> of given bucket name, prefix and recursive flag. All parts
* size are aggregated when aggregatePartSize is true.
* Lists incomplete object upload information of a bucket for prefix recursively.
*
* <pre>Example:{@code
* // Lists incomplete object upload information of a bucket.
* Iterable<Result<Upload>> results =
* minioClient.listIncompleteUploads(
* ListIncompleteUploadsArgs.builder().bucket("my-bucketname").build());
* for (Result<Upload> result : results) {
* Upload upload = result.get();
* System.out.println(upload.uploadId() + ", " + upload.objectName());
* }
*
* // Lists incomplete object upload information of a bucket for prefix.
* Iterable<Result<Upload>> results =
* minioClient.listIncompleteUploads(
* ListIncompleteUploadsArgs.builder()
* .bucket("my-bucketname")
* .prefix("my-obj")
* .build());
* for (Result<Upload> result : results) {
* Upload upload = result.get();
* System.out.println(upload.uploadId() + ", " + upload.objectName());
* }
*
* // Lists incomplete object upload information of a bucket for prefix recursively.
* Iterable<Result<Upload>> results =
* minioClient.listIncompleteUploads(
* ListIncompleteUploadsArgs.builder()
* .bucket("my-bucketname")
* .prefix("my-obj")
* .recursive(true)
* .build());
* for (Result<Upload> result : results) {
* Upload upload = result.get();
* System.out.println(upload.uploadId() + ", " + upload.objectName());
* }
*
* // Lists incomplete object upload information of a bucket for prefix, delimiter.
* // keyMarker, uploadIdMarker and maxUpload to 500
* Iterable<Result<Upload>> results =
* minioClient.listIncompleteUploads(
* ListIncompleteUploadsArgs.builder()
* .bucket("my-bucketname")
* .prefix("my-obj")
* .delimiter("-")
* .keyMarker("b")
* .maxUploads(500)
* .uploadIdMarker("k")
* .build());
* for (Result<Upload> result : results) {
* Upload upload = result.get();
* System.out.println(upload.uploadId() + ", " + upload.objectName());
* }
* }</pre>
*
* @param args {@link ListIncompleteUploadsArgs} objects.
* @return Iterable&lt;Result&lt;Upload&gt;&gt; - Lazy iterator contains object upload
* information.
*/
public Iterable<Result<Upload>> listIncompleteUploads(ListIncompleteUploadsArgs args) {
checkArgs(args);
return this.listIncompleteUploads(args, true);
}

/**
* Returns Iterable<Result<Upload>> of given ListIncompleteUploadsArgs argumentsr. All parts size
* are aggregated when aggregatePartSize is true.
*/
private Iterable<Result<Upload>> listIncompleteUploads(
final String bucketName,
final String prefix,
final boolean recursive,
final boolean aggregatePartSize) {
ListIncompleteUploadsArgs args, final boolean aggregatePartSize) {
return new Iterable<Result<Upload>>() {
@Override
public Iterator<Result<Upload>> iterator() {
return new Iterator<Result<Upload>>() {
private String nextKeyMarker;
private String nextUploadIdMarker;
private String nextKeyMarker = args.keyMarker();
private String nextUploadIdMarker = args.uploadIdMarker();
private ListMultipartUploadsResult listMultipartUploadsResult;
private Result<Upload> error;
private Iterator<Upload> uploadIterator;
private boolean completed = false;

private synchronized void populate() {
String delimiter = "/";
if (recursive) {
String delimiter = args.delimiter();
if (args.recursive()) {
delimiter = null;
}

Expand All @@ -5959,7 +6034,12 @@ private synchronized void populate() {
try {
this.listMultipartUploadsResult =
listMultipartUploads(
bucketName, delimiter, nextKeyMarker, null, prefix, nextUploadIdMarker);
args.bucket(),
delimiter,
nextKeyMarker,
args.maxUploads(),
args.prefix(),
nextUploadIdMarker);
} catch (ErrorResponseException
| IllegalArgumentException
| InsufficientDataException
Expand Down Expand Up @@ -5988,7 +6068,7 @@ private synchronized long getAggregatedPartSize(String objectName, String upload
XmlParserException {
long aggregatedPartSize = 0;

for (Result<Part> result : listObjectParts(bucketName, objectName, uploadId)) {
for (Result<Part> result : listObjectParts(args.bucket(), objectName, uploadId)) {
aggregatedPartSize += result.get().partSize();
}

Expand Down Expand Up @@ -6264,7 +6344,16 @@ public void removeIncompleteUpload(RemoveIncompleteUploadArgs args)
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, ServerException,
XmlParserException {
for (Result<Upload> r : listIncompleteUploads(args.bucket(), args.object(), true, false)) {
for (Result<Upload> r :
listIncompleteUploads(
ListIncompleteUploadsArgs.builder()
.bucket(args.bucket())
.prefix(args.object())
.recursive(true)
.build(),
false)) {

// args.bucket(), args.object(), true, false)) {
Upload upload = r.get();
if (args.object().equals(upload.objectName())) {
abortMultipartUpload(args.bucket(), args.object(), upload.uploadId());
Expand Down Expand Up @@ -7495,6 +7584,9 @@ protected ListMultipartUploadsResult listMultipartUploads(
queryParamMap.put("upload-id-marker", uploadIdMarker);
}

// Setting it as default to encode the object keys in the response
queryParamMap.put("encoding-type", "url");

Response response = executeGet(bucketName, null, null, queryParamMap);

try (ResponseBody body = response.body()) {
Expand Down
Loading

0 comments on commit 7a5cae1

Please sign in to comment.