Rohan
Rohan

Reputation: 768

Upload file to AWS S3 bucket

I have setup S3 bucket in US West i.e N. California

My Pool-ID start with "us-east-1:*****"

I have following code to upload file on my bucket named AWS_BUCKET_NAME

let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USWest1, identityPoolId: AWS_IDENTITY_POOL_ID)
    let configuration = AWSServiceConfiguration(region: .USWest1, credentialsProvider: credentialProvider)
    AWSServiceManager.default().defaultServiceConfiguration = configuration
    let uploadingFileURL = videoURL
    let uploadRequest = AWSS3TransferManagerUploadRequest()
    uploadRequest?.bucket = AWS_BUCKET_NAME
    uploadRequest?.key = fileName
    uploadRequest?.body = uploadingFileURL
    uploadRequest?.contentType = "video/mp4"

    let transferManager = AWSS3TransferManager.default()
    transferManager.upload(uploadRequest!).continueWith(executor: AWSExecutor.mainThread())
    { (task) -> Any? in
        if task.error == nil
        {
            return nil
        }
        else
        {
            print("Error: \(String(describing: task.error))")
        }
        return nil
    }

Mostly it successfully upload the video files. But sometimes its giving error like bellow

Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={NSUnderlyingError=0x608001a53c50 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_kCFStreamErrorCodeKey=8, kCFStreamErrorDomainKey=12}}, NSErrorFailingURLStringKey=https://cognito-identity.us-west-1.amazonaws.com/, NSErrorFailingURLKey=https://cognito-identity.us-west-1.amazonaws.com/, kCFStreamErrorDomainKey=12, _kCFStreamErrorCodeKey=8, NSLocalizedDescription=A server with the specified hostname could not be found.}

Now if i change the regionType in AWSCognitoCredentialsProvider to USEast1 or some other and try again then its again give me same error but after that if i set "USWest1" region again then its working again. It will continue to work successful for 5-6 times or sometimes even more. But again after sometime it generating same error.

One more thing, with same pool id and region, Its working fine in android. only facing this issue in iOS.

Thanks in advance.

Upvotes: 2

Views: 1178

Answers (1)

Rohan
Rohan

Reputation: 768

Finally i found the solution for this. This is not proper answer but it may help someone else who are facing such issue.

let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: AWS_IDENTITY_POOL_ID)
    let configuration = AWSServiceConfiguration(region: .USWest1, credentialsProvider: credentialProvider)

After Michael - sqlbot's comment, i have checked by changing Region one after another. Finally i am creating AWSCognitoCredentialsProvider with .USEast1 and configuring AWSServiceConfiguration with .USWest1 and it is working now.

I don't know the reason behind but probably we need to configure AWSServiceConfiguration with the region of bucket we are using.

Upvotes: 1

Related Questions