Search code examples
rcurlrcurl

Unable to create folder with RCurl


I'm having trouble using the ftpUpload() function of RCurl to upload a file to a non-existent folder in an SFTP. I want the folder to be made if its not there, using the ftp.create.missing.dirs option. Here's my code currently:

.opts <- list(ftp.create.missing.dirs=TRUE)

ftpUpload(what = "test.txt", 
to "sftp://ftp.testserver.com:22/newFolder/existingfile.txt", 
userpwd = paste(user, pwd, sep = ":"), .opts = opts)`

It doesn't seem to be working as I get the following error:

* Initialized password authentication
* Authentication complete
* Failed to close libssh2 file

I can upload a file to an existent folder with success, its just when the folder isn't there I get the error.


Solution

  • The problem seems be due the fact you are trying to create the new folder, as seen in this question: Create an remote directory using SFTP / RCurl

    The error can be found in Microsoft R Open git page:

    case SSH_SFTP_CLOSE:
      if(sshc->sftp_handle) {
        rc = libssh2_sftp_close(sshc->sftp_handle);
        if(rc == LIBSSH2_ERROR_EAGAIN) {
          break;
        }
        else if(rc < 0) {
          infof(data, "Failed to close libssh2 file\n");
        }
        sshc->sftp_handle = NULL;
      }
      if(sftp_scp)
        Curl_safefree(sftp_scp->path);
    

    In the code the parameter rc is related to libssh2_sftp_close function (more info here https://www.libssh2.org/libssh2_sftp_close_handle.html), that tries close the nonexistent directory, resulting in the error.

    Try use curlPerform as:

    curlPerform(url="ftp.xxx.xxx.xxx.xxx/";, postquote="MkDir /newFolder/", userpwd="user:pass")