ruby-on-rails - [Google::Apis::DriveV3 ]获取上传文件地址
问题描述
我想获取使用 rails 中的 'google_drive' gem 上传到 Google Drive 的 URL。
我试图从创建的返回值中获取它,但没有这样的链接,而是 API 链接,因此您无法访问 Google Drive。
有没有获取该链接的方法?
这是返回的链接,不是这个
https://www.googleapis.com/drive/v3/files/1m4Ja7qCNrfqhrkrtKibuPoV02Shp5w7G2?
我要这个
https://drive.google.com/drive/u/0/folders/1ObHbN1heqwewgx1RkyEJ1Bm4GL3TSTjmI
谢谢你。
require "google/apis/drive_v3"
require "googleauth"
require "googleauth/stores/file_token_store"
require "fileutils"
OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
APPLICATION_NAME = "upload-image-to-gdrive".freeze
CREDENTIALS_PATH = "credentials.json".freeze
TOKEN_PATH = "token.yaml".freeze
SCOPE = Google::Apis::DriveV3::AUTH_DRIVE
class DriveUploader
def initialize
# Initialize the API
@@drive = Google::Apis::DriveV3
@@service = @@drive::DriveService.new
@@service.client_options.application_name = APPLICATION_NAME
@@service.authorization = authorize
end
def authorize
client_id = Google::Auth::ClientId.from_file CREDENTIALS_PATH
token_store = Google::Auth::Stores::FileTokenStore.new file: TOKEN_PATH
authorizer = Google::Auth::UserAuthorizer.new client_id, SCOPE, token_store
user_id = "default"
credentials = authorizer.get_credentials user_id
credentials
end
def upload(name, image_source)
file_metadata = {
name: name,
parents: ['1ObHbN11jdig94x1RkyEJ1Bm4GL3TSTjmI'] # Drive ID
}
file = @@service.create_file(
file_metadata,
fields: 'id',
upload_source: image_source,
#content_type: 'image/jpeg' # 'application/pdf'
content_type: 'application/pdf' # 'application/pdf'
)
user_permission = {type: 'anyone', role: 'reader'}
@@service.create_permission(file.id, user_permission, fields: 'id')
file
end
end
解决方案
将文件上传到您的驱动器并检索其 Id 后,您可以使用方法Files: get以获取
file resource
.文件资源包含文件的所有元数据,如文档中所述
对您有用的可能是 the
webContentLink
或 thewebViewLink
- 两者都可以通过
fields
在drive.files.get
方法中指定它们来获得
推荐阅读
- python - pip3.7 ubuntu 16 pip 配置了需要 TLS/SSL 的位置,但是 Python 中的 ssl 模块不可用
- electron - 使用电子生成器构建我的 create-react-app 失败
- python - 在 python 中使用 selenium 处理帧
- sympy - x+x^1.0 不会使用 sympy 进行简化
- ios - iOS - 无法让自定义视图在 UIViewController 中居中
- c++ - 检测 std::shared_ptr 是否持有原始数组(并获取其大小)
- python - 网页美化 html带bs4
- php - MongoDB 不会在 PHP 中发出 JSON 文件
- bash - 如何授予 apache 用户使用 ssh 密钥的权限
- java - 当我尝试在按钮上使用 setText() 时,为什么 Android Studio 会显示错误?