• Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms & Conditions
Saturday, April 1, 2023
No Result
View All Result
Indian Buzz Today
  • Home
  • Global News
  • Startup & Business
  • Politics
  • Sports
  • Tech
  • Bollywood
  • Celebrities
  • Fashion
  • Lifestyle
  • Beauty
  • Home
  • Global News
  • Startup & Business
  • Politics
  • Sports
  • Tech
  • Bollywood
  • Celebrities
  • Fashion
  • Lifestyle
  • Beauty
No Result
View All Result
Indian Buzz Today
No Result
View All Result
Home Tech

Add Recordsdata from Google Drive to Google Cloud Storage with Google Apps Script

by indianbuzz
January 19, 2023
in Tech
0
Add Recordsdata from Google Drive to Google Cloud Storage with Google Apps Script
0
SHARES
0
VIEWS
Share on FacebookShare on Twitter


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



Source_link

indianbuzz

indianbuzz

Next Post
JetBlue Aircraft Crashes Into Parked Airplane at JFK Airport

JetBlue Aircraft Crashes Into Parked Airplane at JFK Airport

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Popular News

    Indian Buzz Today

    Welcome to Indian Buzz The goal of Indian Buzzis to give you the absolute best news sources for any topic! Our topics are carefully curated and constantly updated as we know the web moves fast so we try to as well.

    Category

    • Beauty
    • Bollywood
    • Celebrities
    • Fashion
    • Global News
    • Lifestyle
    • politics
    • Sports
    • Startup & Business
    • Tech

    Site Links

    • Home
    • About Us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • Terms & Conditions

    Recent Post

    • Tata-Owned Air India Pilots Revolt Towards Administration: Are They Compelled To Work Further Hours? – Trak.in
    • Constructing Your First PC: Getting the Fundamentals Proper  – TechGamesNews
    • Turning into a Grasp of Customized Clothes: Suggestions & Tips

    Copyright © 2022 Indianbuzz.today | All Rights Reserved.

    No Result
    View All Result
    • Home
    • Global News
    • Startup & Business
    • Politics
    • Sports
    • Tech
    • Bollywood
    • Celebrities
    • Fashion
    • Lifestyle
    • Beauty

    Copyright © 2022 Indianbuzz.today | All Rights Reserved.