Upload a file via Nextcloud API using the nextcloud-link package

I would like to upload a file by code . I created a test directory at

https://cloud.foo.com/apps/files/?dir=/shared&fileid=200737

Because I’m using NodeJs I used the Nextcloud-Link package


So basically I use the webdav put as described here

With this code

import NextcloudClient from "nextcloud-link";
import { readFileSync } from "fs";

async function bootstrap() {
  try {
    const client: NextcloudClient = new NextcloudClient({
      url: "https://cloud.foo.com",
      username: "myusername",
      password: "mypassword"
    });

    await client.put("shared", readFileSync("./test.csv"));
  } catch (error) {
    console.log(error);
  }
}

bootstrap();

I get a Access to shared was denied error. So two questions come up:

When I do

await client.put("shared/test.csv", readFileSync("./test.csv"));

to create a new file if it doesn’t exist I get a shared/test.csv not found! error.

Any help would be appreciated :slightly_smiling_face:

async function bootstrap() {
  try {
    const client = new NextcloudClient({
      url: "http://nextcloud.test",
      username: "admin",
      password: "admin"
    });

    await client.checkConnectivity();

    await client.put("/test.txt", readFileSync("./test.txt"));
    console.dir(await client.getFiles('/'));
  } catch (error) {
    console.log(error);
  }
}

Works for me. Path has to include the file name. Also a / at the beginning seems to be required.

Thanks for your reply. Unfortunately this doesn’t work for me. When I do this

import NextcloudClient from "nextcloud-link";
import { readFileSync } from "fs";

async function bootstrap() {
  try {
    const client: NextcloudClient = new NextcloudClient({
      url: "https://cloud.company.com",
      username: "myusername",
      password: "mypassword"
    });
    await client.put("/test.csv", readFileSync("./test.csv"));
  } catch (error) {
    console.log(error);
  }
}
bootstrap();

I get the following error message

{ Error
at new r (C:\Users\mhermsen\Desktop\nc-client\node_modules\nextcloud-link\compiled\client.js:1:157224)
at t. (C:\Users\mhermsen\Desktop\nc-client\node_modules\nextcloud-link\compiled\client.js:1:130429)
at C:\Users\mhermsen\Desktop\nc-client\node_modules\nextcloud-link\compiled\client.js:1:129531
at Object.throw (C:\Users\mhermsen\Desktop\nc-client\node_modules\nextcloud-link\compiled\client.js:1:129636)
at s (C:\Users\mhermsen\Desktop\nc-client\node_modules\nextcloud-link\compiled\client.js:1:128433)
at process._tickCallback (internal/process/next_tick.js:68:7)
message: ‘/test.csv not found!’,
stack:
‘Error\n at new r (C:\Users\mhermsen\Desktop\nc-client\node_modules\nextcloud-link\compiled\client.js:1:157224)\n at t. (C:\Users\mhermsen\Desktop\nc-client\node_modules\nextcloud-link\compiled\client.js:1:130429)\n at C:\Users\mhermsen\Desktop\nc-client\node_modules\nextcloud-link\compiled\client.js:1:129531\n at Object.throw (C:\Users\mhermsen\Desktop\nc-client\node_modules\nextcloud-link\compiled\client.js:1:129636)\n at s (C:\Users\mhermsen\Desktop\nc-client\node_modules\nextcloud-link\compiled\client.js:1:128433)\n at process._tickCallback (internal/process/next_tick.js:68:7)’ }

When I replace the put call with

console.dir(await client.getFiles('/'));

or

console.dir(await client.getFiles(''));

I also get a not found error. The message I get is

‘/ not found!’