Box: フォルダ移動

Box: フォルダ移動

Box: Move Folder

この工程は、既存フォルダを指定フォルダに移動します。フォルダの ID・URL は変化しません。

Basic Configs
工程名
メモ
Auto Step icon
Configs for this Auto Step
conf_OAuth2
C1: OAuth2 設定 *
conf_FolderId
C2: 移動させるフォルダの ID *
conf_ParentFolderId
C3: 移動先フォルダの ID *

Notes

  • フォルダ ID は、URL に含まれています https://{sub-domain}.app.box.com/folder/(Folder ID)
  • フォルダ名が競合(コンフリクト)する場合はエラーとなります
  • Box のリフレッシュトークンには、期限があります

Capture

See Also

Script (click to open)
  • 次のスクリプトが記述されている XML ファイルをダウンロードできます
    • box-folder-move.xml (C) Questetra, Inc. (MIT License)
    • Professional のワークフロー基盤では、ファイル内容を改変しオリジナルのアドオン自動工程として活用できます


const main = () => {
    //// == Config Retrieving / 工程コンフィグの参照 ==
    const oauth2 = configs.getObject("conf_OAuth2");
    const folderId = decideFolderId();
    const parentFolderId = decideParentFolderId();
    if (folderId === parentFolderId) {
        throw new Error('Folder cannot be moved to itself.');
    }

    //// == Calculating / 演算 ==
    moveFolder(oauth2, folderId, parentFolderId);
};

/**
  * 移動させるフォルダの ID を config から読み出す
  * 空の場合はエラー
  * @return {String}  移動させるフォルダの ID
  */
const decideFolderId = () => {
    const folderIdDef = configs.getObject("conf_FolderId");
    const folderId = engine.findData(folderIdDef);
    if (folderId === null) {
        throw new Error("Folder ID to move is blank.");
    }
    if (folderId === '0') {
        throw new Error('Root folder cannot be moved.');
    }
    return folderId;
};

/**
  * 移動先フォルダの ID を config から読み出す
  * 空の場合はエラー
  * @return {String}  移動先フォルダの ID
  */
const decideParentFolderId = () => {
    let folderId = "";
    const folderIdDef = configs.getObject("conf_ParentFolderId");
    if (folderIdDef === null) {
        folderId = configs.get("conf_ParentFolderId");
    } else {
        folderId = engine.findData(folderIdDef);
    }
    if (folderId === "" || folderId === null) {
        throw new Error("Destination Folder ID is blank.");
    }
    return folderId;
};

/**
  * Box 上のファイルを指定フォルダに移動する。
  * @param {Object} oauth2
  * @param {String} folderId
  * @param {String} parentFolderId
  */
const moveFolder = (oauth2, folderId, parentFolderId) => {
    const url = `https://api.box.com/2.0/folders/${encodeURIComponent(folderId)}`;
    const jsonBody = {
        "parent": {
            "id": parentFolderId
        }
    };

    const response = httpClient.begin()
        .authSetting(oauth2)
        // fields には id を指定(レスポンスを基本レプリゼンテーションに縮小するため)
        .queryParam("fields", "id")
        .body(JSON.stringify(jsonBody), "application/json; charset=UTF-8")
        .put(url);
    const status = response.getStatusCode();
    const responseTxt = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseTxt);
        throw new Error(`Failed to move. status: ${status}`);
    }
};

上部へスクロール

Questetra Supportをもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む