天天看點

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
    S3 upload file
    S3 upload file

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);