博客
关于我
Android OSS下载文件
阅读量:168 次
发布时间:2019-02-28

本文共 4761 字,大约阅读时间需要 15 分钟。

近期,公司要求使用OSS下载文件,优化下载模块

首先,想要使用OSS,必须下载其相关依赖

implementation 'com.aliyun.dpa:oss-android-sdk:2.9.5'   // implementation project(':oss-android-sdk')

依赖下载成功后,需要配置Config文件(提高安全性,使用STS方式)

public class Config {       // 访问的endpoint地址    public static final String OSS_ENDPOINT = "后台获取的end_point地址";    //callback 测试地址    public static final String OSS_CALLBACK_URL = "上传的callback";       public static final String STS_SERVER_URL = "后台获取STS地址";//STS 地址    public static final String BUCKET_NAME = "后台提供的Bucket_name";    public static final String OSS_ACCESS_KEY_ID = "后台提供的AccessKeyID";    public static final String OSS_ACCESS_KEY_SECRET = "后台提供的AccessKeySecret";    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;    public static final int REQUESTCODE_OPEN_DOCUMENT = 10113;}

在我们配置好Config文件后,就可以初始化OSS了:

OSSCredentialProvider credentialProvider;        //使用自己的获取STSToken的类       	credentialProvider = new OSSAuthCredentialsProvider(Config.STS_SERVER_URL);       	editBucketName = Config.BUCKET_NAME;        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(getApplicationContext(), endpoint, credentialProvider, conf);        OSSLog.enableLog();		//开启Log打印        return new OssService(oss, editBucketName, displayer);

初始化完成后,就可以进行下载啦:

GetObjectRequest get = new GetObjectRequest(Config.BUCKET_NAME, 下载的文件路径);		//此处为设置进度条,如不需要可以删除        get.setProgressListener(new OSSProgressCallback
() { @Override public void onProgress(GetObjectRequest request, long currentSize, long totalSize) { Log.d("GetObject", "currentSize: " + currentSize + " totalSize: " + totalSize); int progress = (int) (100 * currentSize / totalSize); mDisplayer.updateProgress(progress); mDisplayer.displayInfo("下载进度: " + String.valueOf(progress) + "%"); } }); OSSLog.logDebug("asyncGetObject"); // 此处下载任务 OSSAsyncTask task = mOss.asyncGetObject(get, new OSSCompletedCallback
() { @Override public void onSuccess(GetObjectRequest request, GetObjectResult result) { // 请求成功 InputStream inputStream = result.getObjectContent(); //Bitmap bm = BitmapFactory.decodeStream(inputStream); try { //需要根据对应的View大小来自适应缩放 Bitmap bm = mDisplayer.autoResizeFromStream(inputStream); long get_end = System.currentTimeMillis(); OSSLog.logDebug("get cost: " + (get_end - get_start) / 1000f); mDisplayer.downloadComplete(bm); mDisplayer.displayInfo("Bucket: " + mBucket + "\nObject: " + request.getObjectKey() + "\nRequestId: " + result.getRequestId()); } catch (IOException e) { e.printStackTrace(); } } @Override public void onFailure(GetObjectRequest request, ClientException clientExcepion, ServiceException serviceException) { String info = ""; // 请求异常 if (clientExcepion != null) { // 本地异常如网络异常等 clientExcepion.printStackTrace(); info = clientExcepion.toString(); } if (serviceException != null) { // 服务异常 Log.e("ErrorCode", serviceException.getErrorCode()); Log.e("RequestId", serviceException.getRequestId()); Log.e("HostId", serviceException.getHostId()); Log.e("RawMessage", serviceException.getRawMessage()); info = serviceException.toString(); } mDisplayer.downloadFail(info); mDisplayer.displayInfo(info); } });

转载地址:http://fayj.baihongyu.com/

你可能感兴趣的文章
Netty channelRegistered\ChannelActive---源码分析
查看>>
Netty NIO transport && OIO transport
查看>>
netty php,netty
查看>>
Netty WebSocket客户端
查看>>
netty 主要组件+黏包半包+rpc框架+源码透析
查看>>
Vue过渡 & 动画---vue工作笔记0014
查看>>
Netty 异步任务调度与异步线程池
查看>>
Netty 的 Handler 链调用机制
查看>>
Netty 编解码器和 Handler 调用机制
查看>>
Netty 编解码器详解
查看>>
Netty 解决TCP粘包/半包使用
查看>>
Netty 调用,效率这么低还用啥?
查看>>
Netty 高性能架构设计
查看>>
Netty+Protostuff实现单机压测秒级接收35万个对象实践经验分享
查看>>
Netty+SpringBoot+FastDFS+Html5实现聊天App详解(一)
查看>>
netty--helloword程序
查看>>
netty2---服务端和客户端
查看>>
【Flink】Flink 2023 Flink易用性和稳定性在Shopee的优化-视频笔记
查看>>
Netty5.x 和3.x、4.x的区别及注意事项(官方翻译)
查看>>
netty——bytebuf的创建、内存分配与池化、组成、扩容规则、写入读取、内存回收、零拷贝
查看>>