AmazonS3(aws 云服务android sdk接入)

来源:互联网 发布:英雄时代知乎 编辑:程序博客网 时间:2024/05/22 02:30

参考

aws云服务文档
https://aws.amazon.com/cn/documentation/s3/

aws云服务实例代码
https://github.com/awslabs/aws-sdk-android-samples

api
https://docs.aws.amazon.com/AWSAndroidSDK/latest/javadoc/

接入步骤

1、添加依赖

dependencies {    compile 'com.amazonaws:aws-android-sdk-s3:2.2.+'}

2、service和permission

    <service            android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService"            android:enabled="true" />
    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

3、配置参数等信息

public class Constants {    /*     * You should replace these values with your own. See the README for details     * on what to fill in.     */    public static final String COGNITO_POOL_ID = "identity pool ID";    /*     * Region of your Cognito identity pool ID.     */    public static final String COGNITO_POOL_REGION = "POOL_REGION";    /*     * Note, you must first create a bucket using the S3 console before running     * the sample (https://console.aws.amazon.com/s3/). After creating a bucket,     * put it's name in the field below.     */    public static final String BUCKET_NAME = "BUCKET_NAME ";    /*     * Region of your bucket.     */    public static final String BUCKET_REGION = "BUCKET_REGION ";}这里的值也就是对应着亚马逊网页上配置的存储信息,需要修改你自己的配置4、代码接入

文件上传

private void beginUpload(final  String filePath) {        if (filePath == null) {            Toast.makeText(this, "Could not find the filepath of the selected file",                    Toast.LENGTH_LONG).show();            return;        }        try {            TransferUtility transferUtility = S3TransferUitl.getTransferUtility(this);            File file = new File(filePath);            TransferObserver observer =                    transferUtility.upload(                            S3TransferUitl.Constants.BUCKET_NAME,                            file.getName(),                            file);            Log.e("linux","----beginUpload----"+filePath);        /*         * Note that usually we set the transfer listener after initializing the         * transfer. However it isn't required in this sample app. The flow is         * click upload button -> start an activity for image selection         * startActivityForResult -> onActivityResult -> beginUpload -> onResume         * -> set listeners to in progress transfers.         */            observer.setTransferListener(new TransferListener() {                @Override                public void onStateChanged(int id, TransferState state) {                    //这个函数会调用多次,根据不同状态来通知调用者                    Log.e("linux","--onStateChanged---state--"+state);                    if(TransferState.COMPLETED==state){                        setmAvatar(filePath);                        dismissProgressDialog();                        ToastUtil.show(mContext, R.string.upload_avatar_success);                    }else{                        //注意:当TransferState.COMPLETED!=state,并不意味着这里上传失败                    }                }                @Override                public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {                    //这个函数会调用多次,根据不同进度来通知调用者                    Log.e("linux","----onProgressChanged--bytesCurrent--"+bytesCurrent+"--bytesTotal--"+bytesTotal);                    if(bytesCurrent>=bytesTotal){//这里代码依然会调用多次                        dismissProgressDialog();                    }                }                @Override                public void onError(int id, Exception ex) {                    Log.e("linux","----onError----");                    dismissProgressDialog();                    ToastUtil.show(mContext, R.string.upload_avatar_fail);                    ex.printStackTrace();                }            });        } catch (Exception e) {            dismissProgressDialog();            ToastUtil.show(mContext, R.string.upload_avatar_fail);            e.printStackTrace();        }    }

文件下载

 private void beginDownload(String key) {        // Location to download files from S3 to. You can choose any accessible        // file.        File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + key);        // Initiate the download        TransferObserver observer = transferUtility.download(Constants.BUCKET_NAME, key, file);        /*         * Note that usually we set the transfer listener after initializing the         * transfer. However it isn't required in this sample app. The flow is         * click upload button -> start an activity for image selection         * startActivityForResult -> onActivityResult -> beginUpload -> onResume         * -> set listeners to in progress transfers.         */        // observer.setTransferListener(new DownloadListener());    }
原创粉丝点击