Android 上传图片到阿里云 OSS

分享到:

本篇博客主要介绍的是将本地图片上传到服务器的方法技巧

首先附上阿里云oss官网SDK介绍:点击打开链接

附上官方推荐的demo 点击打开链接

在app的build中配置oss :compile 'com.aliyun.dpa:oss-android-sdk:+'

技术相对简单,主要是将这些技术结合起来的例子并不多

首先先初始化oss对象

        // ACCESS_ID,ACCESS_KEY是在[阿里云](https://l.gushuji.site/aliyun)申请的        OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(ACCESS_ID, ACCESS_KEY);        ClientConfiguration conf = new ClientConfiguration();        conf.setConnectionTimeout(15 * 1000); // 连接超时,默认15秒        conf.setSocketTimeout(15 * 1000); // socket超时,默认15秒        conf.setMaxConcurrentRequest(8); // 最大并发请求数,默认5个        conf.setMaxErrorRetry(2); // 失败后最大重试次数,默认2次         // oss为全局变量,OSS_ENDPOINT是一个OSS区域地址        oss = new OSSClient(getApplicationContext(), OSS_ENDPOINT, credentialProvider, conf);

oss上传图片分为同步和异步 

同步的方法

// 构造上传请求PutObjectRequest put = new PutObjectRequest("<bucketName>", "<objectKey>", "<uploadFilePath>");// 文件元信息的设置是可选的// ObjectMetadata metadata = new ObjectMetadata();// metadata.setContentType("application/octet-stream"); // 设置content-type// metadata.setContentMD5(BinaryUtil.calculateBase64Md5(uploadFilePath)); // 校验MD5// put.setMetadata(metadata);try {    PutObjectResult putResult = oss.putObject(put);    Log.d("PutObject", "UploadSuccess");    Log.d("ETag", putResult.getETag());    Log.d("RequestId", putResult.getRequestId());} catch (ClientException e) {    // 本地异常如网络异常等    e.printStackTrace();} catch (ServiceException e) {    // 服务异常    Log.e("RequestId", e.getRequestId());    Log.e("ErrorCode", e.getErrorCode());    Log.e("HostId", e.getHostId());    Log.e("RawMessage", e.getRawMessage());}

此处这个bucketName是在阿里云平台上配置的 用来储存文件 可以设置私有公有等属性 

objectKey为文件的名字 用来从平台上查询图片

FilePath为所选文件的uri路径

异步上传的方法

// 构造上传请求PutObjectRequest put = new PutObjectRequest("<bucketName>", "<objectKey>", "<uploadFilePath>");// 异步上传时可以设置进度回调put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {    @Override    public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {        Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);    }});OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {    @Override    public void onSuccess(PutObjectRequest request, PutObjectResult result) {        Log.d("PutObject", "UploadSuccess");        Log.d("ETag", result.getETag());        Log.d("RequestId", result.getRequestId());    }    @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());        }    }});// task.cancel(); // 可以取消任务// task.waitUntilFinished(); // 可以等待任务完成

一般上传成功时候 客户端需要把文件的objectkey传给后台 后台通过拼接然后把图片的url返回给我们



相关文章