LoginSignup
0
0

More than 3 years have passed since last update.

Node.js で Synology の WebDAV にアクセスする

Posted at

ライブラリーのインストール

sudo npm install -g webdav

フォルダーの一覧

webdav_list.js
#! /usr/bin/node
//
//  webdav_list.js
//
//                      Mar/03/2021
// ---------------------------------------------------------------
const { createClient } = require("webdav")

const client = createClient(
    "https://example.synology.me:5006/",
    {
        username: "scott",
        password: "secret"
    }
)


// ---------------------------------------------------------------
async function webdevListfile (folder)
{
    const directoryItems = await client.getDirectoryContents(folder)

    console.log(directoryItems)
}

// ---------------------------------------------------------------
const folder = "/homes/scott/tmp"
webdevListfile(folder)
// ---------------------------------------------------------------

実行スクリプト

export NODE_PATH=/usr/lib/node_modules
#
./webdav_list.js

ファイルのアップロード

webdav_put.js
#! /usr/bin/node
//
//  webdav_put.js
//
//                      Mar/03/2021
// ---------------------------------------------------------------
var fs = require("fs")
// ---------------------------------------------------------------
const { createClient } = require("webdav");

const client = createClient(
    "https://example.synology.me:5006/",
    {
        username: "scott",
        password: "secret"
    }
);


// ---------------------------------------------------------------
async function webdevPutfile (file_in, file_out)
{
    const str = fs.readFileSync (file_in,'utf8')

    await client.putFileContents(file_out, str)
}

// ---------------------------------------------------------------
const file_in = "./tmp01.txt"
const file_out = "/homes/scott/tmp/tmp01.txt"

webdevPutfile(file_in,file_out)
// ---------------------------------------------------------------

実行スクリプト

export NODE_PATH=/usr/lib/node_modules
#
./webdav_put.js

ファイルのダウンロード

webdav_get.js
#! /usr/bin/node
//
//  webdav_get.js
//
//                      Mar/03/2021
// ---------------------------------------------------------------
var fs = require("fs")
// ---------------------------------------------------------------
const { createClient } = require("webdav");

const client = createClient(
    "https://example.synology.me:5006/",
    {
        username: "scott",
        password: "secret"
    }
);


// ---------------------------------------------------------------
async function webdevGetfile (file_in, file_out)
{
    const strx = await client.getFileContents(file_in, { format: "text" });

    fs.writeFile (file_out,strx, function (err)
        {
        if (err) {
            console.log("Error on write: " + err)
        } else {
            console.log("File written.")
            }
        })

}

// ---------------------------------------------------------------
const file_in = "/homes/scott/tmp/tmp02.txt"
const file_out = "./tmp02.txt"

webdevGetfile(file_in,file_out)
// ---------------------------------------------------------------

実行スクリプト

export NODE_PATH=/usr/lib/node_modules
#
./webdav_get.js
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0