Skip to content

Commit e777ab7

Browse files
examples: Implement console progress bar for GetObject (#554)
Progress bar is of the following form ``` Uploading... 2% [=> ] ... ```
1 parent ba04d55 commit e777ab7

File tree

2 files changed

+106
-9
lines changed

2 files changed

+106
-9
lines changed

examples/GetObjectProgressBar.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Minio Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2017 Minio, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import java.io.IOException;
18+
import java.io.InputStream;
19+
import java.io.OutputStream;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
22+
import java.nio.file.Paths;
23+
import java.nio.file.StandardOpenOption;
24+
import java.security.InvalidKeyException;
25+
import java.security.NoSuchAlgorithmException;
26+
27+
import org.xmlpull.v1.XmlPullParserException;
28+
29+
import com.google.common.io.ByteStreams;
30+
31+
import io.minio.MinioClient;
32+
import io.minio.ObjectStat;
33+
import io.minio.errors.MinioException;
34+
import me.tongfei.progressbar.ProgressBarStyle;
35+
36+
public class GetObjectProgressBar {
37+
/**
38+
* MinioClient.getObjectProgressBar() example.
39+
*/
40+
public static void main(String[] args)
41+
throws IOException, NoSuchAlgorithmException, InvalidKeyException, XmlPullParserException {
42+
try {
43+
/* play.minio.io for test and development. */
44+
MinioClient minioClient = new MinioClient("https://play.minio.io:9000",
45+
"Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
46+
47+
/* Amazon S3: */
48+
// MinioClient minioClient = new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID",
49+
// "YOUR-SECRETACCESSKEY");
50+
51+
// Check whether the object exists using statObject(). If the object is not found,
52+
// statObject() throws an exception. It means that the object exists when statObject()
53+
// execution is successful.
54+
55+
// Get object stat information.
56+
ObjectStat objectStat = minioClient.statObject("testbucket", "resumes/4.original.pdf");
57+
58+
// Get input stream to have content of 'my-objectname' from 'my-bucketname'
59+
InputStream is = new ProgressStream("Downloading .. ", ProgressBarStyle.ASCII,
60+
objectStat.length(), minioClient.getObject("my-bucketname", "my-objectname"));
61+
62+
Path path = Paths.get("my-filename");
63+
OutputStream os = Files.newOutputStream(path, StandardOpenOption.CREATE);
64+
65+
long bytesWritten = ByteStreams.copy(is, os);
66+
is.close();
67+
os.close();
68+
69+
if (bytesWritten != objectStat.length()) {
70+
throw new IOException(path + ": unexpected data written. expected = " + objectStat.length()
71+
+ ", written = " + bytesWritten);
72+
}
73+
74+
} catch (MinioException e) {
75+
System.out.println("Error occurred: " + e);
76+
}
77+
}
78+
}

examples/ProgressStream.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,40 @@ public class ProgressStream extends InputStream {
2525
private ProgressBar pb;
2626

2727
/**
28-
* ProgressStream implements an extends InputStream while
29-
* also writing out the read progress on console. ProgressStream
30-
* can be used as a direct replacement for any InputStream compatible
28+
* ProgressStream implements an extends InputStream while also writing out the read progress on
29+
* console. ProgressStream can be used as a direct replacement for any InputStream compatible
3130
* input.
31+
*
32+
* @param msg Custom message string.
33+
* @param style Custom progress bar style.
3234
* @param stream InputStream to be wrapped.
3335
* @throws IOException For any exception generated by the InputStream.
3436
*/
3537
public ProgressStream(String msg, ProgressBarStyle style, InputStream stream) throws IOException {
38+
this(msg, style, (long)stream.available(), stream);
39+
}
40+
41+
/**
42+
* ProgressStream implements an extends InputStream while also writing out the read progress on
43+
* console. ProgressStream can be used as a direct replacement for any InputStream compatible
44+
* input.
45+
*
46+
* @param msg Custom message string.
47+
* @param style Custom progress bar style.
48+
* @param size Size of the progress bar.
49+
* @param stream InputStream to be wrapped.
50+
* @throws IOException For any exception generated by the InputStream.
51+
*/
52+
public ProgressStream(String msg, ProgressBarStyle style, long size, InputStream stream)
53+
throws IOException {
3654
super();
3755

3856
// Allocate the reader.
3957
this.in = stream;
4058

4159
// Initialize progress bar.
42-
this.pb = new ProgressBar(msg, stream.available(), style);
60+
this.pb = new ProgressBar(msg, size, style);
4361
this.pb.start();
44-
4562
}
4663

4764
@Override
@@ -64,14 +81,16 @@ public int read() throws IOException {
6481

6582
@Override
6683
public int read(byte[] toStore) throws IOException {
67-
this.pb.stepBy(toStore.length); // Update progress bar.
68-
return this.in.read(toStore);
84+
int readBytes = this.in.read(toStore);
85+
this.pb.stepBy(readBytes); // Update progress bar.
86+
return readBytes;
6987
}
7088

7189
@Override
7290
public int read(byte[] toStore, int off, int len) throws IOException {
73-
this.pb.stepBy(toStore.length);
74-
return this.in.read(toStore, off, len);
91+
int readBytes = this.in.read(toStore, off, len);
92+
this.pb.stepBy(readBytes);
93+
return readBytes;
7594
}
7695

7796
@Override

0 commit comments

Comments
 (0)