|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -import java.awt.event.ActionEvent; |
18 | | -import java.awt.event.ActionListener; |
19 | 17 | import java.io.BufferedInputStream; |
20 | 18 | import java.io.File; |
21 | 19 | import java.io.FileInputStream; |
22 | | -import java.io.FileNotFoundException; |
23 | 20 | import java.io.IOException; |
| 21 | +import java.io.InputStream; |
24 | 22 | import java.security.InvalidKeyException; |
25 | 23 | import java.security.NoSuchAlgorithmException; |
26 | 24 |
|
27 | | -import javax.swing.JButton; |
28 | | -import javax.swing.JFrame; |
29 | | -import javax.swing.ProgressMonitorInputStream; |
30 | | -import javax.swing.SwingWorker; |
31 | | - |
32 | 25 | import org.xmlpull.v1.XmlPullParserException; |
33 | 26 |
|
34 | 27 | import io.minio.MinioClient; |
|
40 | 33 | import io.minio.errors.InvalidEndpointException; |
41 | 34 | import io.minio.errors.InvalidPortException; |
42 | 35 | import io.minio.errors.NoResponseException; |
| 36 | +import me.tongfei.progressbar.ProgressBarStyle; |
43 | 37 |
|
44 | 38 |
|
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 { |
59 | 40 | /** |
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. |
74 | 42 | */ |
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 { |
80 | 47 | /* play.minio.io for test and development. */ |
81 | 48 | MinioClient minioClient = new MinioClient("https://play.minio.io:9000", "Q3AM3UQ867SPQQA43P2F", |
82 | | - "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"); |
| 49 | + "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"); |
83 | 50 | /* Amazon S3: */ |
84 | 51 | // MinioClient minioClient = new MinioClient("https://s3.amazonaws.com", |
85 | 52 | // "YOUR-ACCESSKEYID", |
86 | 53 | // "YOUR-SECRETACCESSKEY"); |
87 | 54 |
|
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"; |
109 | 57 |
|
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"); |
145 | 65 | } |
146 | 66 | } |
0 commit comments