|
| 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 | +} |
0 commit comments