[AWS / S3] After all, how do you upload multiple files?

[Initiation] Files created with your own Android application (multiple files are generated at once) Since it became necessary to manage it on the server and receive it on another terminal, Quickly implemented with S3 from Mobile Hub.

[Stumbling] -Specify s3Client! Error appears. ・ AWS S3 Access Denied Error appears. -Don't interact with the server during the synchronization process! Error appears. ・ How can I implement uploading multiple files? Not written in the tutorial. -Although it was implemented once, an error often occurs when calling s3Client.

People who had similar worries. →AWS S3 Java SDK - Access DeniedAmazon s3 upload multiple files android

[Solution]

class AsyncAWS_upload extends AsyncTask<String, Void, String> {

    Activity BaseActivity;

    AsyncAWS_upload(Activity activity) {
        this.BaseActivity = activity;
    }

    @Override
    protected String doInBackground(String... strings) {
        uploadData();
        return null;
    }

    public void uploadData() {

       TransferUtility transferUtility =
                TransferUtility.builder()
                        .context(getApplicationContext())
                        .awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
                        .s3Client(new AmazonS3Client(AWSMobileClient.getInstance().getCredentialsProvider()))
                        .build();


        TransferObserver uploadObserver =
                transferUtility.upload(
                        "protected/hoge.file",
                        new File(Environment.getExternalStorageDirectory() + "/hoge.file"));
              
               transferUtility.upload(
                        "hogehoge_test/hoge.csv",
                        new File(Environment.getExternalStorageDirectory() + "/hoge.csv"));

        uploadObserver.setTransferListener(new TransferListener() {
            @Override
            public void onStateChanged(int id, TransferState state) {
                if (TransferState.COMPLETED == state) {
                    LOGD("XXXTransferStateonStateChanged", "FINISH");
                    // Handle a completed upload.
                }
            }

            @Override
            public void onProgressChanged(
                    int id, long bytesCurrent, long bytesTotal) {
                LOGD("XXXTransferStateonProgressChanged", "HERE");
                // TODO Auto-generated method stub
            }

            @Override
            public void onError(int id, Exception ex) {
                // TODO Auto-generated method stub
                LOGD("XXXTransferStateonError", String.valueOf(ex));
            }

        });

        // If your upload does not trigger the onStateChanged method inside your
        // TransferListener, you can directly check the transfer state as shown here.
        if (TransferState.COMPLETED == uploadObserver.getState()) {
        }
    }
}

-Specify s3Client. (Use the official code) ・ In build.gradle compile ('com.amazonaws:aws-android-sdk-mobile-client:2.6.+@aar') { transitive = true; } If you do not write correctly, com.amazonaws.mobile.client.AWSMobileClient Is not loaded. -Change the access authority to the baguette on the host side. -Create a subclass for asynchronous processing and execute it from the main class. -Make the transferUtility have multiple upload information.

To work with. I have skipped the understanding of the details, so I will supplement it as soon as I check the document. Thank you for the English document.

Recommended Posts

[AWS / S3] After all, how do you upload multiple files?
[Rails] How to upload images to AWS S3 using Carrierwave and fog-aws
[Rails] How to upload images to AWS S3 using refile and refile-s3
How do you write the ternary operator (? :)
Code that deletes all files of the specified prefix in AWS S3 (Java)
Do you use the for statement after all? Do you use a while statement? Proper use of for statement and while statement