-1

I hope you are having a great day. I am working on building an automation service that takes a file from Google Drive (based on file ID/URL) and import it to Google Photos. Please have a look at this demonstration video. This service will be deployed as API on my hosting and integrated to a software where I have access to OAuth Tokens for Google APIs (at the moment, one user account is authenticated, but we are hoping to scale the solution later so users can authenticate themselves based on google account, allow OAuth delegation and use the service - this part is clear).

Using Google Apps Script, I can accomplish this using GPhotosApp, as getting the file by id, grabbing its blob and send the blob to Google Photos. But since I want to integrate the solution into my organization software that is hosted on my own server. I can not use Google Apps Scripts in this case, rather would rely on Google Drive & Google Photos APIs.

The overall workflow is given below:

  1. Locate a publicly shared file (with view access) on the users' Google Drive using get file endpoint
  2. Get the content of the file (Facing problem here - described below)
  3. Create a new file in users' Google Photos using upload_media endpoint of Photos API
  4. Get URL of the newly created Google Photos file (Facing problem here - described below)

I expect help with the following queries:

  1. Can this whole solution be implemented in Node Js using the Google Drive and Photos Node SDKs?
  2. When hitting Google Drive file get endpoint I get back the file metadata, and not the actual file content that I want to upload into Google Photos. On the documentation page, they say alt=media would get you the file content but while trying the api call (on the documentation page), I only see json (default) in alt parameter value. So, how can I get the publicly available file content form users' google dirve?
  3. As demonstrated in the video, almost 900 MB file is imported instantly into Google Photos. I am not sure if this current approach (listed above) I am using will upload the file instantly or depending on file size it will take more time? For reference, file sizes can range from 2MBs - 10GBs.
  4. Lastly, how can I get the URL of media just uploaded in Google Photos?

Similar Resources I found on StackOverflow:

  1. GPhotosApp, which doesn't fullfil my requirements (as it's based on Google Apps Script)
  2. Google Photos API - Loading photos directly from Google Drive (No responses on this post)

I hope I am descriptive with the shared resources and information, if not I will appreciate being corrected. Also, any leads will be really appreciated.

Stay blessed.

Aamir Maarofi
  • 155
  • 1
  • 12
  • First, I deeply apologize that my sample script was not useful for your situation. About your question, 1. Who is run the script? 2. Are the files on the user's drive publicly shared or private files? 3. I cannot understand your 2nd and 3rd questions. Can I ask you about the detail of them? – Tanaike Apr 04 '22 at 00:02
  • Hey @Tanaike, thanks for commenting. Your library is very helpful and I had great experience integrating it into my other projects. Regarding this query 1. Currently we are running the script by setting OAuth 2.0 scopes and API_KEY for one user but in future anyone who sign-up with their google account on our software will access this service. 2. The files on drive are publicly available as "anyone with the link can view" permissions. – Aamir Maarofi Apr 04 '22 at 00:24
  • 3. I added more explanation to 2nd & 3rd. 2nd question: when using the "Get File Endpoint on Google Drive API", I get back 'file details (title, id, etc)' in response, but actually I want to get back the file that I will send to "upload_media endpoint" of google photos. The "get_file" documentation says that setting 'alt' parameter to 'media' will help you download the file content but this doesn't work as only see 'json (default)' value can be set for 'alt' parameter. 3rd question: with my current approach I am not sure if this will upload data instantly or will take time for larger files – Aamir Maarofi Apr 04 '22 at 00:29
  • Also, can I deploy google apps script on my own server to use them as backend service to my application hosted on same server? If this require Google Cloud resource we can scale to accommodate the google cloud cost – Aamir Maarofi Apr 04 '22 at 00:41
  • Thank you for replying. I have to apologize for my poor English skill. Unfortunately, I cannot still understand your question. But I would like to try to understand it. When I could correctly understand it, I would like to think of the solution. I would be grateful if you can forgive my poor English skill. – Tanaike Apr 04 '22 at 04:42

1 Answers1

0

Upload Images to Google Photos

html:

<form>
  <br /><select id="sel1" name="Destination"></select> Upload Destination
  <br /><input type="text" name='FileName' /> File Name
  <br /><input type="file" name='File'; />
  <br /><input type="button" value="Upload" onClick="uploadFile(this.parentNode);" />
  <br /><input type="button" value="Close" onClick="google.script.host.close();" />
</form>
<div id="msg"></div>

js:

function uploadFile(form) {
    console.log('form.elements.Destination.value= %s',form.elements.Destination.value);
    console.log('form.elements.FileName.value= %s',form.elements.FileName.value);
    $('#msg').css('display','block');
    $('#msg').html('UpLoading...Please Wait');
    const file=form.File.files[0];
    const fr=new FileReader();
    fr.onload=function(e) {
      const obj={FileName:form.elements.FileName.value,Destination:form.elements.Destination.value,mimeType:file.type,bytes:[...new Int8Array(e.target.result)]};
      google.script.run
      .withSuccessHandler(function(msg){
        $('#msg').css('display','block');
        $('#msg').html(msg);
      })
      .uploadFile(obj);
    }
    fr.readAsArrayBuffer(file);
  }
  

GS:

function uploadFile(obj) {
  SpreadsheetApp.getActive().toast('Here');
  var folder=DriveApp.getFolderById('1VAh2z-LD6nHPuHzgc7JlpYnPbOP_ch33');
  if(!obj.hasOwnProperty('FileName'))obj['FileName']="NoFileName";
  var blob = Utilities.newBlob(obj.bytes, obj.mimeType, obj.FileName);
  var rObj={};
  var ts=Utilities.formatDate(new Date(), Session.getScriptTimeZone(),"yyMMddHHmmss");
  var file=folder.createFile(blob).setName(obj.FileName + '_' + ts);
  rObj['file']=file;
  rObj['filename']=file.getName();
  rObj['filetype']=file.getMimeType();
  rObj['id']=file.getId();
  if(obj.Destination!=0){
    var uObj={albumid:obj.Destination,fileid:rObj.id,filename:rObj.filename};
    //Logger.log(JSON.stringify(uObj));
    addFileToPhotoLibrary(uObj);
    var msg=Utilities.formatString('<br/>File: %s<br />Type: %s<br />Folder: %s<br />File Added to Photo Library',rObj.filename,rObj.filetype,folder.getName());
    return msg;
  }else{
    var msg=Utilities.formatString('<br/>File: %s<br />Type: %s<br />Folder: %s',rObj.filename,rObj.filetype,folder.getName());
    return msg;
  }
}
Cooper
  • 48,630
  • 6
  • 20
  • 48
  • Hey @Cooper, I really appreciate you took time to answer this question. But I am looking for a solution that will be served as an API. Your current script takes file from the form and upload it to a folder in drive but in my case that will not be necessary, instead if I was using apps script the GPhotosAPP by Tanaike will be the most efficient and easiest approach to accomplish this. – Aamir Maarofi Apr 05 '22 at 21:22
  • I am looking for a solution that will take an existing file stored on my drive and upload it to google photos, not with google apps script but Google Drive & Google Photos V2 APIs that I will host on my server later. – Aamir Maarofi Apr 05 '22 at 21:23
  • So rewrite it any way you wish. – Cooper Apr 05 '22 at 21:30
  • Ofcourse, I will write the code myself and that will not be a problem but currently, I can not retrieve data from Google Drive using the Google Drive API V2 "get" endpoint, I will appreciate if you can share any details regarding my second query in the question. – Aamir Maarofi Apr 05 '22 at 21:45
  • Please post the code that you are having trouble with. Please post the code in your question [mcve] – Cooper Apr 05 '22 at 21:56