Box: Move File
This item moves the File to the specified Folder. File ID and URL won’t change.
Configs: Common
  • Step Name
  • Note
Configs
  • C1: OAuth2 Setting *
  • C2: File ID to move *
  • C3: Folder ID to move to *

Notes

  • Folder ID is contained in the URL: https://{sub-domain}.app.box.com/folder/(Folder ID)
  • File ID is contained in the URL: https://app.box.com/file/(File ID)
  • If there are file name or folder name conflicts it will result in an error
  • Box refresh tokens have an expiration date

Capture

See also

Script (click to open)
  • An XML file that contains the code below is available to download
    • box-file-move.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on

main();
function main(){
  //// == Config Retrieving / 工程コンフィグの参照 ==
  const oauth2 = configs.get( "conf_OAuth2" ) ;
  const fileId = decideFileId();

  if(fileId === "" || fileId === null){
    throw "No File ID";
  }

  const folderId = decideFolderId();

  if(folderId === "" || folderId === null){
    //空の場合はエラーとする when folder id isn't set, throw error
    throw "No Folder ID";
  }
  moveFile(oauth2, folderId, fileId);
}

/**
  * ファイルのIDをconfigから読み出して出力する。
  * @return {String}  ファイルの ID
  */
function decideFileId(){
  const fileIdDef = configs.getObject("conf_FileId");
  const fileId = engine.findData(fileIdDef);
  return fileId;
}

/**
  * フォルダのIDをconfigから読み出して出力する。
  * @return {String}  フォルダの ID
  */
function decideFolderId(){
  let folderId = "";
  const folderIdDef = configs.getObject("conf_FolderId");
  if(folderIdDef === null){
    folderId = configs.get( "conf_FolderId");
  }else{
    folderId = engine.findData(folderIdDef);
  }
  return folderId;
}

/**
  * Box 上のファイルを指定フォルダに移動する。
  */
function moveFile(oauth2, folderId, fileId) {
  const jsonReq = {};
  jsonReq["parent"] = {"id": folderId };
  const url = `https://api.box.com/2.0/files/${fileId}`;

  const response = httpClient.begin()
    .authSetting(oauth2)
    // fields には id を指定(レスポンスを基本レプリゼンテーションに縮小するため)
    .queryParam("fields", "id")
    .body(JSON.stringify(jsonReq), "application/json; charset=UTF-8")
    .put(url);

  const status = response.getStatusCode();
  const responseTxt = response.getResponseAsString();

  if (status >= 300) {
    engine.log(responseTxt);
    throw `Failed to move. status: ${status}`;
  }
}

1 thought on “Box: Move File”

  1. Pingback: Process Start Triggered by a File Upload to Box – Questetra Support

Comments are closed.

%d bloggers like this: