Box Developerドキュメント

アーキテクチャスケルトンの作成

ガイド ユーザー ユーザーのプロビジョニング アーキテクチャスケルトンの作成

アーキテクチャスケルトンの作成

最初の要件は、アカウントの作成時に一般的なファイルとフォルダを個々のユーザーのルートフォルダにコピーすることです。この問題は、標準のLinuxディストリビューション内の/etc/skelと呼ばれるディレクトリを介して解決されています。このディレクトリは、Box固有のソリューションによってエミュレートされます。Linuxで新しいユーザーを追加すると、/etc/skel内のファイルとフォルダが新しいユーザーのホームディレクトリにコピーされます。

JWTベースのBoxアプリケーションを作成すると、Box Enterprise内にサービスアカウントが作成されます。サービスアカウントはBox Enterpriseの共同管理者に似た機能を持っており、このユースケースに最も有用で、ファイルおよびフォルダに対し、所有、コピー、および他のユーザーとのコラボレーションを行うことができます。さらに重要な点は、ユーザー向けのカスタムアプリケーションの開発では厳密にはサービスアカウントを使用する必要がないことです。代わりに、自動化機能でさらにサービスアカウントを活用できます。

Box Platformアプリケーションの要件

この手順のためにJWTベースのカスタムBoxアプリケーションを作成する場合、ユーザーを管理するグループを管理するユーザーとして操作を実行、およびユーザーアクセストークンを生成スコープを有効にする必要があります。

JWTベースのBoxアプリケーションとBoxアプリケーションのスコープの作成の詳細については、JWTアプリケーションの設定を参照してください。

初めに、etcフォルダとskelフォルダを作成し、サービスアカウントにフォルダの所有権を付与します。

etc_skel.json
{
  "name": "etc",
  "parent": {
    "id": "0"
  },
  "children": [
    {
      "name": "skel",
      "children": []
    }
  ]
}

このコードを再利用して、上のJSONオブジェクトが示すフォーマットのフォルダ構造を構築することもできます。

Node
"use strict";
const fs = require("fs");
const box = require("box-node-sdk");

class BoxFolderTreeCreator {
    constructor(boxClient, options) {
        options = options || {};
        if (options.boxClient) {
            throw new Error("Must include a boxClient field.");
        }

        options.boxFolderTreeName = options.boxFolderTreeName || "tree.json";

        this.boxClient = boxClient;
        this.boxFolderTree = JSON.parse(fs.readFileSync(options.boxFolderTreeName));
        this.createdBoxFolders = [];
    }

    async createFolderTree(branch = null, parentFolderId = "0") {
        this.createdBoxFolders = [];
        if (Array.isArray(this.boxFolderTree)) {
            let folderTasks = [];
            this.boxFolderTree.forEach(folder => {
                folderTasks.push(this._createFolder(folder, ""));
            });
            await Promise.all(folderTasks);
            return this.createdBoxFolders;
        } else if (typeof this.boxFolderTree === "object") {
            console.log("Is object");
            await this._createFolders(this.boxFolderTree, "");
            return this.createdBoxFolders;
        } else {
            throw new Error("Incorrectly formatted JSON folder tree.");
        }
    }

    async _createFolders(branch, parentFolderId = "0") {
        if (branch.parent != null && branch.parent.id != null) {
            parentFolderId = branch.parent.id;
        }
        let folder;
        try {
            folder = await this.boxClient.folders.create(parentFolderId, branch.name);
        } catch (e) {
            let existingFolderId = BoxFolderTreeCreator.handleFolderConflictError(e);
            folder = await this.boxClient.folders.get(existingFolderId);
        }
        this.createdBoxFolders.push(folder);
        if (branch.children.length <= 0) {
            console.log("No more folders to create...");
            return;
        } else {
            let createFolderTasks = [];
            branch.children.forEach(child => {
                console.log("Creating folder...");
                console.log(child.name);
                createFolderTasks.push(this._createFolders(child, folder.id));
            });
            return await Promise.all(createFolderTasks);
        }
    }

    static handleFolderConflictError(e) {
        if (e && e.response && e.response.body) {
            let errorBody = e.response.body;
            if (errorBody.status === 409) {
                if (
                    errorBody.context_info &&
                    errorBody.context_info.conflicts &&
                    errorBody.context_info.conflicts.length > 0
                ) {
                    let conflict = errorBody.context_info.conflicts[0];
                    if (conflict && conflict.id) {
                        return conflict.id;
                    }
                }
            }
        }
    }
}

let configFile = fs.readFileSync("config.json");
configFile = JSON.parse(configFile);

let session = box.getPreconfiguredInstance(configFile);
let serviceAccountClient = session.getAppAuthClient("enterprise");

let treeCreator = new BoxFolderTreeCreator(serviceAccountClient);

(async () => {
    let createdFolders = await treeCreator.createFolderTree();
    console.log(createdFolders);
})();