1

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

在上传过程中导致以下错误:

"Error Domain=com.amazonaws.AWSS3ErrorDomain Code=0 \"操作无法\U2019t完成。(com.amazonaws.AWSS3ErrorDomain 错误 0。)\" UserInfo=0x1742e7e00 {HostId=sm4qLUbN2XfkNYefbSU4A95UXTwvNYEBz0+bALqeFzlcXJ7rOGWnr1Yg+8HoU4r2, Bucket=mybucket, Endpoint=mybucket.s3.amazonaws.com, Message=必须访问您尝试访问的存储桶使用指定的端点。请将所有未来的请求发送到此端点。代码=永久重定向,请求 ID=568728899BC328B0}"

我还尝试将“mybucket.s3.amazonaws.com”作为存储桶名称传递,但这导致错误提示找不到存储桶。

我真的不确定下一步该尝试什么。希望你们能给我一些关于如何分离这两个集成的指示,并让两者同时工作。

4

1 回答 1

3

问题是您将 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)
于 2015-02-25T20:01:35.347 回答