gpt4 book ai didi

google-drive-api - 使用 google-api-ruby-client 从 Google Drive 下载文件

转载 作者:行者123 更新时间:2023-12-04 21:56:54 31 4
gpt4 key购买 nike

我尝试从 Google 磁盘上的目录下载文件。

代码,大部分复制自 official quickstart guide , 工作正常:

# ... code from official quickstart tutorial...

# Initialize the API
service = Google::Apis::DriveV3::DriveService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize


# now the real deal
response = service.list_files(q: "'0ByJUN4GMe_2jODVJVVpmRE1VTDg' in parents and trashed != true",
page_size: 100,
fields: 'nextPageToken, files(id, name)')
puts 'Files:'
puts 'No files found' if response.files.empty?
response.files.each do |file|
puts "#{file.name} (#{file.id})"
# content = service.get_file(file.id, download_dest: StringIO.new)
end

输出看起来不错:
Files:
k.h264 (0B4D93ILRdf51Sk40UzBoYmZKMTQ)
output_file.h264 (0B4D93ILRdf51V1RGUDFIWFQ5cG8)
test.mp4 (0B4D93ILRdf51dWpoZWdBV3l4WjQ)
test2.mp4 (0B4D93ILRdf51ZmN4ZGlwZjBvR2M)
test3.mp4 (0B4D93ILRdf51ZXo0WnVfdVBjTlk)
12.mp4 (0ByJUN4GMe_2jRzEwS1FWTnVkX00)
01.mp4 (0ByJUN4GMe_2jSlRWVEw4a1gxa2s)
01.mp4 (0ByJUN4GMe_2jOFpPMW9YNjJuY2M)

但是一旦我取消注释 content = service.get_file(file.id, download_dest: StringIO.new) ,我收到很多错误:
Files:
k.h264 (0B4D93ILRdf51Sk40UzBoYmZKMTQ)
/Users/mvasin/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/google-api-client-0.9.15/lib/google/apis/core/http_command.rb:211:in `check_status': Invalid request (Google::Apis::ClientError)
[...lots of other 'from' stuff...]
from /Users/mvasin/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/google-api-client-0.9.15/generated/google/apis/drive_v3/service.rb:772:in `get_file'
from quickstart.rb:56:in `block in <main>'
from quickstart.rb:54:in `each'
from quickstart.rb:54:in `<main>'

但这就是它应该根据 "download files" official tutorial 中的 ruby​​ 部分工作的方式.

我也试过 content = service.get_file(file.id, download_dest: "/tmp/#{file.name}") ,但它因相同的错误集而失败。

更新:这是我的发现。 如果您以 Google Drive API Ruby quick start tutorial 开头,并想让它下载一些东西,

1)更改范围,不仅让您的脚本读取肉数据,还读取文件内容:
SCOPE = Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY至少 SCOPE = Google::Apis::DriveV3::AUTH_DRIVE_READONLY
2)过滤掉google docs文件,因为这样下载不了,必须转换。对它们进行归档:

2.1) 添加 mime_type到文件集:
response = service.list_files(page_size: 10, fields: 'nextPageToken, files(id, name, mime_type)')
2.2) 并在打印文件的 ID 和名称的最后一个循环中,输入类似
service.get_file(file.id, download_dest: "/your/path/#{file.name}") unless file.mime_type.include? "application/vnd.google-apps"

最佳答案

从你得到的错误来看,它说你的请求无效。因此,请确保您的请求是正确的。这是documentation关于如何使用 Ruby 下载文件(只需单击示例中的 Ruby 即可查看 Ruby 代码。)

Take NOTE: Downloading the file requires the user to have at least read access. Additionally, your app must be authorized with a scope that allows reading of file content.



有关更多信息,请查看以下线程:
  • How to download file from google drive api
  • A Ruby library to read/write files
  • 关于google-drive-api - 使用 google-api-ruby-client 从 Google Drive 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39669343/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com