我正在尝试使用AWS iOS SDK v2在我的 Swift 项目中集成S3和Mobile Analytics。问题是我正在尝试使用两个不同的区域(我正在使用我的主要区域,爱尔兰的eu-west-1,但移动分析是only available from us-east-1 in Virginia)。
我在AppDelegate中有以下内容来初始化移动分析:
let credentialsProviderForMobileAnalytics = AWSCognitoCredentialsProvider.credentialsWithRegionType(
AWSRegionType.USEast1,
accountId: "my_account_id",
identityPoolId: "pool_id",
unauthRoleArn: "XXX",
authRoleArn: "XXX"
)
let awsConfigurationForMobileAnalytics = AWSServiceConfiguration(
region: AWSRegionType.USEast1,
credentialsProvider: credentialsProviderForMobileAnalytics
)
AWSServiceManager.defaultServiceManager().setDefaultServiceConfiguration(awsConfigurationForMobileAnalytics)
var mobileAnalytics = AWSMobileAnalytics(forAppId: "MyAwsAppId")
然后,在从ViewController调用的函数中,我想将图像上传到S3,我有:
let credentialsProviderForS3 = AWSCognitoCredentialsProvider.credentialsWithRegionType(
AWSRegionType.EUWest1,
accountId: "my_account_id",
identityPoolId: "pool_id",
unauthRoleArn: "XXX",
authRoleArn: "XXX"
)
let awsConfigurationForS3 = AWSServiceConfiguration(
region: AWSRegionType.EUWest1,
credentialsProvider: credentialsProviderForS3
)
AWSServiceManager.defaultServiceManager().setDefaultServiceConfiguration(awsConfigurationForS3)
var transferManager = AWSS3TransferManager.defaultS3TransferManager()
var fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory().stringByAppendingPathComponent("temp"))
var uploadRequest1 : AWSS3TransferManagerUploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest1.bucket = "mybucket"
uploadRequest1.key = "my-image.jpeg"
uploadRequest1.body = fileURL
var task = transferManager.upload(uploadRequest1)
如果我对以上两个部分中的一个进行评论,相应的任务就会完美地完成(当评估分析代码时,s3工作,反之亦然),但它不会一起工作 - 我怀疑这是因为我是使用AWSServiceManager.defaultServiceManager().setDefaultServiceConfiguration()
设置默认服务配置,以及我使用两个不同区域以某种方式弄乱一切的事实。它拒绝将事件记录到Mobile Analytics,或者它将使用错误消息中断上传
NSUnderlyingError = 0x17424ee20“操作无法完成。 (kCFErrorDomainCFNetwork错误-1001。)“, NSErrorFailingURLKey = https://s3.amazonaws.com/mybucket/my-image.jpeg})
我尝试像这样设置S3的转移管理器(并评论上面的setDefaultServiceConfiguration
):
var transferManager = AWSS3TransferManager(configuration: awsConfigurationForS3, identifier: "S3")
在上传过程中会导致以下错误:
“Error Domain = com.amazonaws.AWSS3ErrorDomain Code = 0 \”操作无法完成。 (com.amazonaws.AWSS3ErrorDomain错误0。)\“UserInfo = 0x1742e7e00 {HostId = sm4qLUbN2XfkNYefbSU4A95UXTwvNYEBz0 + bALqeFzlcXJ7rOGWnr1Yg + 8HoU4r2,Bucket = mybucket,Endpoint = mybucket.s3.amazonaws.com,Message =您要访问的存储桶必须被解决使用指定的端点。请将所有将来的请求发送到此端点。,Code = PermanentRedirect,RequestId = 568728899BC328B0}“
我还尝试将“mybucket.s3.amazonaws.com”作为存储桶名称传递,但这导致错误,说明找不到存储桶。
我真的不确定下一步该尝试什么。希望你们能给我一些关于如何分离这两个集成的指导,并让它们同时工作。
答案 0 :(得分:2)
问题是您将S3区域设置为USEast1
,但您的存储区位于EUWest1
。根据日志,您将USEast1
设置为以下代码段中的awsConfigurationForS3
对象:
var transferManager = AWSS3TransferManager(configuration: awsConfigurationForS3, identifier: "S3")
您应该将S3区域更新为EUWest1
(您也可以将Cognito Identity区域更新为EUWest1
)。请尝试以下代码段:
// You can use the new constructor with fewer arguments.
let credentialsProviderForS3 = AWSCognitoCredentialsProvider.credentialsWithRegionType(
AWSRegionType.EUWest1,
identityPoolId: "pool_id"
)
let awsConfigurationForS3 = AWSServiceConfiguration(
region: AWSRegionType.EUWest1,
credentialsProvider: credentialsProviderForS3
)
// You need to retain a strong reference to an instance of AWSS3TransferManager until `- upload:` finishes executing. Making it a property is one way to do this.
self.transferManager = AWSS3TransferManager(configuration: awsConfigurationForS3, identifier: "S3")
var fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory().stringByAppendingPathComponent("temp"))
var uploadRequest1 : AWSS3TransferManagerUploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest1.bucket = "mybucket"
uploadRequest1.key = "my-image.jpeg"
uploadRequest1.body = fileURL
// `- upload:` is an asynchronous request. This means you need to retain a strong reference to `transferManager` until the async call completes.
transferManager.upload(uploadRequest1)