package com.fanghua.driver.utils;
|
|
import android.os.Handler;
|
import android.os.Looper;
|
|
import com.fanghua.driver.base.Const;
|
import com.obs.services.ObsClient;
|
import com.obs.services.model.PutObjectResult;
|
|
import java.io.File;
|
import java.util.ArrayList;
|
|
public class OSSUtil {
|
|
public abstract static class OSSUploadCallBack {
|
//单张上传成功
|
public void onFinish(String url) {
|
|
}
|
|
//批量上传成功
|
public void onFinish(ArrayList<String> urls) {
|
}
|
|
//成功文件大小回调
|
public void onSizeProgress(Long currentSize, Long totalSize) {
|
}
|
|
//成功文件数目回调
|
public void onCountProgress(int currentCount, int totalCount) {
|
}
|
|
|
public void onFial(String message) {
|
|
}
|
|
}
|
|
private String bucketName = Const.bucketName;
|
private String endpoint = Const.endpoint;
|
private String keyID = Const.accessKeyId;
|
private String OSS_KEY = Const.accessKeySecret;
|
|
//上传图片
|
public String upImageSync(String imgPath,OSSUploadCallBack callBack) {
|
final String[] url = {imgPath};
|
new Thread(() -> {
|
try {
|
ObsClient obsClient = new ObsClient(keyID,OSS_KEY,endpoint);
|
// obsClient.createBucket(PassportConfig.OSS_BUCKET);
|
PutObjectResult putObjectResult = obsClient.putObject(bucketName, "img/"+imgPath, new File(imgPath));
|
url[0] = putObjectResult.getObjectUrl();
|
obsClient.close();
|
new Handler(Looper.getMainLooper()).post(() -> callBack.onFinish(url[0]));
|
} catch (Exception e) {
|
url[0] = imgPath;
|
new Handler(Looper.getMainLooper()).post(() -> callBack.onFial(e.getMessage()));
|
e.printStackTrace();
|
}
|
}).start();
|
|
return url[0];
|
}
|
}
|