S3 upload file

Upload file to S3

Pre-request: user should already sign-in
S3 config should be defined in /res/raw/awsconfiguration.json

step 1: Configuuration of services

  • download awsconfiguration.json and update it in /res/raw/awsconfiguration.json
    在这里插入图片描述在这里插入图片描述

Step 2: code block

upload file

add it in gradle:

dependencies {
   implementation 'com.amazonaws:aws-android-sdk-s3:2.6.+'
   implementation 'com.amazonaws:aws-android-sdk-cognito:2.6.+'
}

The file is stored in S3 : cyc-userfiles-mobilehub-494198306 bucket

Storage Path : /public/route_trace/user unique ID/xxx(timestamp).csv

Required param : A File class object

Assume : an upload button and need to set click listener

	uploadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            
                // Init Transfer utility 
            
                TransferUtility transferUtility =
                        TransferUtility.builder()
                                .context(getApplicationContext())
                                .awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
                                .s3Client(new AmazonS3Client(AWSMobileClient.getInstance().getCredentialsProvider()))
                                .build();
                                
                // do upload method, and using TransferObserver objects to monitor uploading status            
                
                String file_name = Long.toString(System.currentTimeMillis() / 1000L)+".csv";
                TransferObserver uploadObserver =
                        transferUtility.upload(
                                "cyc-userfiles-mobilehub-494198306",
                                "public/route_trace/"+IdentityManager.getDefaultIdentityManager().getCachedUserID()+"/"+file_name, file);
                
                uploadObserver.setTransferListener(new TransferListener() {

                    @Override
                    public void onStateChanged(int id, TransferState state) {
                        if (TransferState.COMPLETED == state) {
                            // Handle a completed upload.
                            System.out.println("######################Complete");
                        }
                    }

                    @Override
                    public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
                        float percentDonef = ((float) bytesCurrent / (float) bytesTotal) * 100;
                        int percentDone = (int)percentDonef;
                        System.out.println("######################"+"YourActivity ID:" + id + " bytesCurrent: " + bytesCurrent
                                + " bytesTotal: " + bytesTotal + " " + percentDone + "%");
                    }

                    @Override
                    public void onError(int id, Exception ex) {
                        // Handle errors
                        System.out.println("######################Error");
                        ex.printStackTrace();
                    }
                });
            }
        });
Note: 
File class object should be passed into method

TransferObserver uploadObserver =
                        transferUtility.upload(
                                "cyc-userfiles-mobilehub-494198306",
                                "public/route_trace/"+IdentityManager.getDefaultIdentityManager().getCachedUserID()+"/"+file_name, file);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值