Upload file to Google Drive after downloading it from URL - google-apps-script

Greetings! I am now writing a little script for my spreadsheet and I have the last piece of puzzle remaining unsolved.
I want to download a file (or get all the file data) from a specific URL and then upload it to Google Drive (or create a file with those file data). I believe I can do that but how? May anyone help me please?
If this doesn't work, then I would have to set up a server with my computer. Send a request from by spreadsheet script to my computer. Let my computer run a script and download the file and save it into a specific folder. Let Google Drive sync the file... This is totally possible but is there a simpler way to achieve what I want?
Thank you in advance!!!!!

In a nutshell:
var response = UrlFetchApp.fetch(url);
var file = DriveApp.createFile(response.getBlob()).setName(filename);
I've written a more complete script/webapp for this purpose. The full source-code is available at GitHub Gist and at Google Apps Script, feel free to study and use that code. There is also a live version.

Use UrlFetch Services to get the data from the URL and Drive Services to store this data on Google Drive

Related

Is it possible to upload an entire folder to Google Drive using Google Drive API?

I am currently exploring Google Drive APIs and am able to upload an individual file using Postman successfully.
I tried to create/upload a folder using Google Drive API but failed. In fact, I don't see any specific API to deal with a folder. I was giving it a try with create files API.
Reference - https://developers.google.com/drive/api/v3/reference
Any help will be appreciated.
If you check the documentation for files.create you will notice at the top it states
Creates a file.
This is singular each request creates a single file. If you want to upload a directory you will need to first create the directory then upload each file one at a time.
You could try batching but I don't think you can batch the file upload itself you could probably batch the metadata creation performance#details

How to direct Puppeteer downloads directly into Google AppScript

would appreciate if anyone can suggest ideas:
I currently have a Puppeteer script which automates CSV downloads into google drive which then allows me to programmatically access the data and move it into google sheets.
Currently, I run this locally on my computer on VS code. I don't yet understand all too well how to handle file downloads through puppeteer, but I currently have a work around by setting the default chrome download path to a folder on my computer, and that folder is actually Drive for desktop, so easy way to get files directly in drive.
await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: path.resolve('G:/My Drive/DriveData')});
Thing is, I'd like this to be some type of scheduled cloud function not having to be manually run on my computer. IF I were to do that, my Drive for Desktop work-around would not be viable. So is there a way I could pass the file download in Puppeteer directly into Google AppScript somehow? Or use the Drive API to directly receive the file?
My main issue is I don't understand how to actually handle file downloads in Puppeteer, I'm currently just editing the default download location for the google chrome instance puppeteer creates. I ultimately just need to get the data somewhere accessible by Google AppScript.
Any suggestions would be uber-appreciated!
I was also facing the same situation, unfortunately we cant install or use puppeteer in google app script environment,
The alternative is to do the file download using UrlFetchApp , cookies and sessions using session and cookies
The following code will help you save the file into Gdrive
var file = folder.createFile(UrlFetchApp.fetch("url of the downloaded file fetched dynamically"));
You need to check through the network logs in chrome to know how the file and on what URL request you are getting the file downloaded which you can pass to the function
Another alternative that will help you to download the file to drive is to use https://developers.google.com/drive
I had used python to save files in drive https://www.thepythoncode.com/article/using-google-drive--api-in-python

Google Drive change file modified date

I am trying to see if I can update created and modified date of a mp4 file on Google drive.
This is not a question from a developer stand point but anything that can help me to get the job done would work. That said, if there is some way in Google Drive UI (Web App or smart phone app etc.) or using [Google's API harness][1]
to update the modified date of a file, I am ok with it. If your answer includes using API harness, please let me know where can I get the values for the API's parameters so I can run the API call from my login.
Just curious, if I somehow manage to update the modified time of the same file on my computer's hard drive, and then upload it (replace/revise an existing file on Google drive) would it read the meta-data from the file, or Google would use the time I uploaded the document as "modified date"?
Please note:
This is not specifically Google Drive API question, but if Google has an API test harness and I can put in some parameters there to update the file meta-data, I do not mind going to that route.

is it possible to download GMail attachment to local folder?

i want to download attachment from gmail and save it to local folder using google script. i did some research about this and i could't find any solution.
so far, i managed to save gmail's attachment to google drive only.
var attachmentBlob = attachment[0].copyBlob();
var file = DriveApp.createFile(attachmentBlob);
folder.addFile(file);
or, is it possible to create a google script to auto download file from google drive?
need some advice.
As #Sujay already mentioned you cannot download a file to a local folder using Google AppScript because GAS runs server-side.
You have already taken a good step with the code to save the attachment as a file to your Google drive.
To answer your sub-question 'is it possible to create a google script to auto download file from google drive?', you do not need a script to do that, that is what the Google Drive Desktop App (https://www.google.com.ng/drive/download/) does exactly.
It syncs all the files you add to your drive to your local-pc. You can also edit Google Drive preferences to sync select folders only, in your case that might be the drive folder referenced in your folder variable.
GAS runs on the server side, and only has access to Google's internal architecture. To save locally it'll need to create some kind of a blob and trigger a download within your browser. not sure if you can do that.

google drive sdk ,convert pdf to google docs

When I upload any file then it will automatically add in my google drive in google doc format account by php code.
What will be the steps by step process to do this
You need to insert the file, making sure you pass the convert=true parameter. Try out the PHP quickstart guide to get started.