1 oss笔记记录
基本使用
public class UploadHelper {
private static final String TAG = "UploadHelper";
//与个人的存储区域有关
private String ENDPOINT = "你的END_POINT ";
//上传仓库名
private String BUCKET_NAME = "你的BUCKET_NAME ";
public String OSS_ACCESS_KEY_ID = "你的OSS_ACCESS_KEY_ID";
public String OSS_ACCESS_KEY_SECRET = "你的OSS_ACCESS_KEY_SECRET";
// 上传文件目录名称
public String uploadFilePath = "uploadFilePath";
public OSSUploadHelperCallback callback;
private OSS client;
List<String> successPath = new ArrayList<>();
public UploadHelper() {
}
public UploadHelper(String endPoint, String ducketName, String uploadFilePath, String AccessKeyId, String AccessKeySecret) {
this.ENDPOINT = endPoint;
this.BUCKET_NAME = ducketName;
this.OSS_ACCESS_KEY_ID = AccessKeyId;
this.OSS_ACCESS_KEY_SECRET = AccessKeySecret;
this.uploadFilePath = uploadFilePath;
//得到client
client = getOSSClient();
}
private OSS getOSSClient() {
if (true) {
OSSLog.enableLog();
}
ClientConfiguration conf = new ClientConfiguration();
conf.setConnectionTimeout(15 * 1000); // 连接超时,默认15秒
conf.setSocketTimeout(15 * 1000); // socket超时,默认15秒
conf.setMaxConcurrentRequest(5); // 最大并发请求书,默认5个
conf.setMaxErrorRetry(1); // 失败后最大重试次数,默认2次
LogUtils.logd(ENDPOINT + " : " + OSS_ACCESS_KEY_ID + " : " + OSS_ACCESS_KEY_SECRET);
OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider(OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET, "");
return new OSSClient(App.getAppContext(), ENDPOINT, credentialProvider, conf);
}
int number;
/**
* 多图片上传方法
* @param paths 需上传文件的路径
*/
private void uploads(List<String> paths) {
if (client == null) {
return;
}
successPath.clear();
number = 1;
for (String path : paths) {
// 构造上传请求
PutObjectRequest request =
new PutObjectRequest(BUCKET_NAME,
getObjectKey(path), path);
request.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
@Override
public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
// LogUtils.logd("currentSize: " + currentSize + " totalSize: " + totalSize);
}
});
// 本地异常如网络异常等
// 服务异常
OSSAsyncTask ptask = client.asyncPutObject(request, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
@Override
public void onSuccess(PutObjectRequest request, PutObjectResult result) {
successPath.add(getAllPath(request.getObjectKey()));
if (number == paths.size() && callback != null) {
callback.onSuccess(successPath);
}
number++;
}
@Override
public void onFailure(PutObjectRequest request, ClientException clientException, ServiceException serviceException) {
LogUtils.logd("onFailure");
if (callback != null) {
callback.onFailure(request, clientException, serviceException);
}
if (clientException != null) {
// 本地异常如网络异常等
clientException.printStackTrace();
LogUtils.logd(clientException.toString());
}
if (serviceException != null) {
// 服务异常
LogUtils.logd("ErrorCode" + serviceException.getErrorCode());
}
}
});
ptask.waitUntilFinished();
}
}
// 拼接自己服务器的URL
private String getAllPath(String objectKey) {
String imageUrl = "";
if (!TextUtils.isEmpty(objectKey)) {
int i = objectKey.lastIndexOf("/");
String substring = objectKey.substring(i + 1, objectKey.length());
imageUrl = "http://app-linktalk-web.oss-cn-beijing.aliyuncs.com/" + uploadFilePath + substring;
LogUtils.logd(substring);
LogUtils.logd(imageUrl);
}
return imageUrl;
}
/**
* 上传单个文件 (图片,视频,gif,.....)
*
* @param path 本地地址
* @param callback 成功回调
*/
public void uploadImage(String path, OSSUploadHelperCallback callback) {
this.callback = callback;
List<String> strings = new ArrayList<>();
strings.add(path);
uploads(strings);
}
/**
* 上传多文件 (图片,视频,gif,.....)
*
* @param paths 本地地址
* @param callback 成功回调
*/
public void uploadImages(List<String> paths, OSSUploadHelperCallback callback) {
this.callback = callback;
uploads(paths);
}
/**
* 获取OssToken 并上传文件
* @param path 本地地址
* @param callback 成功回调
*/
public void uploadImageT(Context context,String path, OSSUploadHelperCallback callback) {
this.callback = callback;
OkGo.<BaseRespose<OSSTokenBean>>post(Api.formatUrl("/XinBu/OssValues?user=lianyou-oss&token=5c413c4158db33132824a16e"))
.tag(context)
.execute(new DialogCallback<BaseRespose<OSSTokenBean>>(context, false) {
@Override
public void onSuccess(Response<BaseRespose<OSSTokenBean>> response) {
OSSTokenBean ossTokenBean = response.body().data;
ENDPOINT = ossTokenBean.getPoint();
BUCKET_NAME = ossTokenBean.getDucketName();
OSS_ACCESS_KEY_ID = ossTokenBean.getAccessKeyId();
OSS_ACCESS_KEY_SECRET = ossTokenBean.getAccessKeySecret();
uploadFilePath = ossTokenBean.getUploadFilePath();
//得到client
client = getOSSClient();
List<String> strings = new ArrayList<>();
strings.add(path);
uploads(strings);
}
});
}
/**
* 取得系统时间
* @return 时间戳
*/
private String getDateString() {
return System.currentTimeMillis() + "";
}
/**
* 返回key
*
* @param path 本地路径
* @return key
*/
//格式: portrait/201805/sfdsgfsdvsdfdsfs.jpg
private String getObjectKey(String path) {
LogUtils.logd(path);
return String.format(uploadFilePath + "%s." + getFormatName(path), getDateString());
}
/**
* 获取文件格式名
*/
private String getFormatName(String fileName) {
//去掉首尾的空格
fileName = fileName.trim();
String s[] = fileName.split("\\.");
if (s.length >= 2) {
return s[s.length - 1];
}
return "";
}
public interface OSSUploadHelperCallback {
void onSuccess(List<String> allPath);
void onFailure(PutObjectRequest request, ClientException clientException, ServiceException serviceException);
}
}
网友评论