Skip to content

Commit 18608e1

Browse files
authored
Add part size support in UploadObject() api. (#1208)
Signed-off-by: Bala.FA <bala.gluster@gmail.com>
1 parent 1dfb1f0 commit 18608e1

File tree

3 files changed

+62
-73
lines changed

3 files changed

+62
-73
lines changed

api/src/main/java/io/minio/PutObjectArgs.java

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -50,65 +50,6 @@ protected void validate(PutObjectArgs args) {
5050
validateNotNull(args.stream, "stream");
5151
}
5252

53-
private void validateSizes(long objectSize, long partSize) {
54-
if (partSize > 0) {
55-
if (partSize < MIN_MULTIPART_SIZE) {
56-
throw new IllegalArgumentException(
57-
"part size " + partSize + " is not supported; minimum allowed 5MiB");
58-
}
59-
60-
if (partSize > MAX_PART_SIZE) {
61-
throw new IllegalArgumentException(
62-
"part size " + partSize + " is not supported; maximum allowed 5GiB");
63-
}
64-
}
65-
66-
if (objectSize >= 0) {
67-
if (objectSize > MAX_OBJECT_SIZE) {
68-
throw new IllegalArgumentException(
69-
"object size " + objectSize + " is not supported; maximum allowed 5TiB");
70-
}
71-
} else if (partSize <= 0) {
72-
throw new IllegalArgumentException(
73-
"valid part size must be provided when object size is unknown");
74-
}
75-
}
76-
77-
private void validatePartCount(int partCount, long objectSize, long partSize) {
78-
if (partCount > MAX_MULTIPART_COUNT) {
79-
throw new IllegalArgumentException(
80-
"object size "
81-
+ objectSize
82-
+ " and part size "
83-
+ partSize
84-
+ " make more than "
85-
+ MAX_MULTIPART_COUNT
86-
+ "parts for upload");
87-
}
88-
}
89-
90-
private long[] partInfo(long objectSize, long partSize) {
91-
if (objectSize < 0) {
92-
return new long[] {partSize, -1};
93-
}
94-
95-
if (partSize > 0) {
96-
if (partSize > objectSize) partSize = objectSize;
97-
long partCount = partSize > 0 ? (long) Math.ceil((double) objectSize / partSize) : 1;
98-
return new long[] {partSize, partCount == 0 ? 1 : partCount};
99-
}
100-
101-
double pSize = Math.ceil((double) objectSize / MAX_MULTIPART_COUNT);
102-
pSize = Math.ceil(pSize / MIN_MULTIPART_SIZE) * MIN_MULTIPART_SIZE;
103-
partSize = (long) pSize;
104-
long partCount = 1;
105-
if (pSize > 0) {
106-
partCount = (long) Math.ceil(objectSize / pSize);
107-
}
108-
109-
return new long[] {partSize, partCount};
110-
}
111-
11253
/**
11354
* Sets stream to upload. Two ways to provide object/part sizes.
11455
*
@@ -123,12 +64,10 @@ private long[] partInfo(long objectSize, long partSize) {
12364
*/
12465
public Builder stream(InputStream stream, long objectSize, long partSize) {
12566
validateNotNull(stream, "stream");
126-
validateSizes(objectSize, partSize);
12767

128-
long[] partinfo = partInfo(objectSize, partSize);
68+
long[] partinfo = getPartInfo(objectSize, partSize);
12969
long pSize = partinfo[0];
13070
int pCount = (int) partinfo[1];
131-
validatePartCount(pCount, objectSize, partSize);
13271

13372
final BufferedInputStream bis =
13473
(stream instanceof BufferedInputStream)

api/src/main/java/io/minio/PutObjectBaseArgs.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,58 @@ public boolean preloadData() {
6060
@SuppressWarnings("unchecked") // Its safe to type cast to B as B is inherited by this class
6161
public abstract static class Builder<B extends Builder<B, A>, A extends PutObjectBaseArgs>
6262
extends ObjectWriteArgs.Builder<B, A> {
63+
private void validateSizes(long objectSize, long partSize) {
64+
if (partSize > 0) {
65+
if (partSize < MIN_MULTIPART_SIZE) {
66+
throw new IllegalArgumentException(
67+
"part size " + partSize + " is not supported; minimum allowed 5MiB");
68+
}
69+
70+
if (partSize > MAX_PART_SIZE) {
71+
throw new IllegalArgumentException(
72+
"part size " + partSize + " is not supported; maximum allowed 5GiB");
73+
}
74+
}
75+
76+
if (objectSize >= 0) {
77+
if (objectSize > MAX_OBJECT_SIZE) {
78+
throw new IllegalArgumentException(
79+
"object size " + objectSize + " is not supported; maximum allowed 5TiB");
80+
}
81+
} else if (partSize <= 0) {
82+
throw new IllegalArgumentException(
83+
"valid part size must be provided when object size is unknown");
84+
}
85+
}
86+
87+
protected long[] getPartInfo(long objectSize, long partSize) {
88+
validateSizes(objectSize, partSize);
89+
90+
if (objectSize < 0) return new long[] {partSize, -1};
91+
92+
if (partSize <= 0) {
93+
// Calculate part size by multiple of MIN_MULTIPART_SIZE.
94+
double dPartSize = Math.ceil((double) objectSize / MAX_MULTIPART_COUNT);
95+
dPartSize = Math.ceil(dPartSize / MIN_MULTIPART_SIZE) * MIN_MULTIPART_SIZE;
96+
partSize = (long) dPartSize;
97+
}
98+
99+
if (partSize > objectSize) partSize = objectSize;
100+
long partCount = partSize > 0 ? (long) Math.ceil((double) objectSize / partSize) : 1;
101+
if (partCount > MAX_MULTIPART_COUNT) {
102+
throw new IllegalArgumentException(
103+
"object size "
104+
+ objectSize
105+
+ " and part size "
106+
+ partSize
107+
+ " make more than "
108+
+ MAX_MULTIPART_COUNT
109+
+ "parts for upload");
110+
}
111+
112+
return new long[] {partSize, partCount};
113+
}
114+
63115
/**
64116
* Sets flag to control data preload of stream/file. When this flag is enabled, entire
65117
* part/object data is loaded into memory to enable connection retry on network failure in the

api/src/main/java/io/minio/UploadObjectArgs.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,25 @@ private void validateFilename(String filename) {
6464
}
6565
}
6666

67-
public Builder filename(String filename) throws IOException {
67+
public Builder filename(String filename, long partSize) throws IOException {
6868
validateFilename(filename);
6969
final long objectSize = Files.size(Paths.get(filename));
70-
if (objectSize > MAX_OBJECT_SIZE) {
71-
throw new IllegalArgumentException(
72-
"object size " + objectSize + " is not supported; maximum allowed 5TiB");
73-
}
74-
75-
double pSize = Math.ceil((double) objectSize / MAX_MULTIPART_COUNT);
76-
pSize = Math.ceil(pSize / MIN_MULTIPART_SIZE) * MIN_MULTIPART_SIZE;
7770

78-
final long partSize = (long) pSize;
79-
final int partCount = (pSize > 0) ? (int) Math.ceil(objectSize / pSize) : 1;
71+
long[] partinfo = getPartInfo(objectSize, partSize);
72+
final long pSize = partinfo[0];
73+
final int partCount = (int) partinfo[1];
8074

8175
operations.add(args -> args.filename = filename);
8276
operations.add(args -> args.objectSize = objectSize);
83-
operations.add(args -> args.partSize = partSize);
77+
operations.add(args -> args.partSize = pSize);
8478
operations.add(args -> args.partCount = partCount);
8579
return this;
8680
}
8781

82+
public Builder filename(String filename) throws IOException {
83+
return this.filename(filename, 0);
84+
}
85+
8886
public Builder contentType(String contentType) {
8987
validateNotEmptyString(contentType, "content type");
9088
operations.add(args -> args.contentType = contentType);

0 commit comments

Comments
 (0)