This tutorial describes how one can add information and folders out of your Google Drive to a bucket in Google Cloud Storage utilizing Google Apps Script.
This tutorial describes how one can add information and folders out of your Google Drive to a bucket in Google Cloud Storage utilizing Google Apps Script. You’ll be able to even arrange a time-based set off, like a cron job, that watches a folder in your Google Drive and robotically add new incoming information to Google Cloud Storage. The identical method will also be used to add information from Google Drive to Firebase Storage.
Create Google Cloud Storage Bucket
To get began, go to console.cloud.google.com/projectcreate and create a brand new Google Cloud Venture. As soon as the undertaking has been added, go to console.cloud.google.com/storage/create-bucket and create a brand new bucket. Give your bucket a singular identify and choose the area the place you need to retailer your information. If the information you might be importing are non-public and also you don’t need to make them public anytime later, chances are you’ll allow the “Implement public entry prevention on this bucket” choice.
Create Storage Service Account
Subsequent, go to IAM & Admin > Service Accounts console.cloud.google.com/iam-admin/serviceaccounts/create and create a brand new service account. Give your service account a reputation and choose the “Storage Admin” function. You might also need to add the “Service Account Token Creator” function to the service account, as that is required to create signed URLs for the information you add to Google Cloud Storage.
From the record of service accounts, click on the one you created within the earlier step. Go to the “Keys” tab and click on on “Add Key” > “Create New Key,” and choose the JSON choice. It will obtain a JSON file containing the service account credentials. You have to these credentials to add information to Google Cloud Storage.
Write Google Apps Script Code
Go to script.new to create a brand new Google Apps Script undertaking. Click on on Libraries and add the OAuth2 library 1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF
to your undertaking. Subsequent, add a brand new file service.js
, and use the values of private_key
and client_email
out of your service account JSON file to create a brand new OAuth2 service.
const service_account = {
private_key: '-----BEGIN PRIVATE KEY-----n51CjpLsH8An-----END PRIVATE KEY-----n',
client_email: 'add@storage-labnol.iam.gserviceaccount.com',
};
const getStorageService = () =>
OAuth2.createService('FirestoreStorage')
.setPrivateKey(service_account.private_key)
.setIssuer(service_account.client_email)
.setPropertyStore(PropertiesService.getUserProperties())
.setCache(CacheService.getUserCache())
.setTokenUrl('https://oauth2.googleapis.com/token')
.setScope('https://www.googleapis.com/auth/devstorage.read_write');
Add Recordsdata to Google Cloud Storage
Subsequent, we’ll write the add perform in Apps Script. The perform takes the file ID of the file you need to add to Google Cloud Storage, the identify of the bucket, and the trail the place you need to retailer the file.
The perform makes use of the getStorageService
perform from the earlier step to create a brand new OAuth2 service. The getAccessToken
methodology of the OAuth2 service is used to get the entry token required to add information to Google Cloud Storage.
const DRIVE_FILE_ID = 'abc123';
const STORAGE_BUCKET = 'labnol.appspot.com';
const FILE_PATH = 'parentFolder/subFolder';
const uploadFileToCloudStorage = () => {
const file = DriveApp.getFileById(DRIVE_FILE_ID);
const blob = file.getBlob();
const bytes = blob.getBytes();
const API = `https://www.googleapis.com/add/storage/v1/b`;
const location = encodeURIComponent(`${FILE_PATH}/${file.getName()}`);
const url = `${API}/${STORAGE_BUCKET}/o?uploadType=media&identify=${location}`;
const service = getStorageService();
const accessToken = service.getAccessToken();
const response = UrlFetchApp.fetch(url, {
methodology: 'POST',
contentLength: bytes.size,
contentType: blob.getContentType(),
payload: bytes,
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
const end result = JSON.parse(response.getContentText());
Logger.log(JSON.stringify(end result, null, 2));
};
Additionally see: File Add Kinds for Google Drive