Backup Google Drive folder to Google Cloud Storage

Stéphane Giron
3 min readJun 30, 2022

--

Today the 30th June 2022, is the launch of the french region for Google Cloud Platform and to celebrate that opening what better for french Google Workspace customers (and also others) than archive a Google Drive folder in a Cloud Storage bucket located in their country :-)

Setup your GCP project with Cloud Storage

First you will need to create a Google Cloud Platform project and verify that :

✅ Cloud storage service is enabled

✅ Cloud Storage API is enabled

✅ Cloud Storage JSON API is enabled

Now create your bucket, go to Cloud Storage (direct link) and click “Create Bucket”

Example, create bucket for France region

Then enter a unique name and click “Continue”. Now select the region. For my example I will select the France specific region :

Create Cloud Storage bucket for region France europe-west9 (Paris)

Click on “Continue” button for all the others questions.

Now you bucket is created :

YOUR-BUCKET-NAME is the name you defined.

Upload files to Cloud Storage from Google Drive

Of course to do that I use Google Apps Script 😀

✨ Some highlights regarding the code ✨

# To upload the file we will use the standard

DriveApp.getFileById(id).getBlob() method to get file as blob.

We will just filter the Google Sheets, Docs and Slides files from this method to get files in Office format and not PDF which is the default one. We will download the file from the exportlinks :

blob = UrlFetchApp.fetch(file.exportLinks[mime[file.mimeType].type],{
method: ‘GET’,
headers: {
Authorization: ‘Bearer ‘ + ScriptApp.getOAuthToken(),
}
}).getBlob();
fileName = file.title+’.’+mime[file.mimeType].extension ;

Last line is to add the extension to the file name as in Drive the Google files don’t have extension in the name.

# Management of shortcuts

Some months ago Google introduced shortcuts, so we need to intercept this specific type of files to get the source instead of the shortcut. Specifc point regarding shortcut I needed to use the Drive v3 API and not the v2 which is the one used by Drive advanced service in Google Apps Script.

function getFileFromShortcut(id){
let rep = UrlFetchApp.fetch(‘https://www.googleapis.com/drive/v3/files/'+id+'?fields=shortcutDetails(targetId)',
{method: ‘GET’,
headers: {
Authorization: ‘Bearer ‘ + ScriptApp.getOAuthToken(),
}
});

return Drive.Files.get(JSON.parse(rep).shortcutDetails.targetId)
}

We also use a try()catch() to prevent shortcuts the user don’t have access to.

# Upload to cloud Storage

For simplification we use the current user Access Token to upload the file on Cloud Storage as we assume the creator of the bucket and script will have all the access. This can also be done with a Service Account, here is a reference from Amit Agarwal another Google Workspace GDE 😉 to implement this.

At the end to start upload you need to run the function : uploadFilesFromFolderToCloudStorage()

Get your copy

You can make a copy of the script : link

Once opened click on “Make a copy” button :

Copy Google Apps Script file

If you prefer you can get the code from GitHub : link.

--

--