Search code examples
node.jsapiowncloudocs

How to create folder and upload files using the OCS owncloud API


I am developing an app that use an owncloud OCS API.

I used the Owncloud REST API for manage users and now I want to manage the files of each user. I am using nodejs. Is an API for manage files like manage users one?

What I found was the API URL for shared files:

owncloud/ocs/v1.php/apps/files_sharing/api/v1/shares

This returns only the shared files. I want one for the all the files

Thanks.


Solution

  • Using CURL command we use:

    • curl -X MKCOL <folder_URL> for create a folder
    • curl -X PUT <folder_URL>/<file_name> --data-binary @<file_location_in_pc> for upload a file.

    This is an example of the code:

    /**
       * Upload a file to an user folder
       * @param userId
       * @param fileName
       * @param fileLocation
       * @param callback
       */
      function fnUploadDocument(userId, fileName, fileLocation, callback) {
        var json = {
          done: false
        }
        var command = 'curl -X PUT "'
        command += srv.ownclouddirUtil.getUrlUser()
        command += srv.h3apifolder + userId + '/'
        command += fileName + '"'
        command += ' --data-binary @"' + fileLocation + '"'
        console.log('Command--: ', command)
        srv.fileSystemService.runScript(command, function (stdout, error, stderr) {
          if (error === null) {
            json.done = true
            callback(json)
          } else {
            json.error = error
            json.stderr = stderr
            callback(json)
          }
        })
      }