【问题标题】:Get a reference to the AWSS3 object获取对 AWSS3 对象的引用
【发布时间】:2016-05-20 02:24:55
【问题描述】:

我正在将照片从我的 iOS 应用程序成功上传到 Amazon S3。我需要获取该照片的可公开访问的 URL。我没有手动构建 URL,而是使用以下方式来执行此操作。

let transferManager = AWSS3TransferManager.defaultS3TransferManager()
transferManager.upload(uploadRequest).continueWithBlock { task in
    if let error = task.error {
        print("Upload failed: \(error.code) - \(error.localizedDescription)")
    }
    if let exception = task.exception {
        print("Upload failed: \(exception)")
    }

    if task.result != nil {
        print("Successfully uploaded!")

        let credentialsProvider = AWSCognitoCredentialsProvider(regionType: CognitoRegionType, identityPoolId: CognitoIdentityPoolId)
        let configuration = AWSServiceConfiguration(region: DefaultServiceRegionType, credentialsProvider:credentialsProvider)
        let aws3 = AWSS3(configuration: configuration)
        let publicURL = aws3.configuration.endpoint.URL.URLByAppendingPathComponent(uploadRequest.bucket!).URLByAppendingPathComponent(uploadRequest.key!)
        print(publicURL)
    }

    return nil
}

这很好用,我得到了正确的公共 URL。

https://s3-ap-northeast-1.amazonaws.com/myapp/DAEF70E9-495A-40B4-B853-3B337486185D-4988-00000E22AB8E25A6.jpg

我有两个问题。

1)。以这种方式初始化 AWSS3(configuration: configuration) 现在已弃用。

2)。这在初始化代码时已经发生在 App Delegate 的 didFinishLaunchingWithOptions 方法中。

let credentialsProvider = AWSCognitoCredentialsProvider(regionType: CognitoRegionType, identityPoolId: CognitoIdentityPoolId)
let configuration = AWSServiceConfiguration(region: DefaultServiceRegionType, credentialsProvider:credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

但是,尝试从此配置调用 endpoint 属性返回 nil。

所以我想做的是这个。我不想在 App Delegate 和这里​​重复初始化代码。因此,如果有办法在 App Delegate 中获取对已初始化对象的引用,我很想知道。

【问题讨论】:

    标签: ios swift amazon-web-services amazon-s3


    【解决方案1】:

    我认为您可以使用以下 API:https://docs.aws.amazon.com/AWSiOSSDK/latest/Classes/AWSS3.html#//api/name/registerS3WithConfiguration:forKey:

    SDK 会为您保存对象,并且始终可以使用 S3ForKey 获取它:此处提到:https://docs.aws.amazon.com/AWSiOSSDK/latest/Classes/AWSS3.html#//api/name/S3ForKey:

    API 参考中有代码 sn-ps 演示了用法。

    -罗汉

    【讨论】:

      【解决方案2】:

      我实际上能够使用AWSS3.defaultS3() 获取 S3 对象的实例。所以我可以像这样构建公共 URL。

      let publicURL = AWSS3.defaultS3().configuration.endpoint.URL.URLByAppendingPathComponent(uploadRequest.bucket!).URLByAppendingPathComponent(uploadRequest.key!)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-13
        • 2010-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        相关资源
        最近更新 更多