简体   繁体   中英

Fetch file from external URL and upload to Google Drive using Apps Script

I'm not sure if this is even possible. I'm trying to fetch file that being uploaded to formfacade server via the add-on in google form. I'm using it to allow other non-gmail users to upload file without having to sign-in.

I referred to answer from Mogsdad and dheeraj raj in Can I download file from URL link generated by google apps script to use UrlFetchApp to meet this objective. Below are my codes:

Method 1 :

    function UrlFile2gdrive() {
     var sheet=SpreadsheetApp.getActiveSheet();
     var lrow=sheet.getLastRow();
     //var fileURL=sheet.getRange(lrow,2).getValue();
     var fileURL='https://formfacade.com/uploaded/1FAIpQLSfscYq_sbYcT2P3Sj3AvSD2zYKalIM0SKdPTESf1wE9Rq8qew/'
     +'97dc1ee0-f212-11ea-95c3-bdb6c5ab13b3/2124651919/A%20Sample%20PDF.pdf'
     var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}}; 
     
     var response=UrlFetchApp.fetch(fileURL,params);
      Logger.log(response.getContentText());
    var fileBlob=response.getBlob();
    
      
      var folder='0B2b-M7h6xF-Mflk3dGswano2TnJ3dGlmZG8wOUREMFg4blM5SHBuM3lqYmdPOThZSTBTSWs'
    
    
    var filename=fileURL.split("/").pop();
    //var filename=fileURL.split("%2F").pop();
    var file=decodeURIComponent(filename);
      Logger.log("filename : "+file);
      
     
     var newfile=DriveApp.getFolderById(folder).createFile(fileBlob.setName(file));    
     //var newfile=DriveApp.getFolderById(folder).createFile(response.setName(filename));
     
    }

Method 2

    //api-key : AIzaSyCcbdBCI-Kgcz3tE1N4paeF9a-vdi3Uzz8
    
    //Declare function
    function URL2gdriveWithPswd() {
      //Getting url,existing name and new name for image from the sheet in 
      //variable url, name and new_name respectively
      var sh = SpreadsheetApp.getActiveSheet();
      var row = sh.getLastRow();
      Logger.log(row);
      //for (var i = 2; i <= row; i++) {
    
        /*var url = sh.getRange(i, 2).getValue();
        Logger.log(url);
        var name = sh.getRange(i, 3).getValue();
        var new_name = sh.getRange(i, 4).getValue();*/
      var url = sh.getRange(row, 2).getValue();
        Logger.log(url);
        var filenm=url.split("/").pop();
        var new_name=decodeURIComponent(filenm);
        var name = sh.getRange(row, 3).getValue();
        //var new_name = sh.getRange(row, 4).getValue();
        //Creating authentication token for downloading image, it may not be //required if image can be downloaded without login into
        var user = "dtestsys@gmail.com";
        var password = "1851235656";
        var headers = {
          "Accept": "application/xml",
          "Content-Type": "application/xml",
          "Authorization": "Basic " + Utilities.base64Encode(user + ":" + password)
        };
        //defining method to download file
        var options = {
          "method": "get",
          "headers": headers
        };
        //Getting folder name where to store downloaded image
        var folders = DriveApp.getFoldersByName('File Uploader (File responses)');
        while (folders.hasNext()) {
          var folder = folders.next();
          Logger.log(folder.getName());
        }
        //Getting response on hit of url using downloading method defined //earlier storing in Blob
        var response = UrlFetchApp.fetch(url, options).getBlob();
        //Creating image in folder defined with response in blob and logging same file //in log to check, if required
        var file = folder.createFile(response);
        Logger.log(file);
    
        //renaming image
        var images = folder.getFiles();
        while (images.hasNext()) {
          var image = images.next();
          
          file.setName(new_name);
          Logger.log("imagename : "+image.getName());
        }
      //}
    }

However, both methods managed to get a file into my gdrive but the content consists of the html codes only ( https://drive.google.com/file/d/1NYQoMmCQEoP3z6L8niq1mpvIx7xl83zu/view?usp=sharing ), which I think the URL passed in google response sheet is just a "mask". I noticed that inside the file has some lines that mentioned api-key and code (next to user email address). Is it possible to achieve my objective? Are those api-key and code would be useful to get authorized to access the file and download it in gdrive?

I rechecked.The link produced and passed into my google sheet response is only the login page that redirects to another XML file. When I copied back the final URL after the original file content is displayed, the URL is as below: https://storage.googleapis.com/formfacade-public/1FAIpQLSfscYq_sbYcT2P3Sj3AvSD2zYKalIM0SKdPTESf1wE9Rq8qew%2F97dc1ee0-f212-11ea-95c3-bdb6c5ab13b3%2F2124651919%2FA%20Sample%20PDF.pdf?GoogleAccessId=firebase-adminsdk-pve0p%40formfacade.iam.gserviceaccount.com&Expires=1599671507&Signature=fBzWej0fEgF6Aw7oCHX%2FTTUfHbcep%2Bj%2B%2FhB3fYFUDeq0SFTuyJ6jTnLWQJmldD6XkVug0%2BNki7ZPNo2ESufvIfQjhVLKXgvp7UiQheJ4GYL%2BtXgFLaUyglgemmfp7KSvIvPxpMcpC2lR8em3E5YIvMRr9tcfzagvusQYHEb9mlD7k833bVoqFUVWuP%2FkP8tl%2BHYVL15kSXAjtFif4QZpu%2FFHwSik89Keo78LKTm0U8hZiAMeYDQZWF6w1pcKpy04md3xKtDPwZYCoUWOOtKkCI6JLskE5HweDvMCGnDbxW8o6SWD%2BIC%2FlaNC6%2BJ81OB10cuRqwQPEc9LnfgCZK7b1A%3D%3D

When I pasted the above link, I got to see as per screenshot below:- XML 文件格式外观 . So, I'm guessing they don't share direct access link to the uploaded file so that we are left with the option to buy/subscribe the paid version.

Would anyone knows if there's any better altrnative(s) I could use to achieve this objective? Like maybe a link with API-key just like what I learnt from @Tanaike in his previous answer on Convert-API to convert pdf file to PNG? Of course it has some limits for the free version but it still is a very helpful solution.

You are not assigning content-type of the blob anywhere. But if you do the naming right it would not matter. In method 1 you are trying to set a name on the blob when you should be setting it on the file created from the Blob.

Try setting the name on the file after creating it.

Example:

function myFunction() {
  var url  ="http://www.africau.edu/images/default/sample.pdf";
  var response = UrlFetchApp.fetch(url);
  console.log(response.getResponseCode());
  var blob=response.getAs('application/pdf');
  var folder = "<SOME-FOLDER-ID>";
  var fileName=decodeURIComponent(url.split("/").pop());
  
  console.log("File named : "+fileName);
  var file=DriveApp.getFolderById(folder).createFile(blob);
  // Set the name to the created file after creating it!
  file.setName(fileName); 
}

For reference see class File .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM