Google Drive: Copy File
Creates a copy of a file in a chosen Folder
2020-01-10 (C) Questetra, Inc. (MIT License)
Configs
  • C1: OAuth2 Config Name *
  • C2: Source File ID * #{EL}
  • C3: Folder ID to store (When empty, create in origin folder) #{EL}
  • C4: New File Name (When empty, named automatically) #{EL}
  • C5: STRING DATA for New File ID
  • C6: STRING DATA for New File Viewing URL
Script (click to open)

main();
function main(){
  //// == Config Retrieving / 工程コンフィグの参照 ==
  const oauth2 = configs.get( "OAuth2" ) + "";
  const fileId = configs.get( "SourceFileId" ) + "";
  const folderId = configs.get( "FolderId" ) + "";
  const saveIdData = configs.get( "DataForId" ) + "";
  const saveUrlData = configs.get( "DataForUrl" ) + "";
  const newFileName = configs.get( "NewFileName" ) + "";

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

  //// == Calculating / 演算 ==
  const token  = httpClient.getOAuth2Token( oauth2 );

  // preparing for API Request
  let apiRequest = httpClient.begin(); // HttpRequestWrapper
  // com.questetra.bpms.core.event.scripttask.HttpClientWrapper

  // Request HEADER (OAuth2 Token, HTTP Basic Auth, etc)

  apiRequest = apiRequest.bearer( token );

  // Request PATH (https://example.com/abc/def/)
  let apiUri = "https://www.googleapis.com/drive/v3/files/";
    apiUri += fileId;
    apiUri += "/copy";

  // Request QUERY (?a=b)
  apiRequest = apiRequest.queryParam( "supportsTeamDrives", true );
  apiRequest = apiRequest.queryParam( "fields", "id,webViewLink" );
  // Request BODY (JSON, Form Parameters, etc)
  let requestObj = {};
    requestObj.parents = [];
  
  if(folderId !== "" && folderId !== null){
    requestObj.parents[0] = folderId;
  }
    
  if ( newFileName !== "" && newFileName !== null ){ 
    requestObj.name = newFileName;
  }
  apiRequest = apiRequest.body( JSON.stringify( requestObj ), "application/json" );

  // Access to the API (POST, GET, PUT, etc)
  let response = apiRequest.post( apiUri ); // HttpResponseWrapper
  const httpStatus = response.getStatusCode() + "";
  let accessLog = "---POST request--- " + httpStatus + "\n";
  accessLog += response.getResponseAsString() + "\n";
  if (httpStatus >= 300) {
    const error = "Failed to copy \n status:" + httpStatus + "\n" + response.getResponseAsString();
    throw error;
  }
  const responseObj = JSON.parse( response.getResponseAsString() );

  // Retrieve Properties from Response-JSON
  const newFileId = responseObj.id;
  const newFileUrl = responseObj.webViewLink;
  // Error Handling
  // (no set)

  //// == Data Updating / ワークフローデータへの代入 ==
  if ( saveIdData !== "" ){ engine.setDataByNumber( saveIdData, newFileId ); }
  if ( saveUrlData !== "" ){ engine.setDataByNumber( saveUrlData, newFileUrl ); }
  engine.log(accessLog);
}

Download

Capture

Notes

  1. No way to save a copy to “My Drive”

See also

4 thoughts on “Google Drive: Copy File”

  1. Pingback: Google Docs: Document; Replace All Text – Questetra Support

  2. Pingback: Google Drive Files Copy – Questetra Support

  3. Pingback: Google Drive: File Permissions, Remove AnyoneWithLink – Questetra Support

  4. Pingback: Google Drive: File Permissions, Add Reader – Questetra Support

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d