Skip to content

Commit 832d0e4

Browse files
examples: Implement console progress bar for PutObject (#550)
Progress bar is of the following form ``` Uploading... 2% [=> ] 34594816/1513308160 (0:00:10 / 0:07:16) ```
1 parent f562ebc commit 832d0e4

File tree

5 files changed

+252
-99
lines changed

5 files changed

+252
-99
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ project(':api') {
225225

226226
project(':examples') {
227227
dependencies {
228+
compile "me.tongfei:progressbar:0.5.3"
228229
compile project(':api')
229230
}
230231

examples/ProgressStream.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
20+
import me.tongfei.progressbar.ProgressBar;
21+
import me.tongfei.progressbar.ProgressBarStyle;
22+
23+
public class ProgressStream extends InputStream {
24+
private InputStream in;
25+
private ProgressBar pb;
26+
27+
/**
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
31+
* input.
32+
* @param stream InputStream to be wrapped.
33+
* @throws IOException For any exception generated by the InputStream.
34+
*/
35+
public ProgressStream(String msg, ProgressBarStyle style, InputStream stream) throws IOException {
36+
super();
37+
38+
// Allocate the reader.
39+
this.in = stream;
40+
41+
// Initialize progress bar.
42+
this.pb = new ProgressBar(msg, stream.available(), style);
43+
this.pb.start();
44+
45+
}
46+
47+
@Override
48+
public int available() throws IOException {
49+
return this.in.available();
50+
}
51+
52+
@Override
53+
public void close() throws IOException {
54+
this.pb.stop();
55+
this.in.close();
56+
return;
57+
}
58+
59+
@Override
60+
public int read() throws IOException {
61+
this.pb.step();
62+
return this.in.read();
63+
}
64+
65+
@Override
66+
public int read(byte[] toStore) throws IOException {
67+
this.pb.stepBy(toStore.length); // Update progress bar.
68+
return this.in.read(toStore);
69+
}
70+
71+
@Override
72+
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);
75+
}
76+
77+
@Override
78+
public long skip(long n) throws IOException {
79+
this.pb.stepTo(n);
80+
return this.in.skip(n);
81+
}
82+
83+
@Override
84+
public boolean markSupported() {
85+
return false;
86+
}
87+
}

examples/PutObjectProgressBar.java

Lines changed: 18 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
import java.awt.event.ActionEvent;
18-
import java.awt.event.ActionListener;
1917
import java.io.BufferedInputStream;
2018
import java.io.File;
2119
import java.io.FileInputStream;
22-
import java.io.FileNotFoundException;
2320
import java.io.IOException;
21+
import java.io.InputStream;
2422
import java.security.InvalidKeyException;
2523
import java.security.NoSuchAlgorithmException;
2624

27-
import javax.swing.JButton;
28-
import javax.swing.JFrame;
29-
import javax.swing.ProgressMonitorInputStream;
30-
import javax.swing.SwingWorker;
31-
3225
import org.xmlpull.v1.XmlPullParserException;
3326

3427
import io.minio.MinioClient;
@@ -40,107 +33,34 @@
4033
import io.minio.errors.InvalidEndpointException;
4134
import io.minio.errors.InvalidPortException;
4235
import io.minio.errors.NoResponseException;
36+
import me.tongfei.progressbar.ProgressBarStyle;
4337

4438

45-
public class PutObjectProgressBar extends JFrame {
46-
47-
private static final long serialVersionUID = 1L;
48-
private static final String defaultFileName = "/etc/issue";
49-
private JButton button;
50-
51-
PutObjectProgressBar() {
52-
button = new JButton("Click here to upload !");
53-
ButtonActionListener bal = new ButtonActionListener();
54-
button.addActionListener(bal);
55-
56-
this.getContentPane().add(button);
57-
}
58-
39+
public class PutObjectProgressBar {
5940
/**
60-
* go() implements a blocking UI frame.
61-
*/
62-
public void go() {
63-
64-
this.setLocationRelativeTo(null);
65-
this.setVisible(true);
66-
this.pack();
67-
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
68-
}
69-
70-
/**
71-
* uploadFile(fileName) uploads to configured object storage upon reading
72-
* a local file, while asynchronously updating the progress bar UI
73-
* as well. This function is involed when user clicks on the UI
41+
* MinioClient.putObjectProgressBar() example.
7442
*/
75-
private void uploadFile(String fileName) throws IOException, NoSuchAlgorithmException, InvalidKeyException,
76-
XmlPullParserException, InvalidEndpointException, InvalidPortException,
77-
InvalidBucketNameException, InsufficientDataException, NoResponseException,
78-
ErrorResponseException, InternalException, InvalidArgumentException {
79-
43+
public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException,
44+
InvalidEndpointException, InvalidPortException, InvalidBucketNameException,
45+
InsufficientDataException, NoResponseException, ErrorResponseException, InternalException,
46+
InvalidArgumentException, IOException, XmlPullParserException {
8047
/* play.minio.io for test and development. */
8148
MinioClient minioClient = new MinioClient("https://play.minio.io:9000", "Q3AM3UQ867SPQQA43P2F",
82-
"zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
49+
"zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
8350
/* Amazon S3: */
8451
// MinioClient minioClient = new MinioClient("https://s3.amazonaws.com",
8552
// "YOUR-ACCESSKEYID",
8653
// "YOUR-SECRETACCESSKEY");
8754

88-
File file = new File(fileName);
89-
BufferedInputStream bis;
90-
try {
91-
bis = new BufferedInputStream(new FileInputStream(file));
92-
ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(
93-
this,
94-
"Uploading... " + file.getAbsolutePath(),
95-
bis);
96-
97-
pmis.getProgressMonitor().setMillisToPopup(10);
98-
minioClient.putObject("bank", "my-objectname", pmis, bis.available(), "application/octet-stream");
99-
pmis.close();
100-
System.out.println("my-objectname is uploaded successfully");
101-
} catch (FileNotFoundException e) {
102-
// TODO Auto-generated catch block
103-
e.printStackTrace();
104-
} catch (IOException e) {
105-
// TODO Auto-generated catch block
106-
e.printStackTrace();
107-
}
108-
}
55+
String objectName = "my-objectname";
56+
String bucketName = "my-bucketname";
10957

110-
/**
111-
* Internal class extends button listener, adds methods to initiate upload operation.
112-
*/
113-
private class ButtonActionListener implements ActionListener {
114-
115-
@Override
116-
public void actionPerformed(ActionEvent e) {
117-
118-
button.setEnabled(false);
119-
SwingWorker<?, ?> worker = new SwingWorker<Object, Object>() {
120-
121-
@Override
122-
protected Object doInBackground() throws Exception {
123-
uploadFile(defaultFileName);
124-
return null;
125-
}
126-
127-
@Override
128-
protected void done() {
129-
button.setEnabled(true);
130-
}
131-
132-
};
133-
134-
worker.execute();
135-
}
136-
137-
}
138-
139-
/**
140-
* MinioClient.putObjectProgressBar() example.
141-
*/
142-
public static void main(String[] args) {
143-
PutObjectProgressBar demo = new PutObjectProgressBar();
144-
demo.go();
58+
File file = new File("my-filename");
59+
InputStream pis = new BufferedInputStream(new ProgressStream("Uploading... ",
60+
ProgressBarStyle.ASCII,
61+
new FileInputStream(file)));
62+
minioClient.putObject(bucketName, objectName, pis, pis.available(), "application/octet-stream");
63+
pis.close();
64+
System.out.println("my-objectname is uploaded successfully");
14565
}
14666
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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.awt.event.ActionEvent;
18+
import java.awt.event.ActionListener;
19+
import java.io.BufferedInputStream;
20+
import java.io.File;
21+
import java.io.FileInputStream;
22+
import java.io.FileNotFoundException;
23+
import java.io.IOException;
24+
import java.security.InvalidKeyException;
25+
import java.security.NoSuchAlgorithmException;
26+
27+
import javax.swing.JButton;
28+
import javax.swing.JFrame;
29+
import javax.swing.ProgressMonitorInputStream;
30+
import javax.swing.SwingWorker;
31+
32+
import org.xmlpull.v1.XmlPullParserException;
33+
34+
import io.minio.MinioClient;
35+
import io.minio.errors.ErrorResponseException;
36+
import io.minio.errors.InsufficientDataException;
37+
import io.minio.errors.InternalException;
38+
import io.minio.errors.InvalidArgumentException;
39+
import io.minio.errors.InvalidBucketNameException;
40+
import io.minio.errors.InvalidEndpointException;
41+
import io.minio.errors.InvalidPortException;
42+
import io.minio.errors.NoResponseException;
43+
44+
45+
public class PutObjectUiProgressBar extends JFrame {
46+
47+
private static final long serialVersionUID = 1L;
48+
private static final String defaultFileName = "/etc/issue";
49+
private JButton button;
50+
51+
PutObjectUiProgressBar() {
52+
button = new JButton("Click here to upload !");
53+
ButtonActionListener bal = new ButtonActionListener();
54+
button.addActionListener(bal);
55+
56+
this.getContentPane().add(button);
57+
}
58+
59+
/**
60+
* go() implements a blocking UI frame.
61+
*/
62+
public void go() {
63+
64+
this.setLocationRelativeTo(null);
65+
this.setVisible(true);
66+
this.pack();
67+
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
68+
}
69+
70+
/**
71+
* uploadFile(fileName) uploads to configured object storage upon reading
72+
* a local file, while asynchronously updating the progress bar UI
73+
* as well. This function is involed when user clicks on the UI
74+
*/
75+
private void uploadFile(String fileName) throws IOException, NoSuchAlgorithmException, InvalidKeyException,
76+
XmlPullParserException, InvalidEndpointException, InvalidPortException,
77+
InvalidBucketNameException, InsufficientDataException, NoResponseException,
78+
ErrorResponseException, InternalException, InvalidArgumentException {
79+
80+
/* play.minio.io for test and development. */
81+
MinioClient minioClient = new MinioClient("https://play.minio.io:9000", "Q3AM3UQ867SPQQA43P2F",
82+
"zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
83+
/* Amazon S3: */
84+
// MinioClient minioClient = new MinioClient("https://s3.amazonaws.com",
85+
// "YOUR-ACCESSKEYID",
86+
// "YOUR-SECRETACCESSKEY");
87+
88+
File file = new File(fileName);
89+
BufferedInputStream bis;
90+
try {
91+
bis = new BufferedInputStream(new FileInputStream(file));
92+
ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(
93+
this,
94+
"Uploading... " + file.getAbsolutePath(),
95+
bis);
96+
97+
pmis.getProgressMonitor().setMillisToPopup(10);
98+
minioClient.putObject("bank", "my-objectname", pmis, bis.available(), "application/octet-stream");
99+
pmis.close();
100+
System.out.println("my-objectname is uploaded successfully");
101+
} catch (FileNotFoundException e) {
102+
// TODO Auto-generated catch block
103+
e.printStackTrace();
104+
} catch (IOException e) {
105+
// TODO Auto-generated catch block
106+
e.printStackTrace();
107+
}
108+
}
109+
110+
/**
111+
* Internal class extends button listener, adds methods to initiate upload operation.
112+
*/
113+
private class ButtonActionListener implements ActionListener {
114+
115+
@Override
116+
public void actionPerformed(ActionEvent e) {
117+
118+
button.setEnabled(false);
119+
SwingWorker<?, ?> worker = new SwingWorker<Object, Object>() {
120+
121+
@Override
122+
protected Object doInBackground() throws Exception {
123+
uploadFile(defaultFileName);
124+
return null;
125+
}
126+
127+
@Override
128+
protected void done() {
129+
button.setEnabled(true);
130+
}
131+
132+
};
133+
134+
worker.execute();
135+
}
136+
137+
}
138+
139+
/**
140+
* MinioClient.putObjectProgressBar() example.
141+
*/
142+
public static void main(String[] args) {
143+
PutObjectUiProgressBar demo = new PutObjectUiProgressBar();
144+
demo.go();
145+
}
146+
}

functional/FunctionalTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.math.BigInteger;
2020
import java.util.*;
2121
import java.io.*;
22-
import java.lang.*;
2322

2423
import static java.nio.file.StandardOpenOption.*;
2524
import java.nio.file.*;

0 commit comments

Comments
 (0)