AWS iOS SDK:在两个不同的地区同时使用S3和Mobile Analytics



我正在尝试在我的Swift项目中使用AWS iOS SDK v2集成S3和Mobile Analytics。问题是,我试图使用两个不同的地区(我正在与我的主要地区,爱尔兰的eu-west-1合作,但移动分析只能从弗吉尼亚州的us-east-1获得)。

我在我的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")

这导致上传过程中出现以下错误:

"错误域=com.amazonaws.AWS3ErrorDomain Code=0\"操作无法完成。(com.amazonaws.AWS3ErrorDomain error 0。)\"UserInfo=0x1742e7e00{HostId=sm4qLUbN2XfkNYefbS4A95UXTwvNYEBz0+bALqeFzlcXJ7rOGWnr1Yg+8HoU4r2,Bucket=mybucket,Endpoint=mybucket.s3.amazonaws.com,Message=您试图访问的存储桶必须使用指定的终结点进行寻址。请将所有未来的请求发送到此终结点。,Code=PermanentRedirect,RequestId=568728899BC328B0}"

我还尝试将"mybucket.s3.amazonaws.com"作为bucket名称,但这导致了一个错误,说明找不到bucket。

我真的不知道下一步该做什么。希望你们能给我一些关于如何分离这两个集成的建议,并使两者同时工作。

问题是您将S3区域设置为USEast1,但您的bucket位于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)

相关内容

  • 没有找到相关文章

最新更新