1

I created a function to upload an image to AWS S3 bucket. The function is called when a button is pressed. This works for first instance after a new Gradle build. But when I try to call the function again, I get the following error:

E/StorageQuickstart: The client issued a subsequent call to `Amplify.configure`
after the first had already succeeded.
btnSubmit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Upload to AWS S3
        File imageFile = new File(imgFilePath);
        uploadImage(imageFile.getName(), imgFilePath);
        finish();
    }
});

Function:

// AWS Amplify
private void uploadImage(String imageName, String imageAbsolutePath) {
    // Amplify Initialize
    AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {
        @Override
        public void onResult(UserStateDetails userStateDetails) {
            try {
                Amplify.addPlugin(new AWSS3StoragePlugin());
                Amplify.configure(getApplicationContext());
                Log.i("StorageQuickstart", "Amplify Initialized");
                Amplify.Storage.uploadFile(
                    imageName,
                    imageAbsolutePath,
                    new ResultListener<StorageUploadFileResult>() {
                        @Override
                        public void onResult(StorageUploadFileResult result) {
                            Log.i("StorageQuickStart", "Successfully uploaded: " + result.getKey());
                            Log.i("ImageURL", getS3ObjectUrl(imageName));
                        }

                        @Override
                        public void onError(Throwable error) {
                            Log.e("StorageQuickstart", "Upload error.", error);
                        }
                    }
                );
            } catch (Exception e) {
                Log.e("StorageQuickstart", e.getMessage());
            }
        }

        @Override
        public void onError(Exception e) {
            Log.e("StorageQuickstart", "Initialization error.", e);
        }
    });
}
Jameson
  • 6,400
  • 6
  • 32
  • 53
ConfusedPanda
  • 33
  • 1
  • 5

1 Answers1

2

This is because AWSMobileClient.getInstance().initialize() method gets called, every time you click the button. Whereas initialization should be done only once. So, You can call AWSMobileClient.getInstance().initialize() method either in the Launcher Activity or you can use it in Application class as mentioned in the initialize Amplify section in the docs

Jameson
  • 6,400
  • 6
  • 32
  • 53
Feroz Khan
  • 2,396
  • 5
  • 20
  • 37