Google Drive Hosting with Apps Script

The tutorial on hosting websites with Google Drive makes use of the HTMLService of Google Apps Script. The user uploads a zip file, the file is extracted using the Utilities.unzip method (make the sure the MIME type is set to application/zip) and saved in a folder.

The setSharing method is then used for making the Google Drive folder public and the ID of the folder is passed to the user. Here’s the full code:

/* Written by Amit Agarwal on August 9, 2013 */

function doGet() {
  var output = HtmlService.createHtmlOutputFromFile('labnol');
  output.setTitle('Publish Website on Google Drive');
  return output;
}

function uploadWebsite(form) {
  try {
    var zip,
      files,
      name,
      folder,
      file,
      host,
      found = false;

    zip = form.zipFile.setContentType('application/zip');
    files = Utilities.unzip(zip);

    // Unique folder name based on the current date and time
    name = Utilities.formatDate(new Date(), 'GMT', 'ddMMyyyyHHmmss');
    folder = DriveApp.createFolder('Website #' + name);

    for (var i = 0; i < files.length; i++) {
      file = folder.createFile(files[i]);
      if (file.getName() === 'index.html') {
        found = true;
      }
    }

    if (found) {
      // Set the sharing permissions of the Drive folder as Public
      folder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
      var site = 'https://googledrive.com/host/' + folder.getId() + '/';
      return 'Your <a href="' + site + '" target="_blank">website</a> is now live on Google Drive. The URL is: ' + site;
    } else {
      // if the index.html file is not available, don't publish the website
      DriveApp.removeFolder(folder);
    }

    return "Sorry, we couldn't find an index.html in your zip file. Please try again.";
  } catch (e) {
    return e.toString();
  }
}
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.