如何在Android中的Cognito用户/身份池中使用TransferUtility [英] How to use TransferUtility with Cognito user / Identity pools in Android
问题描述
我需要将Android中的一个文件上传到s3存储桶。
我们要使用Cognito用户池对上传进行身份验证。
I need to upload one file in Android to an s3 bucket. We want to authenticate the upload with Cognito User pools.
执行此操作的标准方法是使用Amazon Amplify SDK中的Storage API。
The standard way to do this would be to use the Storage API from the Amazon Amplify SDK.
问题是我需要报告上传进度,并且Amplify Storage不允许您设置监听器以观察Android中的进度。
Problem is that I need to report upload progress for the upload, and Amplify Storage does not allow you to set a listener to observe Progress in Android.
这使我需要使用 TransferUtility
。
我如何应该使用具有TransferUtility的Cognito对上传操作进行身份验证?
How I am supposed to authenticate the upload operation using Cognito with TransferUtility?
目前我正在执行以下操作:
At the moment I'm doing:
val credentials = CognitoCachingCredentialsProvider(
context,
"eu-west-1:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
Regions.US_WEST_1)
val s3Client = AmazonS3Client(credentials, Region.getRegion(Regions.US_WEST_1))
val transferUtility = TransferUtility.builder()
.context(context)
.s3Client(s3Client)
.build()
但是我得到了403:
com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: XXXXXXXXXXX), S3 Extended Request ID: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx=
我真的努力获取任何文档,因为似乎 Storage
API现在比 TransferUtility
更受青睐, Cognito用户池或用户身份是通过Amplitude Storage完成的。
I'm really struggling to get any documentation for this, as it seems the Storage
API is now preferred over TransferUtility
, and everything to do with Cognito user pools or user identities is done with Amplitude Storage
推荐答案
我终于找到了一些与此相关的文档。
基本上,您将 AWSMobileClient
用作 AWSConfiguration
的实现,并构建 TransferUtility
I've finally found some documentation do to this.
Basically, you use AWSMobileClient
as the implementation for the AWSConfiguration
with building the TransferUtility
TransferUtility transferUtility =
TransferUtility.builder()
.context(getApplicationContext())
.awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
.s3Client(new AmazonS3Client(AWSMobileClient.getInstance().getCredentialsProvider()))
.build();
TransferObserver uploadObserver =
transferUtility.upload(
"s3Folder/s3Key.txt",
new File("/path/to/file/localFile.txt"));
此外,我还需要说明另外两个警告可以在不接收HTTP 403的情况下执行此工作:
Also, there are two other caveats I had to figure out to make this work without receiving HTTP 403:
- 将必需的信息添加到
awsconfiguration.json
- Add required information to
awsconfiguration.json
{
"IdentityManager": {
"Default": {}
},
"CognitoUserPool": {
"Default": {
"PoolId": "region_XXXXXXX",
"AppClientId": "XXXXXXXXXXXXXXX",
"Region": "XXXXX"
}
},
"CredentialsProvider": {
"CognitoIdentity": {
"Default": {
"PoolId": "XXXXX:XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Region": "XXXXXX"
}
}
},
"S3TransferUtility": {
"Default": {
"Bucket": "XXXXXXXXXXXXXXXXXXX-data-bucket",
"Region": "XXXXXX"
}
}
}
请注意, CredentialsProvider.CognitoIdentity.Default.PoolId
与
CognitoUserPool不同。 Default.PoolId
!
- 将 private / public前缀附加到
key
在需要时执行上载,具体取决于服务器端的配置。
- Prepend the "private"/"public" prefix to the
key
when performing the upload if required, depending on the server-side configuration.
这篇关于如何在Android中的Cognito用户/身份池中使用TransferUtility的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!