LoginSignup
1
1

More than 3 years have passed since last update.

WebDAVを使ってファイル一覧を取得する

Last updated at Posted at 2019-08-06

はじめに

WebDAVはhttpを利用したWebコンテンツにアクセスするプロトコルです。
npmのWebDAVを利用して、某有名企業のファイルストレージにアップロード済みのファイル一覧を取得するサンプルを作成したのでアップします。
npm WebDAVのサンプルコードが説明不足といいサンプルがないので参考までに。

前提

  • OS : Windows7以上
  • PoweShellのターミナルで実行
  • VSCodeでコード編集
  • node.js環境構築済み

npm WebDAVのインストール

npm install webdav --save 

サンプル

  • □と■は伏字。
  • npmのサンプルでは、
// Get directory contents
const directoryItems = await client.getDirectoryContents("/");

の例しかなく、awaitの使用方法については、asyncと組合せのfunctionで定義する。

定義しなければ、下記のような実行時エラー。

PS C:\Users\~> node webdavgetfiles.js
C:\Users\~\webdavgetfiles.js:17
    const directoryItems = await client.getDirectoryContents(path, { deep: true });
                                 ^^^^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
  • {deep: true}を追加して、指定ディレクトリのサブディレクトリのファイルも取得します。
  • typeが'file'を選択します。

webdavgetfiles.js

const { createClient } = require("webdav");

const client = createClient(
    "https://kfs.□□□□.ne.jp/",
    {
        username: "□□□□□□□□□□□□□",
        password: "■■■■■■■■■■■■■"
    }
);

const path = "/■■■■■■■■/■■■■■■■■■■/■■■■■■■■■■■/■■■■■■■■■■■■/";
/**
 *  webdevGetfile
 *   async ~ await
 */
async function webdevGetfile () {
    const directoryItems = await client.getDirectoryContents(path, { deep: true });

    for ( var i = 0; i < directoryItems.length ; i++ ) {
        if ( directoryItems[i].type == 'file') {
            console.log(directoryItems[i].filename);
        }
    }

}

webdevGetfile();

実行結果

※□は伏字、みずらいですが本番データなのでご容赦ください。

PS C:\Users\~> node webdavgetfiles.js
/□□□□□□□□□□□□/□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□/□□□□□□□□/□□□/□□□□□□□□.zip
/□□□□□□□□□□□□/□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□/□□□□□□□□/□□□/□□□□□□□□.zip
/□□□□□□□□□□□□/□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□/□□□□□□□□/□□□/□□□□□□□□.zip
/□□□□□□□□□□□□/□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□/□□□□□□□□/□□□/□□□□□□□□.zip
/□□□□□□□□□□□□/□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□/□□□□□□□□/□□□/□□□□□□□□.zip
/□□□□□□□□□□□□/□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□/□□□□□□□□/□□□/□□□□□□□□.zip
/□□□□□□□□□□□□/□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□/□□□□□□□□/□□□/□□□□□□□□.zip
/□□□□□□□□□□□□/□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□/□□□□□□□□/□□□/□□□□□□□□.zip
/□□□□□□□□□□□□/□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□/□□□□□□□□/□□□/□□□□□□□□.zip

まとめ

WebDAVを利用すれば、httpプロトコルでwebサーバからファイルを自動取得したり、自動アップロードができます。
色々な業務で応用可能と思います。ぜひ活用してみてください。

1
1
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
1
1