Search code examples
amazon-s3cordova-plugins

PhoneGapp FileTransfer Upload to S3 Returning Code=3 But Working to Heroku


I am trying to upload an image from my ionic app to Amazon's S3 storage in an Ionic app directly via the Cordova FileTransfer plugin. The upload to my own Heroku server works, but the upload to the Amazon S3 server always fails with code=3 (Connection Error).

I am initiating the file transfer with this command:

ft.upload(imageURI, s3Config.url, succ, fail, options, true);

And following options:

ImageURI: cdvfile://localhost/temporary/cdv_photo_044.jpg
Upload URI: https://8d90n90q.s3.amazonaws.com
Options:    {
  "fileKey": "file",
  "fileName": "rndcdv_photo_044.jpg",
  "mimeType": "image/jpeg",
  "params": {
    "key": "cdv_photo_044.jpg",
    "AWSAccessKeyId": "AKIAIL6Q62UVPOESBLQA",
    "acl": "public-read",
    "policy": "eyJleHBpcmF0aW9uIjoiMjAxNS0xMS0wNFQwMDowMTo0MC4xMDhaIiwiY29uZGl0aW9ucyI6W1sic3RhcnRzLXdpdGgiLCIka2V5IiwiIl0sWyJzdGFydHMtd2l0aCIsIiRDb250ZW50LVR5cGUiLCIiXSx7ImJ1Y2tldCI6IjhkOTBuOTBxIn0seyJhY2wiOiJwdWJsaWMtcmVhZCJ9XX0=",
    "signature": "BILWcf3I7IMjAXdTm0nh4Md81YI=",
    "Content-Type": "image/jpeg"
  },
  "headers": {
    "Connection": "close"
  },
  "httpMethod": "POST",
  "chunkedMode": false
}

Things tried:

  • I have validated that upload to s3 works by using curl.
  • I have enabled CORS from all hosts on S3.
  • Tried suggested solutions: chunkedMode & headers(connection close).

I have setup an example repository with the ionic app to try: https://github.com/benmarten/ionic-s3-upload-test You can toggle between line 47 and 49 to see the difference (not working and working). Please help!


Solution

  • I actually found the solution myself. With iOS9 there was a new policy mechanism introduced, hence one needs to update the ionic/phonegapp config.xml file:

    <access origin="*"/>
    
    <platform name="ios">
      <config-file platform="ios" target="*-Info.plist" parent="NSAppTransportSecurity">
        <dict>
          <key>NSAllowsArbitraryLoads</key>
          <true/>
        </dict>
      </config-file>
    </platform>
    

    To automatically populate these configurations to the iOS/Android configuration files, you can use this hook: https://gist.github.com/marcocarnazzo/6f01a57d390e8fe3071f