Search code examples
androidamazon-s3awss3transferutility

Use S3 TransferUtility with Content Uri


im trying to upload a file to S3 using the TransferUtility. I get the Uri via the ACTION_GET_CONTENT Intent. The Result of the Intent looks something like content://com.android.externalstorage.documents/document/0000-0000:DCIM/Camera/20170811_105420_HDR.jpg

I managed to Upload the File via a AmazonS3Client using PutObjectRequest and InputStream but I'd like to use TransferUtility and the TransferObserver for convenience and the ability to pause the upload.

My Code:

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

File f = new File(_uri.getPath());
TransferObserver tO = tU.upload("test.jpg", f);

crashes with:

Caused by: java.lang.IllegalArgumentException: Invalid file: /document/0000-0000:DCIM/Camera/20170811_105420_HDR.jpg
                  at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:532)
                  at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:497)
                  at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:466)
                  at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:420)
                  at my.domain.myapp.aws.AwsConnector.uploadFile(AwsConnector.java:76)
                  at my.domain.myapp.MainActivity.onActivityResult(MainActivity.kt:26)
                  at android.app.Activity.dispatchActivityResult(Activity.java)
                  at android.app.ActivityThread.deliverResults(ActivityThread.java) 
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java) 
                  at android.app.ActivityThread.-wrap20(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java) 
                  at android.os.Handler.dispatchMessage(Handler.java) 
                  at android.os.Looper.loop(Looper.java) 
                  at android.app.ActivityThread.main(ActivityThread.java) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 

Thanks in advance


Solution

  • File f = new File(_uri.getPath());
    

    A Uri is not a file, and TransferUtility seems to demand a file. So, either:

    • Stop using ACTION_GET_CONTENT, and instead integrate a file chooser library, so you can limit yourself to actual files, or

    • Use the Uri and openInputStream() on ContentResolver, then use that stream to make a copy of the data to some file that you control, then upload that file.