Android平台使用AK 从阿里云oss下载文件的方法

1. 阿里云oss官网,资料比较齐,有时间应该系统看一下。

  https://help.aliyun.com/document_detail/106959.html?spm=a2c4g.11186623.6.1162.56754b86vIbv3t

2.初次使用坑比较多,主要如下:

  manifest增加app权限: 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

build.gradle增加

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

proguard-rules.pro增加

-keep class com.alibaba.sdk.android.oss.** { *; }
-dontwarn okio.**
-dontwarn org.apache.commons.codec.binary.**

使用AK下载oss

OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(OSS_ACCESS_KEY_ID,OSS_ACCESS_KEY_SECRET);
mClient = new OSSClient(MainApplication.getContext(), OSS_ENDPOINT, credentialProvider);


//主流畅
        GetObjectRequest get = new GetObjectRequest("autoai-oss-model", "ace_model/1.0.0_xxx.zip");
        getCallback = new TestGetCallback();

        try {
            mClient.getObject(get);
        } catch (ClientException e1) {
            e1.printStackTrace();
        } catch (ServiceException e1) {
            e1.printStackTrace();
        }
        OSSAsyncTask getTask = mClient.asyncGetObject(get, getCallback);

        getTask.waitUntilFinished();




   public final  class TestGetCallback implements OSSCompletedCallback<GetObjectRequest, GetObjectResult> {

        public GetObjectRequest request;
        public GetObjectResult result;
        public ClientException clientException;
        public ServiceException serviceException;


        @Override
        public void onSuccess(GetObjectRequest request, GetObjectResult result) {
            Log.v("AvceService","onSuccess");
            this.request = request;
            this.result = result;
            writeFile("/sdcard/file1m");
        }

        @Override
        public void onFailure(GetObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
            Log.v("AvceService","onFailure");
            this.request = request;
            this.clientException = clientExcepion;
            this.serviceException = serviceException;
        }
    }





//cannot be access in the main thread.
public void writeFile(String name)
    {
        String srcFileBase64Md5 = null;
        try {
            srcFileBase64Md5 = BinaryUtil.toBase64String(BinaryUtil.calculateMd5(name));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Log.i(TAG,""+srcFileBase64Md5);

        Log.i(TAG,"haha-- "+getCallback.result.getStatusCode());
        assertEquals(200, getCallback.result.getStatusCode());
        Log.i(TAG,"haha---");
        long length = getCallback.result.getContentLength();
        Log.i(TAG,"downloadFile "+length );
        byte[] buffer = new byte[(int) length];
        int readCount = 0;
        InputStream inputStream = getCallback.result.getObjectContent();
        Log.i(TAG,""+inputStream);
        while (readCount < length) {
            try {

                readCount += inputStream.read(buffer, readCount, (int) length - readCount);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            Log.i(TAG,"new file " );

            FileOutputStream fout = new FileOutputStream(name);
            Log.i(TAG,"downloadFile -- " );
            fout.write(buffer);
            fout.close();
        } catch (Exception e) {
            OSSLog.logInfo(e.toString());
            Log.i(TAG,e.toString() );

        }
        String downloadFileBase64Md5 = null;
        try {
            downloadFileBase64Md5 = BinaryUtil.toBase64String(BinaryUtil.calculateMd5(name));
        } catch (IOException e) {
            e.printStackTrace();
        }
        assertEquals(srcFileBase64Md5, downloadFileBase64Md5);
    }

其他:

AK方式。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值