build.gradle
中添加依赖
implementation 'com.aliyun.dpa:oss-android-sdk:+'
Config.java
/**
* Config
*
* @author lao
* @date 2019/10/8
*/
public class Config {
// To run the sample correctly, the following variables must have valid values.
// The endpoint value below is just the example. Please use proper value according to your region
// 访问的endpoint地址
public static final String OSS_ENDPOINT = "http://oss-cn-shenzhen.aliyuncs.com";
//callback 测试地址
public static final String OSS_CALLBACK_URL = "http://oss-demo.aliyuncs.com:23450";
// STS 鉴权服务器地址。
// 或者根据工程sts_local_server目录中本地鉴权服务脚本代码启动本地STS鉴权服务器。
public static final String STS_SERVER_URL = "http://*****/sts/getsts";//STS 地址
public static final String BUCKET_NAME = "talkp";
public static final String OSS_ACCESS_KEY_ID = "仓库ID";
public static final String OSS_ACCESS_KEY_SECRET = "秘钥";
public static final int DOWNLOAD_SUC = 1;
public static final int DOWNLOAD_Fail = 2;
public static final int UPLOAD_SUC = 3;
public static final int UPLOAD_Fail = 4;
public static final int UPLOAD_PROGRESS = 5;
public static final int LIST_SUC = 6;
public static final int HEAD_SUC = 7;
public static final int RESUMABLE_SUC = 8;
public static final int SIGN_SUC = 9;
public static final int BUCKET_SUC = 10;
public static final int GET_STS_SUC = 11;
public static final int MULTIPART_SUC = 12;
public static final int STS_TOKEN_SUC = 13;
public static final int FAIL = 9999;
public static final int REQUESTCODE_AUTH = 10111;
public static final int REQUESTCODE_LOCALPHOTOS = 10112;
}
UploadHelper.java
public class UploadHelper {
private static final String TAG = UploadHelper.class.getSimpleName();
// 与你们的存储区域有关系
public static final String ENDPOINT = "oss-cn-shenzhen.aliyuncs.com";
// 上传的仓库名
private static final String BUCKET_NAME = "talkp";
private static OSS getClient() {
ClientConfiguration conf = new ClientConfiguration();
conf.setConnectionTimeout(15 * 1000); // connction time out default 15s
conf.setSocketTimeout(15 * 1000); // socket timeout,default 15s
conf.setMaxConcurrentRequest(5); // synchronous request number,default 5
conf.setMaxErrorRetry(2); // retry,default 2
OSSLog.enableLog(); //write local log file ,path is SDCard_path\OSSLog\logs.csv
OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider(Config.OSS_ACCESS_KEY_ID, Config.OSS_ACCESS_KEY_SECRET, "<StsToken.SecurityToken>");
OSSPlainTextAKSKCredentialProvider provider = new OSSPlainTextAKSKCredentialProvider(Config.OSS_ACCESS_KEY_ID, Config.OSS_ACCESS_KEY_SECRET);
OSS oss = new OSSClient(Factory.app(), Config.OSS_ENDPOINT, provider, conf);
// 明文设置secret的方式建议只在测试时使用,更多鉴权模式请参考后面的`访问控制`章节
return oss;
}
/**
* 上传的最终方法,成功返回则一个路径
*
* @param objKey 上传上去后,在服务器上的独立的KEY
* @param path 需要上传的文件的路径
* @return 存储的地址
*/
private static String upload(String objKey, String path) {
// 构造一个上传请求
PutObjectRequest request = new PutObjectRequest(BUCKET_NAME,
objKey, path);
try {
// 初始化上传的Client
OSS client = getClient();
// 开始同步上传
PutObjectResult result = client.putObject(request);
Log.d(TAG, "upload: result=" + result);
// 得到一个外网可访问的地址
String url = client.presignPublicObjectURL(BUCKET_NAME, objKey);
// 格式打印输出
Log.d(TAG, String.format("PublicObjectURL:%s", url));
return url;
} catch (Exception e) {
e.printStackTrace();
// 如果有异常则返回空
return null;
}
}
/**
* 上传普通图片
*
* @param path 本地地址
* @return 服务器地址
*/
public static String uploadImage(String path) {
String key = getImageObjKey(path);
return upload(key, path);
}
/**
* 上传头像
*
* @param path 本地地址
* @return 服务器地址
*/
public static String uploadPortrait(String path) {
String key = getPortraitObjKey(path);
return upload(key, path);
}
/**
* 上传音频
*
* @param path 本地地址
* @return 服务器地址
*/
public static String uploadAudio(String path) {
String key = getAudioObjKey(path);
return upload(key, path);
}
/**
* 分月存储,避免一个文件夹太多
*
* @return yyyyMM
*/
private static String getDateString() {
return DateFormat.format("yyyyMM", new Date()).toString();
}
// image/201703/dawewqfas243rfawr234.jpg
private static String getImageObjKey(String path) {
String fileMd5 = HashUtil.getMD5String(new File(path));
String dateString = getDateString();
return String.format("image/%s/%s.jpg", dateString, fileMd5);
}
// portrait/201703/dawewqfas243rfawr234.jpg
private static String getPortraitObjKey(String path) {
String fileMd5 = HashUtil.getMD5String(new File(path));
String dateString = getDateString();
return String.format("portrait/%s/%s.jpg", dateString, fileMd5);
}
// audio/201703/dawewqfas243rfawr234.mp3
private static String getAudioObjKey(String path) {
String fileMd5 = HashUtil.getMD5String(new File(path));
String dateString = getDateString();
return String.format("audio/%s/%s.mp3", dateString, fileMd5);
}
}