Minio As object storage, flexible and convenient, combined with Java implementation of Minio file upload
1. Build a MAVEN environment and add dependent packages
<properties>
<minio.version>4.0.0</minio.version>
</properties>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>${minio.version}</version>
</dependency>
2. Write a minioutils
public class Minioclientutils {
private static final Logger Logger = Loggerfactory.getlogger (Minioclientutils.class);
private static Minioclientutils minioclientutils;
Private Minioclient minioclient;
private static int retry_num = 3;
private static final String Bucketpublicpolicy = "{\" version\ ": \" 2012-10-17\ ", \" statement\ ": [{\" action\ ": [\" S3: Getbucketlocation\ ", \" S3:listbucket\ ", \" S3:listbucketmultipartuploads\ "],\" effect\ ": \" allow\ ", \" Principal\ ": {\ "Aws\": [\ "*\"]},\ "resource\": [\ "arn:aws:s3:::test\"],\ "sid\": \ "\"},{\ "action\": [\ "s3:abortmultipartupload\", \ " S3:deleteobject\ ", \" s3:getobject\ ", \" S3:listmultipartuploadparts\ "," s3:putobject\ "],\" effect\ ": \" Allow\ ", \" Principal\ ": {\" aws\ ": [\" *\ "]},\" resource\ ": [\" arn:aws:s3:::test/*\ "],\" sid\ ": \" \ "}]}";
public static Minioclientutils getinstance () {
if (null! = Minioclientutils) {
return minioclientutils;
}
Synchronized (Minioclientutils.class) {
if (null = = Minioclientutils) {
Minioclientutils = new Minioclientutils ();
}
}
return minioclientutils;
}
Private Minioclientutils () {
Init ();
}
private void init () {
Final Configuration Configuration = Initconfiguration ();
String url = configuration.getstring ("Minio.url", stringutils.empty);
String username = configuration.getstring ("Minio.name", stringutils.empty);
String Password = configuration.getstring ("Minio.password", stringutils.empty);
String region = configuration.getstring ("Minio.region", stringutils.empty);
try {
if (stringutils.isnotempty (URL) && stringutils.isnotempty (username) && Stringutils.isnotempty ( Password)) {
Minioclient = new minioclient (URL, username, password, false);
}
} catch (Exception e) {
Logger.error ("Restclient.close occur error", e);
}
}
public boolean createbucketpublic (String bucketname) {
Boolean iscreated;
try {
if (minioclient.bucketexists (Bucketname)) {
iscreated = false;
// }
Minioclient.makebucket ("Buzi");
Minioclient.setbucketpolicy (Bucketname, Bucketpublicpolicy);
Iscreated = true;
} catch (Exception e) {
iscreated = false;
Logger.error ("Createbucketpublic", e);
E.printstacktrace ();
}
return iscreated;
}
public string Uploadjpegfile (string bucketname, String Miniopath, String jpgfilepath) {
Return UploadFile (Bucketname, Miniopath, Jpgfilepath, Mediatype.image_jpeg_value);
}
public string Uploadjpegstream (string bucketname, String miniopath, InputStream inputstream) {
Return Uploadstream (Bucketname, Miniopath, InputStream, Mediatype.image_jpeg_value);
}
public string Uploadstream (string bucketname, String Miniofilepath, InputStream InputStream, string mediatype) {
Logger.info ("Uploadstream for bucketname={} miniofilepath={} inputstream.getclass={}, mediatype={}", BucketName,
Miniofilepath, Inputstream.getclass (), mediatype);
IF (Stringutils.isblank (mediatype)) {
mediatype = Mediatype.application_octet_stream_value;
}
try {
Putobjectwithretry (Bucketname, Miniofilepath, InputStream, mediatype);
Return Cleanurlbyremoveip (Minioclient.getobjecturl (Bucketname, Miniofilepath));
} catch (Exception e) {
Logger.error ("Uploadstream occur error:", e);
throw new RuntimeException (e);
}
}
public string UploadFile (string bucketname, String Miniofilepath, String LocalFile, string mediatype) {
Logger.info ("UploadFile for bucketname={} miniofilepath={} localfile={}, mediatype={}", Bucketname,
Miniofilepath, LocalFile, mediatype);
IF (Stringutils.isblank (mediatype)) {
mediatype = Mediatype.application_octet_stream_value;
}
try {
Putobjectwithretry (Bucketname, Miniofilepath, LocalFile, mediatype);
Return Cleanurlbyremoveip (Minioclient.getobjecturl (Bucketname, Miniofilepath));
} catch (Exception e) {
Logger.error ("UploadFile occur error:", e);
throw new RuntimeException (e);
}
}
Public list<minioentity> Listfilesswap (string bucketname, String prefix, Boolean recursive) {
Logger.info ("List files for bucketname={} prefix={} recursive={}", Bucketname, prefix, recursive);
Return Swapresulttoentitylist (minioclient.listobjects (bucketname, prefix, recursive));
}
Public iterable<result<item>> listfiles (string bucketname, String prefix, Boolean recursive) {
Logger.info ("List files for bucketname={} prefix={} recursive={}", Bucketname, prefix, recursive);
Return minioclient.listobjects (bucketname, prefix, recursive);
}
Public list<minioentity> Listfilesbybucketnameswap (String bucketname) {
Logger.info ("Listfilesbybucketname for bucketname={}", Bucketname);
Return Swapresulttoentitylist (minioclient.listobjects (Bucketname, NULL, true));
}
Public iterable<result<item>> listfilesbybucketname (String bucketname) {
Logger.info ("Listfilesbybucketname for bucketname={}", Bucketname);
Return minioclient.listobjects (Bucketname, NULL, TRUE);
}
Public iterable<result<item>> Listfilesbybucketandprefix (string bucketname, string prefix) {
Logger.info ("Listfilesbybucketandprefix for bucketname={} and prefix={}", Bucketname, prefix);
Return minioclient.listobjects (bucketname, prefix, true);
}
Public list<minioentity> Listfilesbybucketandprefixswap (string bucketname, string prefix) {
Logger.info ("Listfilesbybucketandprefix for bucketname={} and prefix={}", Bucketname, prefix);
Return Swapresulttoentitylist (minioclient.listobjects (bucketname, prefix, true));
}
Private Configuration initconfiguration () {
ClassLoader ClassLoader = MinioClientUtils.class.getClassLoader ();
if (null = = ClassLoader) {
ClassLoader = Thread.CurrentThread (). Getcontextclassloader ();
}
Configuration configuration = null;
URL resource = Classloader.getresource ("minio.properties");
if (null = = Resource) {
Logger.error ("Can not find minio.properties");
throw new RuntimeException ("Can not find minio.properties");
}
try {
Configuration = new Propertiesconfiguration (Resource);
} catch (ConfigurationException e) {
Logger.error ("Load properties from url={} occur error", resource.tostring ());
throw new RuntimeException ("Load properties from Url=" + resource.tostring () + "occur error", e);
}
return configuration;
}
Private Minioentity swapresulttoentity (result<item> Result) {
Minioentity minioentity = new minioentity ();
try {
if (result.get () = null) {
Item item = Result.get ();
Minioentity.setobjectname (Cleanurlbyremoveip (Item.objectname ()));
Minioentity.setdir (Item.isdir ());
Minioentity.setetag (Item.etag ());
Minioentity.setlastmodified (Item.lastmodified ());
Minioentity.setsize (Item.size ());
Minioentity.setstorageclass (Item.storageclass ());
}
} catch (Exception e) {
Logger.error ("Urlutils error, e={}", E.getmessage ());
}
return minioentity;
}
Private list<minioentity> Swapresulttoentitylist (iterable<result<item>> results) {
list<minioentity> minioentities = new arraylist<> ();
for (result<item> result:results) {
Minioentities.add (swapresulttoentity (result));
}
return minioentities;
}
public void Putobjectwithretry (string bucketname, String objectName, InputStream Stream, String contentType) throws Ioexc Eption, InvalidKeyException, NoSuchAlgorithmException, Insufficientdataexception, InvalidArgumentException, Noresponseexception, Invalidbucketnameexception, Xmlpullparserexception, internalexception {
int current = 0;
Boolean issuccess = false;
while (!issuccess && current < Retry_num) {
try {
Minioclient.putobject (Bucketname, ObjectName, Stream, ContentType);
Issuccess = true;
} catch (Errorresponseexception e) {
Logger.warn ("[Minio] putObject stream, errorresponseexception occur for time =" + current, E);
current++;
}
}
if (current = = Retry_num) {
Logger.error ("[Minio] PutObject, backetname={}, objectname={}, failed finally!");
}
}
public void Putobjectwithretry (string bucketname, String objectName, String fileName, String contentType) throws Invalidb Ucketnameexception, NoSuchAlgorithmException, Insufficientdataexception, IOException, InvalidKeyException, Noresponseexception, Xmlpullparserexception, Errorresponseexception, Internalexception, InvalidArgumentException, insufficientdataexception {
int current = 0;
Boolean issuccess = false;
while (!issuccess && current < Retry_num) {
try {
Minioclient.putobject (Bucketname, ObjectName, FileName, ContentType);
Issuccess = true;
} catch (Errorresponseexception e) {
current++;
Logger.debug ("[Minio] PutObject file, errorresponseexception occur!");
}
}
if (current = = Retry_num) {
Logger.error ("[Minio] PutObject, backetname={}, objectname={}, failed finally!");
}
}
public static void Main (string[] args) {
Minioclientutils.getinstance (). Createbucketpublic ("Helo");
}
}
}
3. The tool class has encapsulated various uploads.
4.public void Upload (@RequestParam ("file") mulipartfile data,string bucketname,string path) {
String fileName = Data.getoriginalfilename ();
String FilePath = path+ "/" +filename;
String ContentType = Data.getcontenttype ();
InputStream Inputstram = Data.getinputstream ();
Minioclientutils minioclientutils = Minioclinetutils.getinstance ();
Minioclientutils.uploadstream (
Bucketname,
FilePath,
InputStream,
ContentType
);
}
Minio Integrated springboot development for file upload