0

I am getting "Code 3" (connection error) error when trying to upload an image file from my Cordova / Phonegap 3.2 app to Amazon S3 using FileTransfer and in Android 4.3 version platform.

 options.fileKey = "file";
    options.fileName = filename_for_s3;
    options.mimeType = "image/jpeg";
    options.chunkedMode = false;
    options.headers = {
        Connection: "close"
    }

ft.upload(imageURI, encodeURI("https://" + data.Bucket + ".s3.amazonaws.com/"), success, fail, options);

The code works on iOS7 and older Android versions (tested on Android 3.2 and 2.3), but my problem is with version 4.3. I assume that the problem is not with the code itself, as it works with other version, but might be the Phonegap library itself.

There might be a workaround but I am not familiar with it. I tried many options posted on what it seems like a similar problem, like changing the chunkedMode to false, without encoding the URI, adding "Connection: 'close'" header but it still doesn't work. Others suggested that this might be an issue with the google analytics plugin that causes it, so I removed it and I still get the same error. I also tried the answers in this question without success.

I also want to add that I am generating a signature and policy on the server.

  options.params = {

     "key": "uploads/" + filename_for_s3,
     "AWSAccessKeyId": data.AWSAccessKey,
     'success_action_status': '201',
     "acl": "private",
     "policy": data.Policy,
     "signature": data.Signature

                };

Cors permissions are also set as needed:

 <?xml version="1.0" encoding="UTF-8"?>
 <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <ExposeHeader>ETag</ExposeHeader>
    <ExposeHeader>x-amz-meta-custom-header</ExposeHeader>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>

Update: I also checked the Amazon S3 logs, but there isn't any line related to an upload.

I've spent a few days trying to figure it out without any success. I hope that you can help me solve this issue. Thanks.

It seems like this is a bug in Cordova 3.2 (see my post here). I was suggested to use the latest FileTransfer library, and also updated the File library, however I get an error when compiling.

I need help to solve this issue.

Update: updating the FileTransfer, File and Capture plugins to the newest version didn't solve the problem, in fact, now it throws Code 3 on iOS7 too - didn't happen before.

Community
  • 1
  • 1
Liron Harel
  • 10,819
  • 26
  • 118
  • 217
  • For your `filekey`, have you tried putting `document`? Also, make sure that you can upload publicly to S3 that way. I've used S3 a little, but from what I remember you usually have to pass some sort of access key and secret in the URL. – Andrew Lively Mar 17 '14 at 12:19
  • I will try the document. I also use signature and policy that works on other platforms (updated the question with the params I am passing). Thanks. – Liron Harel Mar 17 '14 at 13:00
  • @AndrewLively changing to document type didn't help. – Liron Harel Mar 17 '14 at 14:01
  • i'm not familiar with Phonegap, but are you sure that you are calling (the upload function) inside an Asynctask, or using another thread to call upload function inside phonegap. – AbuQauod Mar 26 '14 at 10:24
  • i found this if this could help you: http://simonmacdonald.blogspot.com/2012/04/sorry-for-being-gone-so-long-vacation.html – AbuQauod Mar 26 '14 at 10:26

3 Answers3

1

Have you tried ft.upload(imageURI,"https://" + data.Bucket + ".s3.amazonaws.com/", success, fail, options); ? Looks like you are URL encoding the :// parts of the url.

Chris Gunawardena
  • 6,246
  • 1
  • 29
  • 45
  • Yep, I tried without encoding the URL. As I mentioned, it works on android 2,3 iOS7 without an issue even with encoded URL, the only issue is in 4.3 that I tried, probably in other 4.xx versions. – Liron Harel Mar 24 '14 at 11:07
  • You probably seen this already, but just in case: http://grandiz.com/phonegap-development/phonegap-file-transfer-error-code-3-solved/ – Chris Gunawardena Mar 24 '14 at 11:31
  • I know everything points to a Cordova bug, but have you tried a smaller/different image, different bucket or even a different upload location (like a test php file on your server)? – Chris Gunawardena Mar 24 '14 at 11:43
1

kindly try this

ft.upload(imageURI, encodeURI("https://" + data.Bucket + ".s3.amazonaws.com/"), success, fail, options, true);

I have added true at the last. Because its a accepts all certificates. By defaults it will reject self signed certificates. Now you are using https. I hope this works

Bastin Robin
  • 907
  • 16
  • 30
0

I also got the error code 3 when I tried to download a pdf-file. I also tried to pass Connection: 'close', update plugins etc. - nothing worked. Eventually I just started to use a XMLHttpRequest in combination with cordova-plugin-file:

window.resolveLocalFileSystemURL(cordova.file.externalCacheDirectory, function(entry) {
    entry.getFile('myfile.pdf', { create: true, exclusive: false }, function(fileEntry) {
        downloadFile(fileEntry, url);
    }, onCreateFileError);
}, onResolveError);

function downloadFile(fileEntry, url) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'blob';
    xhr.onload = function (e) {
        fileEntry.createWriter(function(fileWriter) {
            fileWriter.write(new Blob([xhr.response]));
        });
    };
    xhr.send();
}

This code is for downloading, but I think the same approach can be applied for uploading.

pto3
  • 513
  • 1
  • 5
  • 13