2016-03-29 17 views
2

iOSアプリからAmazon S3に写真をアップロードしています。その写真の公開URLにアクセスする必要があります。 URLを手動で作成するのではなく、次の方法でURLを作成します。AWSS3オブジェクトへの参照を取得する

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 

私には2つの問題があります。

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プロパティを呼び出すしようとしています。

私はこれをやりたいと思っています。私はApp Delegateとここの両方で初期化コードを繰り返す必要はありません。だからApp Delegateで既に初期化されたオブジェクトへの参照を取得する方法があるなら、私は知りたいと思う。 https://docs.aws.amazon.com/AWSiOSSDK/latest/Classes/AWSS3.html#//api/name/registerS3WithConfiguration:forKey

答えて

0

-Rohan

私は実際にAWSS3.defaultS3()とS3オブジェクトのインスタンスを取得することができました。だから私はこのようにパブリックURLを構築することができました。

let publicURL = AWSS3.defaultS3().configuration.endpoint.URL.URLByAppendingPathComponent(uploadRequest.bucket!).URLByAppendingPathComponent(uploadRequest.key!) 
関連する問題