4

I am trying to integrate S3 upload to upload a video file and tried Developer authenticated Identity method. Everything is configured as per the aws docs says.

DeveloperAuthenticatedIdentityProvider Class :

class DeveloperAuthenticatedIdentityProvider : AWSCognitoCredentialsProviderHelper {
    override func token() -> AWSTask<NSString> {
    //return AWSTask //with token and will set identityId
}

and then

let devAuth = DeveloperAuthenticatedIdentityProvider(regionType: COGNITO_REGION, identityPoolId: COGNITO_POOL_ID, useEnhancedFlow: true, identityProviderManager:nil)
let credentialsProvider = 
AWSCognitoCredentialsProvider(regionType: COGNITO_REGION, identityProvider:devAuth)

let configuration = 
AWSServiceConfiguration(region: S3_REGION, credentialsProvider:credentialsProvider)

AWSServiceManager.default().defaultServiceConfiguration = configuration

after configuring these things tried to upload using AWSS3TransferManager

let transferManager = AWSS3TransferManager.default()

let uploadingFileURL = URL(fileURLWithPath: "your/file/path/myTestFile.txt")

let uploadRequest = AWSS3TransferManagerUploadRequest()

uploadRequest.bucket = "myBucket"
uploadRequest.key = "myTestFile.txt"
uploadRequest.body = uploadingFileURL
transferManager.upload(uploadRequest).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in

    if let error = task.error as? NSError {
        if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) {
            switch code {
            case .cancelled, .paused:
                break
            default:
                print("Error uploading: \(uploadRequest.key) Error: \(error)")
            }
        } else {
            print("Error uploading: \(uploadRequest.key) Error: \(error)")
        }
        return nil
    }

    let uploadOutput = task.result
    print("Upload complete for: \(uploadRequest.key)")
    return nil
})

Whenever I call Upload method it shows

[Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=8 "(null)" UserInfo={__type=NotAuthorizedException, message=Unauthenticated access is not supported for this identity pool.}]

also DeveloperAuthenticatedIdentityProvider not getting fired

kindly please help.

Community
  • 1
  • 1
LeNI
  • 1,184
  • 2
  • 10
  • 19
  • did you go through this link https://stackoverflow.com/questions/40524324/aws-how-to-properly-authenticate-a-user-against-cognito-pool-and-use-it-for-cog – manukv Dec 14 '17 at 11:01

1 Answers1

6

When you using Developer authenticated identity for cognito identity provider you need not use AWSS3TransferManager.default() You need to register the AWSServiceConfiguration to the AWSS3TransferManager with a key.

AWSS3TransferManager.register(with: configuration!, forKey: "KEY")

Try this way:

let devAuth = DeveloperAuthenticatedIdentityProvider(regionType:  COGNITO_REGION, identityPoolId: COGNITO_POOL_ID, useEnhancedFlow: true, identityProviderManager:nil)
    let credentialsProvider = AWSCognitoCredentialsProvider(regionType: COGNITO_REGION, identityProvider:devAuth)
    let configuration = AWSServiceConfiguration(region: S3_REGION, credentialsProvider:credentialsProvider)

    AWSS3TransferManager.register(with: configuration!, forKey: "YOUR_KEY")

//Start Upload

let uploadRequest = AWSS3TransferManagerUploadRequest()

 //Set all properties to uploadRequest

    AWSS3TransferManager.s3TransferManager(forKey: "YOUR_KEY").upload(uploadRequest!).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in
                // Do something with the response

                if task.isCancelled {
                    print("Cancelled Upload")

                }
                else if (task.error != nil) {

                    print("Upload error --> \(task.error)")
                }else{

                    print("Upload success!!! Be happy :)")

                }
                return task
            })

Just try, I think it may work.

manukv
  • 2,031
  • 18
  • 30