Google Scripts – ExaVault Direct Link Generator

  • report
    Disclaimer
    Click for Disclaimer
    This Project is over a year old (first published about 9 years ago). As such, please keep in mind that some of the information may no longer be accurate, best practice, or a reflection of how I would approach the same thing today.
  • infoFull Project Details
    info_outlineClick for Full Project Details
    Date Posted:
    Jan. 10, 2016
ExaVault is essentially a service that provides file hosting with a simple directory interface, but has the distinct feature of offering FTP access and advanced permissions (which is why its user base is mainly businesses / enterprise). To make part of my job easier at work, I wanted to integrate our ExaVault access into a Google Scripts project I was working on. I wanted to have my Google Script retrieve a CSV from our ExaVault folder and parse and process it into a specific format. However, I quickly ran into an issue. ExaVault, by default, provides links to files through the FTP protocol. The links look like “ftp://links:pass@test.exavault.com/transfer.pdf”. Unfortunately, the UrlFetchApp, which is a service Google Scripts uses to handle web requests, only works with HTTP/HTTPS requests/responses, and not the FTP protocol. So, I needed to figure out how get a direct link to my CSV in ExaVault, instead of a FTP link. The solution? Use the ExaVault API to generate a temporary direct link. It ended up being much easier than it sounds – ExaVault’s API is extremely easy to use. Here is my reusable code (below) – the return of my function is the direct download link to your file, which you can then pass to the URLFetchApp. Feel free to modify/reuse/do whatever you want with it (attribution not required, but is always appreciated). Username, password, and filepath are all mandatory arguments for the function, whereas opt_downloadname is optional. Make sure “api_key” is declared and set to your api key value (you will need to do a one-time generation of an API key here: https://clients.exavault.com/clientarea.php?action=products).
var api_key = "PUT_YOUR_API_KEY_HERE"; // DO NOT SHARE WITH ANYONE, PRIVATE API KEY FOR EXAVAULT
var access_token;

function Exavault_URL_Fetcher(username, password, filepath, opt_downloadname) {
  var done = false;
  var downloadname = (opt_downloadname || "downloadfile"); // use the optional download file name if provided, otherwise default to "downloadfile"
  while (done == false){
    if (access_token == undefined){
        Logger.log("Retreiving access token using user+pass");
        var payload = {
          "username" : username,
          "password" : password,
          "api_key" : api_key
        };
        var options = {
          "method" : "post",
          "payload": payload
        };
        var response = UrlFetchApp.fetch("https://api.exavault.com:443/v1/authenticateUser", options)
        var parsed = JSON.parse(response);
        var token = parsed.results.accessToken;
        access_token = token;
    }
   else {
       Logger.log("Retreiving Download URL using token+key");
       var payload = {
         "access_token" : access_token,
         "filePaths" : filepath,
         "downloadName": opt_downloadname,
         "api_key": api_key
       };
       var options = {
         "method": "post",
         "payload": payload
       };
       var response = UrlFetchApp.fetch("https://api.exavault.com:443/v1/getDownloadFileUrl", options)
       var parsed = JSON.parse(response);
       var downloadURL = parsed.results.url;
       return downloadURL;
       done = true;
   }
 }
}
Comments have been disabled for this post.