Box: Move File
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 *
Script (click to open)

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)
    .body(JSON.stringify(jsonReq), "application/json; charset=UTF-8")
    .put(url);

  const status = response.getStatusCode();
  const responseTxt = response.getResponseAsString();
  
  engine.log(`status: ${status}`);

  if (status >= 300) {
    engine.log(responseTxt);
    throw "Failed to move.";
  }
  engine.log("Succeeded to move.")
}

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. Use regularly to ensure that it does not expire. (Box: Token & URL Expiration)

Capture

See also

Appendix

  • 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
%d bloggers like this: