google-apps-scriptgoogle-drive-api

Can getBlob() but not getBytes()


I am trying to verify that files were saved correctly to disk so once they are saved I fetch them to compare byte length with the original Blob used to save the file. Most of the time it works, but whenever the file contains application/json content I get

Exception: Access denied: DriveApp.

In my code below I can retrieve the Blob but not the bytes in the last line of code. Is there a special reason I should not be able to do this for application/json files?

function a(){
  var fetchedFile = DriveApp.getFileById('11BPqR9K9EznpBAdMqqFJ7m0DbSUQZ6qU');
  var a =fetchedFile.getBlob();
  var fileFoundLength = a.getBytes();  
}

Specifically if anyone wants to reproduce the error here is the script that was in the file:

{"version":"1.0","provider_name":"Pr\u00e9stamos P2P","provider_url":"https:\/\/prestamosp2p.es","author_name":"Arturo V.G","author_url":"https:\/\/prestamosp2p.es\/author\/fcqgf\/","title":"Pr\u00e9stamos P2P y prestamistas particulares en Espa\u00f1a","type":"rich","width":600,"height":338,"html":"<blockquote class=\"wp-embedded-content\"><a href=\"https:\/\/prestamosp2p.es\/\">Pr\u00e9stamos P2P y prestamistas particulares en Espa\u00f1a<\/a><\/blockquote>\n<script type='text\/javascript'>\n<!--\/\/--><![CDATA[\/\/><!--\n\t\t\/*! This file is auto-generated *\/\n\t\t!function(d,l){\"use strict\";var e=!1,o=!1;if(l.querySelector)if(d.addEventListener)e=!0;if(d.wp=d.wp||{},!d.wp.receiveEmbedMessage)if(d.wp.receiveEmbedMessage=function(e){var t=e.data;if(t)if(t.secret||t.message||t.value)if(!\/[^a-zA-Z0-9]\/.test(t.secret)){var r,a,i,s,n,o=l.querySelectorAll('iframe[data-secret=\"'+t.secret+'\"]'),c=l.querySelectorAll('blockquote[data-secret=\"'+t.secret+'\"]');for(r=0;r<c.length;r++)c[r].style.display=\"none\";for(r=0;r<o.length;r++)if(a=o[r],e.source===a.contentWindow){if(a.removeAttribute(\"style\"),\"height\"===t.message){if(1e3<(i=parseInt(t.value,10)))i=1e3;else if(~~i<200)i=200;a.height=i}if(\"link\"===t.message)if(s=l.createElement(\"a\"),n=l.createElement(\"a\"),s.href=a.getAttribute(\"src\"),n.href=t.value,n.host===s.host)if(l.activeElement===a)d.top.location.href=t.value}}},e)d.addEventListener(\"message\",d.wp.receiveEmbedMessage,!1),l.addEventListener(\"DOMContentLoaded\",t,!1),d.addEventListener(\"load\",t,!1);function t(){if(!o){o=!0;var e,t,r,a,i=-1!==navigator.appVersion.indexOf(\"MSIE 10\"),s=!!navigator.userAgent.match(\/Trident.*rv:11\\.\/),n=l.querySelectorAll(\"iframe.wp-embedded-content\");for(t=0;t<n.length;t++){if(!(r=n[t]).getAttribute(\"data-secret\"))a=Math.random().toString(36).substr(2,10),r.src+=\"#?secret=\"+a,r.setAttribute(\"data-secret\",a);if(i||s)(e=r.cloneNode(!0)).removeAttribute(\"security\"),r.parentNode.replaceChild(e,r)}}}}(window,document);\n\/\/--><!]]>\n<\/script><iframe sandbox=\"allow-scripts\" security=\"restricted\" src=\"https:\/\/prestamosp2p.es\/embed\/\" width=\"600\" height=\"338\" title=\"\u00abPr\u00e9stamos P2P y prestamistas particulares en Espa\u00f1a\u00bb \u2014 Pr\u00e9stamos P2P\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" class=\"wp-embedded-content\"><\/iframe>"}

Solution

  • I believe your goal as follows.

    Issue and workaround:

    I could replicate your situation. I also thought that the reason of your issue might be due to Possible the file contained malware and that's why Google blocks it. from your comment. In that case, the file size can be directly retrieved from the file object like DriveApp.getFileById(fileId).getSize(). (Also, the file size can be also retrieved with Drive API.) And, when I tested this, I confirmed that the file size could be retrieved from the file including your text value.

    But, in your situation, I thought that it might be required to consider about the Google Docs files. At Google Docs files (Spreadsheet, Document, Slides and so on), the value of quotaBytesUsed (Ref) is 0. This has already been mentioned by Rubén's answer. But when the Blob is retrieved from Google Docs file, the file type is converted to the PDF format. In this case, the data size can be retrieved. In this answer, this is also considered.

    Modified script:

    var fetchedFile = DriveApp.getFileById("###");  // Please set the file ID.
    var a = fetchedFile.getBlob();
    var size = a.getContentType() == "application/json" ? fetchedFile.getSize() : a.getBytes().length;
    console.log(size)
    

    Reference: