我正在尝试将图像资源从我的应用程序上传到S3存储桶:
我还创建了具有未经身份验证的访问权限的Cognito身份池,然后更新了IAM Unauth_Role以添加新的自定义策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject"
],
"Resource": "arn:aws:s3:::#BUCKET_NAME#/*"
}
]
}
然后在ios项目中,我安装了Pod AWSS3。 在AppDelegate中,此方法在didFinishLaunchingWithOptions中调用:
fileprivate func configureAWS()
{
AWSDDLog.sharedInstance.logLevel = .verbose
let credentialsProvider:AWSCognitoCredentialsProvider =
AWSCognitoCredentialsProvider(regionType:AWSRegionType.USEast1,
identityPoolId: "#POOL_ID#")
let configuration = AWSServiceConfiguration(region:AWSRegionType.USEast1, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
}
然后在我的控制器中,我开始上传:
if let urlImagePath = localFileURL,
let uploadRequest = AWSS3TransferManagerUploadRequest() {
[uploadRequest configuration…]
}
最后启动请求:
AWSS3TransferManager.default().upload(uploadRequest).continueWith(executor: AWSExecutor.immediate(), block: { (task) -> Any? in
if let error = task.error {
print("❌ Upload image failed with error: (\(error.localizedDescription))")
completionHandler(false)
return nil
}
}
task.error不是nil,我收到以下消息:
Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=10 "(null)" UserInfo={__type=ResourceNotFoundException, message=IdentityPool 'us-east-1:d4e0d542-089f-4fb4-83be-93946576bc00' not found.}