可以获取Blob(),但不能获取Bytes()



我正在尝试验证文件是否正确保存到磁盘,因此一旦保存,我就会提取它们,将字节长度与用于保存文件的原始Blob进行比较。大多数时候它都能工作,但每当文件包含application/json内容时,我就会得到

异常:访问被拒绝:DriveApp。

在下面的代码中,我可以检索Blob,但不能检索最后一行代码中的字节。我不能对application/json文件执行此操作是否有特殊原因?

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

特别是,如果有人想重现错误,这里是文件中的脚本:

{"version":"1.0","provider_name":"Pru00e9stamos P2P","provider_url":"https://prestamosp2p.es","author_name":"Arturo V.G","author_url":"https://prestamosp2p.es/author/fcqgf/","title":"Pru00e9stamos P2P y prestamistas particulares en Espau00f1a","type":"rich","width":600,"height":338,"html":"<blockquote class="wp-embedded-content"><a href="https://prestamosp2p.es/">Pru00e9stamos P2P y prestamistas particulares en Espau00f1a</a></blockquote>n<script type='text/javascript'>n<!--//--><![CDATA[//><!--ntt/*! This file is auto-generated */ntt!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="u00abPru00e9stamos P2P y prestamistas particulares en Espau00f1au00bb u2014 Pru00e9stamos P2P" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" class="wp-embedded-content"></iframe>"}

我相信你的目标如下。

  • 您想要使用Google Apps脚本检索文件大小

问题和解决方法:

我可以复制你的情况。我还认为,你的问题可能是由于你的评论中的Possible the file contained malware and that's why Google blocks it.。在这种情况下,可以像DriveApp.getFileById(fileId).getSize()一样直接从文件对象中检索文件大小。(此外,文件大小也可以通过驱动器API检索。(当我测试它时,我确认文件大小可以从包括文本值的文件中检索。

但是,在你的情况下,我认为可能需要考虑谷歌文档文件。在Google文档文件(电子表格、文档、幻灯片等(中,quotaBytesUsed(Ref(的值为0。鲁本的回答已经提到了这一点。但是,当从Google Docs文件中检索Blob时,文件类型会转换为PDF格式。在这种情况下,可以检索数据大小。在这个答案中,也考虑到了这一点。

修改的脚本:

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)
  • 在这个修改后的脚本中,当文件类型为application/json(来自您的问题(时,将使用fetchedFile.getSize()检索文件大小。在其他文件类型的情况下,使用a.getBytes().length检索大小。

  • 或者,作为另一个示例,下面的脚本怎么样?在此脚本中,当size = a.getBytes().length发生错误时,将使用size = fetchedFile.getSize()检索文件大小。

    var fetchedFile = DriveApp.getFileById("###");  // Please set the file ID.
    var a = fetchedFile.getBlob();
    var size = 0;
    try {
    size = a.getBytes().length;
    } catch(e) {
    size = fetchedFile.getSize();
    }
    console.log(size)
    

参考:

  • getSize((

Google Drive句柄有超过存储配额的文件,但也有没有。很可能;application/json";内容与后面的内容相对应。

您可以使用isGoogleType进行检查。

相关内容

最新更新