Box: Upload File
Upload files to the specified folder on Box.
2019-06-26 (C) Questetra, Inc. (MIT License)
Configs
  • C1: OAuth2 Setting Name *
  • C2: File type data item whose attached files will be uploaded *
  • C3: Folder ID that files will be uploaded (Root folder if blank) #{EL}
  • C4: String type data item that will save uploaded file ids
  • C5: String type data item that will save uploaded file urls
Script
// Upload to box via boxAPI (ver. 20161018)
// (c) 2016, Questetra, Inc. (the MIT License)

// OAuth2 config
// Authorization Endpoint URL: https://app.box.com/api/oauth2/authorize
// Token Endpoint URL: https://app.box.com/api/oauth2/token
// Scope:
// Consumer Key: (Get by box Application on box Developers)
// Consumer Secret: (Get by box Application on box Developers)

main();
function main(){
  //// == 工程コンフィグの参照 / Config Retrieving ==
  const oauth2 = configs.get( "conf_OAuth2" );
  const fileDataNumber = configs.get( "uploadedFile" ); // (returns Number)
  const idDataNumber = configs.get( "fileId" );
  const urlDataNumber = configs.get( "fileUrl" );
  //// == ワークフローデータの参照 / Data Retrieving ==
  let folderId = configs.get( "uploadedFolderId");
  if(folderId == "" || folderId == null){
    folderId = "0";
  }
  const files = engine.findDataByNumber( fileDataNumber );
  //// == 演算 / Calculating ==
  //let accessLog = "";
  let responseJson = "";
  let uploadedFileId = "";
  let uploadedFileUrl = "";
  const token = httpClient.getOAuth2Token( oauth2 );
  if (files !== null) {
    for (let i=0; i< files.size(); i++) {
      const file = files.get(i);
      let attributes = {
        //parent : '0'
        parent : {id : folderId}
      };
      attributes["name"] = String(file.getName());
      let response = httpClient.begin()
        .bearer(token)
        .multipart('attributes', JSON.stringify(attributes))
        .multipart('file', file)
        .post('https://upload.box.com/api/2.0/files/content');
      responseJson += response.getResponseAsString() + "\n";
      //when error thrown
      if (response.getStatusCode() !== 201) {
        if (response.getStatusCode() === 409) {
          throw "failed to upload: " + file.getName() + "\n status:" + response.getStatusCode() +"\n" + responseJson + "\n Same name file already exists.";
        }else{
          throw "failed to upload: " + file.getName() + "\n status:" + response.getStatusCode() + "\n" + responseJson;
        }
      }
      engine.log("---POST request--- " + response.getStatusCode() + "\n" + file.getName() + "\n" + responseJson + "\n");
      let json = JSON.parse(response.getResponseAsString());
      if(uploadedFileId !== ""){
        uploadedFileId += "\n";
      }
      if(uploadedFileUrl !== ""){
        uploadedFileUrl += "\n";
      }
      uploadedFileId += json.entries[0].id;
      uploadedFileUrl += "https://app.box.com/file/" + json.entries[0].id;
      responseJson = "";
    }
  }

  // for Debug
  //accessLog += responseJson + "\n";

  //// == ワークフローデータへの代入 / Data Updating ==
  if(idDataNumber !== "" && idDataNumber !==  null){
    const idDataDef = engine.findDataDefinitionByNumber(idDataNumber);
    //Multiple Judge
    if(idDataDef.matchDataType("STRING_TEXTFIELD") && files.size() > 1){
      throw "Multiple files are uploaded.Can't set data to single-line string Data Item."
    }
    engine.setDataByNumber(idDataNumber,uploadedFileId);
  }
  if(urlDataNumber !== "" && urlDataNumber !==  null){
    const urlDataDef = engine.findDataDefinitionByNumber(urlDataNumber);
    //Multiple Judge
    if(urlDataDef.matchDataType("STRING_TEXTFIELD") && files.size() > 1){
      throw "Multiple files are uploaded.Can't set data to single-line string Data Item."
    }
    engine.setDataByNumber(urlDataNumber,uploadedFileUrl);
  }
}

Download

Capture

Notes

  1. Folder ID is contained in the URL. https://{sub-domain}.app.box.com/folder/(Folder ID)
  2. If there are filename conflicts, it results in an error.
  3. The refresh token for Box has the expiration. Use regularly to ensure that it does not exceed the expiration. (60 days, as of July 2019. https://box-content.readme.io/docs/oauth-20)