简体   繁体   English

如何使用iOS SDK在运行时更改AWS的区域?

[英]How to change the region of AWS at runtime using iOS sdk?

Thanks in advance. 提前致谢。 I'm planning to release an app in different countries, where the users will be given an option to select the country and based on the selection their respective aws region will be used. 我计划在不同的国家/地区发布应用程序,在该国家/地区,用户可以选择国家/地区,然后根据选择使用各自的AWS区域。

First time login works fine, where the user selects the region and S3 upload works fine. 首次登录可以正常工作,用户可以选择区域,S3上传可以正常工作。

But If the user signs out and then another user logs in with different region, then the s3 upload fails with time out error. 但是,如果用户注销,然后另一个用户使用其他区域登录,则s3上传将失败,并显示超时错误。

Here is the code: 这是代码:

guard let config = AWSServiceConfiguration(region: regionTypeByString(regionString: userData.getS3RegionBasedOnSelection()), credentialsProvider: self.awsAuthenticationProvider) else { return }
AWSS3TransferUtility.register(with: config, forKey: "Transfer")

AWSS3TransferUtility.s3TransferUtility(forKey: "Transfer")?.uploadFile(
                fileUrl,
                bucket: bucketPath,
                key: key,
                contentType: "application/octet-stream",
                expression: expression,
                completionHandler: { (task, error) in

                    if error != nil {
                        DDLogInfo("!!!!!! Error RAW: \(String(describing: error))")
                        DDLogInfo("!!!!!! Error Code: \(error!.code)")
                        DDLogInfo("!!!!!! Error Description: \(error!.localizedDescription)")
                    }
                })

What's the proper way to change/switch s3 region ? 更改/切换s3区域的正确方法是什么?

For each region create a config and register it under respective keys. 为每个区域创建一个配置,并在相应的键下进行注册。 And do the same while initializing the AWSS3. 并在初始化AWSS3时执行相同的操作。

eg: 例如:

region1: 区域1:

let config = AWSServiceConfiguration(region: .USWest1, credentialsProvider: getAwsAuthenticationProvider())
AWSS3.register(with: config, forKey: "USWest1")
AWSS3TransferUtility.register(with: config, forKey: "USWest1")
//--> later in code
    let transferUtility = AWSS3TransferUtility.s3TransferUtility(forKey: "USWest1")
    transferUtility.uploadFile ()

region2: 区域2:

let config = AWSServiceConfiguration(region: .USWest2, credentialsProvider: getAwsAuthenticationProvider())
AWSS3.register(with: config, forKey: "USWest2")
AWSS3TransferUtility.register(with: config, forKey: "USWest2")
//--> later in code
     let transferUtility = AWSS3TransferUtility.s3TransferUtility(forKey: "USWest2")
     transferUtility.uploadFile ()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM