minio 官方的sdk 只有java 版本的不兼容android,可以尝试使用 amazon s3 的oss sdk 来实现功能。两者协议是兼容的。
gradle 依赖
// https://mvnrepository.com/artifact/com.amazonaws/aws-android-sdk-s3
implementation 'com.amazonaws:aws-android-sdk-s3:2.22.5'
// https://mvnrepository.com/artifact/com.amazonaws/aws-android-sdk-mobile-client
implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.22.5'
工具类MinIoOssHelper.java
package com.example.zhstplayer.utils;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
//import io.minio.MinioClient;
//import io.minio.ObjectStat;
public class MinIoOssHelper {
private static final String TAG = MinIoOssHelper.class.getSimpleName();
private static MinIoOssHelper sMinIoOssHelper;
private String ossUrl;
private String accessKey;
private String secretKey;
private long transferedbytes;
public interface MinIoCallback {
void progress(int progress);
}
private MinIoOssHelper(String ossUrl, String accessKey, String secretKey) {
this.ossUrl = ossUrl;
this.accessKey = accessKey;
this.secretKey = secretKey;
}
public static MinIoOssHelper getInstance(String ossUrl, String accessKey, String secretKey) {
if(sMinIoOssHelper == null) {
sMinIoOssHelper = new MinIoOssHelper(ossUrl, accessKey, secretKey);
}
return sMinIoOssHelper;
}
/*
文件下载
*/
public boolean downloadOssObject(String bucketName, String objectName, String savePath, MinIoCallback callback) throws Exception {
AmazonS3 s3 = new AmazonS3Client(new AWSCredentials() {
@Override
public String getAWSAccessKeyId() {
return MinIoOssHelper.this.accessKey;//minio的key
}
@Override
public String getAWSSecretKey() {
return MinIoOssHelper.this.secretKey;//minio的密钥
}
}, Region.getRegion(Regions.CN_NORTH_1),new ClientConfiguration());
//服务器地址
s3.setEndpoint(this.ossUrl);
ObjectMetadata metadata = s3.getObjectMetadata(bucketName, objectName);
LogUtils.d(TAG, "object length = " + metadata.getContentLength() + " type = " + metadata.getContentType());
S3Object object = s3.getObject(bucketName, objectName);
InputStream stream = object.getObjectContent();
// 读取输入流直到EOF并打印到控制台。
byte[] buf = new byte[1024 * 10];
int bytesRead;
long currentBytes = 0;
int prevProcess = 0;
long totalBytes = metadata.getContentLength();
FileOutputStream fileOutputStream = new FileOutputStream(savePath);
while ((bytesRead = stream.read(buf, 0, buf.length)) >= 0) {
//System.out.println(new String(buf, 0, bytesRead));
currentBytes += bytesRead;
int process = (int)(currentBytes * 100 / totalBytes);
if(callback != null && process != prevProcess) {
callback.progress(process);
prevProcess = process;
}
fileOutputStream.write(buf, 0, bytesRead);
}
fileOutputStream.flush();
fileOutputStream.close();
return true;
}
/*
文件上传
*/
public boolean uploadOssObject(String bucketName, String objectName, String filePath, MinIoCallback callback) {
AmazonS3 s3 = new AmazonS3Client(new AWSCredentials() {
@Override
public String getAWSAccessKeyId() {
return MinIoOssHelper.this.accessKey;//minio的key
}
@Override
public String getAWSSecretKey() {
return MinIoOssHelper.this.secretKey;//minio的密钥
}
}, Region.getRegion(Regions.CN_NORTH_1),new ClientConfiguration());
//服务器地址
s3.setEndpoint(this.ossUrl);
File file = new File(filePath);
transferedbytes = 0;
s3.putObject(new PutObjectRequest(bucketName, objectName, file).withGeneralProgressListener(progressEvent -> {
if(callback != null) {
transferedbytes += progressEvent.getBytesTransferred();
int process = (int) (transferedbytes * 100 / file.length());
callback.progress(process);
}
}));
return true;
}
}