OSS上传视频文件记录

项目中用到了视频上传到OSS服务器,做个记录:

1.首先添加依赖


//添加OSS对象存储的sdk

compile'com.aliyun.dpa:oss-android-sdk:2.4.5'

compile'com.squareup.okhttp3:okhttp:3.4.1'

compile'com.squareup.okio:okio:1.9.0'


2.权限设置


3.初始化oss



public class OssAuthConfig {

private ContextmContext;

private ActivitymActivity;

private String endpoint ="http://oss-cn-qingdao.aliyuncs.com";//oss服务器所在地.根据oss判断

private String accessKeyId ="";//注册oss后获取到的key

private String secretKeyId ="";//后台设置加密获取的stskeyid

private String securityToken ="";//后台设置加密获取的ststoken

private String securityExpiration ="";

private String bucketName ="*****";//由后台在申请oss的时候配置

private StringcurrentTime;

public OssAuthConfig(Context mContext) {

this.mContext = mContext;

}

public OSS initOss(OssAuthBean.DataBean data){

accessKeyId = data.getAccessKeyId();

secretKeyId = data.getAccessKeySecret();

securityToken = data.getSecurityToken();

securityExpiration = data.getExpiration();

//这里的accessKeyId,secretKeyId,securityToken 是

OSSCredentialProvider credentialProvider =

new OSSStsTokenCredentialProvider(accessKeyId,secretKeyId,securityToken);

ClientConfiguration conf =new ClientConfiguration();

conf.setConnectionTimeout(15 *1000);// 连接超时,默认15秒

        conf.setSocketTimeout(15 *1000);// socket超时,默认15秒

        conf.setMaxConcurrentRequest(5);// 最大并发请求数,默认5个

        conf.setMaxErrorRetry(2);// 失败后最大重试次数,默认2次

        OSS oss =new OSSClient(mContext,endpoint, credentialProvider);

//返回oss对象供调用

return oss;

}

/**

*异步上传视频到oss

/

public void upLoadVideo(String path , String name , OSS oss){

// 构造上传请求

        Calendar calendar = Calendar.getInstance();

Date date = calendar.getTime();

String timeData = getTime(date);

String time = timeData +"-00-00-00";

currentTime = DateUtil.dataOne(time);

final String upLoadName ="OriginalVideo/android/" +currentTime +".mp4";

//upLoadName是上传的文件存在oss的名字,我这里使用了时间戳

//path是文件在本地的路径

PutObjectRequest put =new PutObjectRequest(bucketName, upLoadName, path);

        OSSAsyncTask task = oss.asyncPutObject(put,new OSSCompletedCallback() {

@Override

            public void onSuccess(PutObjectRequest request, PutObjectResult result) {

Log.d("PutObject","UploadSuccess");

if (mOnUploadSuccessListener !=null) {

mOnUploadSuccessListener.onSuccessListener(upLoadName);

}

}

@Override

            public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {

// 请求异常

                if (clientExcepion !=null) {

// 本地异常如网络异常等

                    clientExcepion.printStackTrace();

}

if (serviceException !=null) {

// 服务异常

                    Log.e("ErrorCode", serviceException.getErrorCode());

Log.e("RequestId", serviceException.getRequestId());

Log.e("HostId", serviceException.getHostId());

Log.e("RawMessage", serviceException.getRawMessage());

}

}

});

}

private String getTime(Date date) {//可根据需要自行截取数据显示

        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");

return format.format(date);

}

/**

*设置回调接口方便监听视频上传成功

/

OnUploadSuccessListenermOnUploadSuccessListener;//声明接口

    public void setUploadListener(OnUploadSuccessListener OnUploadSuccessListener) {

mOnUploadSuccessListener = OnUploadSuccessListener;

}

public interface OnUploadSuccessListener {

void onSuccessListener(String name);

}

}

你可能感兴趣的:(OSS上传视频文件记录)