2

Im trying to upload image to Aws S3 bucket. I tried to follow a tutorial and I'm getting a error saying "Returning ENOTCONN because protocol has not yet been set up." I'm new to swift and I'm not able to understand why the error is occurring also.My code for S3 upload is as follows:

 let uploadRequest = AWSS3TransferManagerUploadRequest()
 uploadRequest?.body = url!
 uploadRequest?.key = remoteFileName
 uploadRequest?.bucket = S3BucketName
 uploadRequest?.contentType = "image/" + ext

 let transferManager = AWSS3TransferManager.default()

 // Perform Upload
 transferManager.upload(uploadRequest!).continueWith(block: { (task:AWSTask<AnyObject>) -> AnyObject! in

      if let error = task.error{
                print("error \(error.localizedDescription)")
            }

      if task.result != nil {
            let url = AWSS3.default().configuration.endpoint.url
            let publicURL = url?.appendingPathComponent((uploadRequest?.bucket!)!).appendingPathComponent((uploadRequest?.key!)!)
               print("Uploaded to:\(publicURL)")
            }
             return nil
 })

My S3 is in ap-south-1 and cognito pool id in us-west-2. I guess thats creating the problem.Is there a way to fix the issue without creating another bucket in us-west-2. I get the following error:

error

  • Do you have appropriate policy set up on your S3 bucket? – Anushk Nov 22 '17 at 05:03
  • Yes.My bucket policy is as follows:{ "Version": "2008-10-17", "Statement": [ { "Sid": "AddPerm", "Effect": "Allow", "Principal": "*", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::sohtest/*" } ] } – Geethanjali Reddy Nov 22 '17 at 05:06
  • Did you set the cognito pool ID and region correctly in Info.Plist? I guess you have used the code in AWS github demo. – rajtharan-g Jan 11 '18 at 04:52

1 Answers1

0

You want the bucket policy to be somewhat like this if the cognito pool is not set up for authentication: Notice Principal and Action values Bucket Policy

Also, is there any particular reason you're using AWSS3TransferManagerUploadRequest? If the policy doesn't resolve your issue, you can use the following code for AWSS3TransferUtilityUploadExpression which sends your data in chunks asynchronously.

   let expression = AWSS3TransferUtilityUploadExpression()

    expression.progressBlock = progressBlock

    transferUtility.uploadData(UIImagePNGRepresentation(imageNew!)!,
                               bucket: "bucket-name",
                               key: (imgName.removeWhitespace()),
                               contentType: "image/png",
                               expression: expression,
                               completionHandler: completionHandler).continueWith { (task) -> AnyObject! in
                                                if let error = task.error {
                                                    print("Error: \(error.localizedDescription)")
                                                }

                                                if let _ = task.result {
                                                    print("Upload Starting!")
                                                    // Do something with uploadTask.
                                                }

                                                return nil;
                }
Anushk
  • 482
  • 4
  • 20