将 S3 TransferUtility 与 Content Uri 结合使用
Use S3 TransferUtility with Content Uri
我正在尝试使用 TransferUtility
将文件上传到 S3。我通过 ACTION_GET_CONTENT
Intent 获取 Uri。
Intent 的结果类似于 content://com.android.externalstorage.documents/document/0000-0000:DCIM/Camera/20170811_105420_HDR.jpg
我设法使用 PutObjectRequest
和 InputStream
通过 AmazonS3Client
上传文件,但我想使用 TransferUtility
和 TransferObserver
方便和暂停上传的能力。
我的代码:
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);
崩溃:
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)
提前致谢
File f = new File(_uri.getPath());
一个Uri
不是一个文件,TransferUtility
似乎需要一个文件。所以,要么:
停止使用 ACTION_GET_CONTENT
,而是集成 a file chooser library,这样您就可以将自己限制在实际文件中,或者
在 ContentResolver
上使用 Uri
和 openInputStream()
,然后使用该流将数据复制到您控制的某个文件,然后上传那个文件。
我正在尝试使用 TransferUtility
将文件上传到 S3。我通过 ACTION_GET_CONTENT
Intent 获取 Uri。
Intent 的结果类似于 content://com.android.externalstorage.documents/document/0000-0000:DCIM/Camera/20170811_105420_HDR.jpg
我设法使用 PutObjectRequest
和 InputStream
通过 AmazonS3Client
上传文件,但我想使用 TransferUtility
和 TransferObserver
方便和暂停上传的能力。
我的代码:
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);
崩溃:
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)
提前致谢
File f = new File(_uri.getPath());
一个Uri
不是一个文件,TransferUtility
似乎需要一个文件。所以,要么:
停止使用
ACTION_GET_CONTENT
,而是集成 a file chooser library,这样您就可以将自己限制在实际文件中,或者在
ContentResolver
上使用Uri
和openInputStream()
,然后使用该流将数据复制到您控制的某个文件,然后上传那个文件。