Skip to content

Commit

Permalink
Add overwrite flag to downloadObject() API. (#1321)
Browse files Browse the repository at this point in the history
When setting overwrite flag to true for DownloadObjectArgs makes
overwriting destination file if it exists.

Signed-off-by: Bala.FA <[email protected]>
  • Loading branch information
balamurugana authored Apr 21, 2022
1 parent fa596e3 commit 9eb1b0f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
25 changes: 15 additions & 10 deletions api/src/main/java/io/minio/DownloadObjectArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,40 @@

package io.minio;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;

/** Argument class of {@link MinioClient#downloadObject}. */
public class DownloadObjectArgs extends ObjectReadArgs {
private String filename;
private boolean overwrite;

public String filename() {
return filename;
}

public boolean overwrite() {
return overwrite;
}

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

/** Argument class of {@link DownloadObjectArgs}. */
public static final class Builder extends ObjectReadArgs.Builder<Builder, DownloadObjectArgs> {
private void validateFilename(String filename) {
validateNotEmptyString(filename, "filename");
}

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

private void validateFilename(String filename) {
validateNotEmptyString(filename, "filename");

if (Files.exists(Paths.get(filename))) {
throw new IllegalArgumentException("Destination file " + filename + " already exists");
}
public Builder overwrite(boolean flag) {
operations.add(args -> args.overwrite = flag);
return this;
}
}

Expand All @@ -55,11 +59,12 @@ public boolean equals(Object o) {
if (!(o instanceof DownloadObjectArgs)) return false;
if (!super.equals(o)) return false;
DownloadObjectArgs that = (DownloadObjectArgs) o;
return Objects.equals(filename, that.filename);
if (!Objects.equals(filename, that.filename)) return false;
return overwrite == that.overwrite;
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), filename);
return Objects.hash(super.hashCode(), filename, overwrite);
}
}
9 changes: 7 additions & 2 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public void downloadObject(DownloadObjectArgs args)
ServerException, XmlParserException {
String filename = args.filename();
Path filePath = Paths.get(filename);
if (Files.exists(filePath)) {
if (!args.overwrite() && Files.exists(filePath)) {
throw new IllegalArgumentException("Destination file " + filename + " already exists");
}

Expand All @@ -314,7 +314,12 @@ public void downloadObject(DownloadObjectArgs args)
+ ", written = "
+ bytesWritten);
}
Files.move(tempFilePath, filePath, StandardCopyOption.REPLACE_EXISTING);

if (!args.overwrite()) {
Files.move(tempFilePath, filePath);
} else {
Files.move(tempFilePath, filePath, StandardCopyOption.REPLACE_EXISTING);
}
} finally {
if (is != null) {
is.close();
Expand Down

0 comments on commit 9eb1b0f

Please sign in to comment.