Google Photos API - Save to Drive

1,565 views
Skip to first unread message

Brett Grear

unread,
Jan 11, 2022, 9:04:15 AM1/11/22
to Google Apps Script Community
Dear all,

Does anyone have any experience with the Google Photos API?
I've been playing around with it and have been able to get an object containing all the images in an album using "https://photoslibrary.googleapis.com/v1/mediaItems:search"

I've then been able to get the Baseurl from each image and use UrlFetchApp to get the blob of each image.
This has allowed me to save the blob to Google Drive.

Everything works correctly, the only problem is it's very slow.
Is there anyway to get the blobs in bulk via a single Url fetch, or a way to batch insert the blobs? Currently, I have to make one call to Url Fetch to get the blob and one call to the Drive API to insert it. This takes a lot of processing time.

Thanks in advance,

Brett

Andrew Roberts

unread,
Jan 11, 2022, 3:06:43 PM1/11/22
to google-apps-sc...@googlegroups.com
Hi,

I've not got an answer to your question, but I've been working on a Google Photos Manager (mainly for copying albums to a similarly named GDrive folder) myself and would be interested to follow your work.

Here's a link to the Log sheet that has my code bound to it. Take a copy if you want to have a look at the code.

This may not be relevant as you are making two different endpoint calls, but have you had a look at fetchAll()?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/8f5609c3-29d0-47c6-9831-fa00b7c7d6d9n%40googlegroups.com.

Brett Grear

unread,
Jan 11, 2022, 3:55:55 PM1/11/22
to Google Apps Script Community
I haven't had a chance to look properly at your script yet but it looks much more advanced that what I'm trying to do.
I had the idea to create a workspace add-on for Drive that you can just click on the add-on in the sidebar and select the album to folder to save the photos to.  What I didn't realise is that workspace add-ons have a 30 second run time limit so I am trying to make the code as fast as possible to save more photos before it times out.  At the moment it can only handle between 20-50 before it times out.  I have designed it so that after 20 it stops and asks the user if they want to continue which is a neat/annoying workaround but I must admit I had completely forgotten about fetchAll.  I'm gonna run some tests and see how much faster that is.  I suspect the real bottle neck is Drive.Files.insert. 

Brett Grear

unread,
Jan 11, 2022, 5:26:11 PM1/11/22
to Google Apps Script Community
Using FetchAll sped the runtime up considerably but it still needs to be faster to be properly useful.

I found a similar question about batching here - looking specifically at Method D.  Tanaike has the credit for the idea.  But this post is looking at specifically at making a pdf copy of a document rather than inserting from a blob.  It also uses the obsolete www.googleapis.com/batch api.  I wonder if something like this could be adapted to work with blobs. 

Tanaike

unread,
Jan 12, 2022, 1:36:05 AM1/12/22
to Google Apps Script Community
I have published the script for using the batch request as a Google Apps Script library. https://github.com/tanaikech/BatchRequest This library can use with the current specification. But, unfortunately, in the current stage, the batch requests cannot use the blob. It seems that this is the current specification at Google side.

So, in the current stage, in order to reduce the process cost of that flow, it is required to request the URLs with the asynchronous process like FetchApp of Google Apps Script and fetch of Javascript. But, when multiple requests are run, please be careful of the error related to many concurrent requests.

Andrew Roberts

unread,
Jan 12, 2022, 1:36:29 AM1/12/22
to google-apps-sc...@googlegroups.com
Sounds like we are working toward a similar goal. My project is to "scratch an itch" but I can envisage other people could find it useful, but I'll probably go the open source route rather than all the loops needed to jump through for getting an Add-on published.

Have you thought about creating an editor Add-on instead? This will give you at least 6 mins (possible 30 mins for Workspace accounts - but there seems to be some discussion about that at the moment). And it also opens up the possibility of using the CBL library to give yourself up to 6 hours of execution a day!

Brett Grear

unread,
Jan 12, 2022, 5:10:19 AM1/12/22
to Google Apps Script Community
Thanks Tanaike, your library looks really good.  It does say on https://developers.google.com/drive/api/v3/batch that "Note: Currently, Google Drive does not support batch operations for media, either for upload or download".  So, I'm not sure if this is possible.


And@ roberts, I have created editor add-ons in the past and this would alleviate the runtime problem.  I could go down this route although I opted for workspace add-on as it was a chance to properly learn the CardService layout.  I wasn't aware of the 30 second (in practise it seems to be 45 seconds) run time when starting.  I also didn't like the idea of having to open Google Sheets in order to save Google Photos to Google Drive although it would be a perfectly valid solution, and one that I've used in the past for other projects.

I have opted for also making a webapp which I'll host on eduscripts.uk.   A user will be able to select their album and just paste in the url of the folder where they would like to photos stored.  This way I can use client side javascript to call the server-side script (possibly multiple times simultaneously, although I'm not sure about concurrent runtime limits).  It's not a scaleable solution as apps script web-apps are limited to 30 concurrent users but that should be plenty for now.

I'm mainly interested in making it for myself anyway.  I'm a teacher and have all my photos on Google Photos.  But sometimes other teachers will request photos to be put in a specific folder after an event and I hate having to download them, unzip them and reupload them to the folder.  I'm not sure how many others would find it useful. 
Reply all
Reply to author
Forward
0 new messages