AmazonS3(aws 云服务android sdk接入)

参考

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());
    }
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
Spring Boot Amazon S3 是一个集成组件,它允许你在使用 Spring Boot 框架的 Java 应用程序中轻松地与 Amazon Simple Storage Service (S3) 集成。Amazon S3 是一种云存储服务,用于存储和检索大量的静态文件、应用程序数据或其他任何类型的数据。 在 Spring Boot 中集成 Amazon S3,你可以执行以下操作: 1. 添加依赖:在你的 `pom.xml` 或者 `build.gradle` 文件中添加 AWS SDK for Java 和 Spring Cloud AWS S3 的依赖。 ```xml <!-- Maven --> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-s3</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-aws-s3</artifactId> </dependency> // Gradle implementation 'com.amazonaws:aws-java-sdk-s3' implementation 'org.springframework.cloud:spring-cloud-starter-aws-s3' ``` 2. 配置AWS凭证:你需要提供 AWS 密钥(ACCESS_KEY_ID)和秘密访问密钥(SECRET_ACCESS_KEY),或者设置环境变量(如 `AWS_ACCESS_KEY_ID` 和 `AWS_SECRET_ACCESS_KEY`),以授权对 S3 的访问。 3. 创建 S3 实体和Repository:根据需要定义 S3 存储的对象模型,如 `S3Object` 或自定义实体,然后创建对应的 Repository 接口以支持 CRUD 操作。 4. 上传/下载文件:使用 Spring Boot 的 `@Autowired` 注解注入 S3Client 对象,然后调用其提供的方法来上传文件到 S3 或从 S3 下载文件。 5. 使用S3服务:Spring Cloud AWS S3 提供了诸如 `AmazonS3Operations` 或 `AmazonS3Client` 的便利类,可以直接用于执行常见的 S3 操作,如列出对象、删除对象等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值