9个不稳定版本 (3个重大更改)
0.6.1 | 2024年2月23日 |
---|---|
0.6.0 | 2024年2月19日 |
0.5.2 | 2024年2月11日 |
0.5.1 | 2024年1月27日 |
0.3.1 | 2024年1月20日 |
在 身份验证 中排名 345
每月下载 216 次
用于 drive-v3-macros
420KB
5.5K SLoC
drive-v3
一个Rust库,用于向Google Drive API v3发送请求。
此库包含调用Google Drive API v3中所有Google文档的函数,截至最新发布日期。
用法
设置
在使用Google Drive的API之前,您需要在Google Cloud项目中启用它。
这是免费的,您可以通过这些 步骤 启用API。
完成 授权桌面应用程序凭据 部分后,您可以使用保存的 client_secrets.json
文件来授权此库。
获取凭据
在您开始调用Google的API之前,您首先需要获取一些有效的 Credentials
。
为此,您需要您的 client_secrets.json
文件以及您计划使用的范围。有关选择范围的帮助,您可以阅读Google的 选择范围 指南。
有了这些元素,您可以通过调用 Credentials::from_client_secrets_file
来提示您通过浏览器授权您的应用程序
use drive_v3::Credentials;
// This is the file downloaded in the Setup section
let client_secrets_path = "my_client_secrets.json";
// The OAuth scopes you need
let scopes: [&'static str; 2] = [
"https://www.googleapis.com/auth/drive.metadata.readonly",
"https://www.googleapis.com/auth/drive.file",
];
let credentials = Credentials::from_client_secrets_file(&client_secrets_path, &scopes)?;
太好了!现在您有有效的凭据可以调用Google的API,但是这些凭据会在一段时间后过期,届时您将不得不再次请求它们。
为了避免每次过期时都必须通过浏览器进行授权,您应该将它们保存到您的系统中
credentials.store("very/secure/path/to/my/credentials.json")?;
现在您不必担心每次都要请求新的凭据,而是只需检查它们是否已过期,并在必要时刷新它们
use drive_v3::Credentials;
// Path where the credentials were stored
let credentials_storage_path = "very/secure/path/to/my/credentials.json";
// The OAuth scopes you need
let scopes: [&'static str; 2] = [
"https://www.googleapis.com/auth/drive.metadata.readonly",
"https://www.googleapis.com/auth/drive.file",
];
let mut stored_credentials = Credentials::from_file(&credentials_storage_path, &scopes)?;
// Refresh the credentials if they have expired
if !stored_credentials.are_valid() {
stored_credentials.refresh()?;
// Save them so we don't have to refresh them every time
stored_credentials.store(&credentials_storage_path)?;
}
发送请求
现在我们有了有效的 Credentials
,我们可以开始发送请求。为此,我们需要创建一个 Drive
。
Drive
是API本身的表示,通过它我们可以向Google API中的任何端点发送请求。
要创建它,我们只需要传递给它 Credentials
use drive_v3::Drive;
let drive = Drive::new(&credentials);
然后发出您想要的请求,例如列出 Drive 中的文件
let file_list = drive.files.list()
.fields("files(name, id, mimeType)") // Set what fields will be returned
.q("name = 'file_im_looking_for' and not trashed") // search for specific files
.execute()?;
if let Some(files) = file_list.files {
for file in &files {
println!("{}", file);
}
}
如您所见,files.list
函数使用了 builder 模式,这允许您指定请求中需要的查询参数。
得益于这种模式,您可以在 Google 的 参考 中搜索特定资源的文档,并使用其查询参数中的任何一个,这些参数将具有对应名称的函数(唯一的区别是函数将使用 snake_case
而不是 camelCase
)。
请求响应
如前一部分所示,要从 Drive 的资源之一获取响应,您必须调用 .execute()
,这将返回一个 Result
,它将是一个 Error
或 Google API 的响应。
响应类型将取决于请求,它可能返回空内容,一个如 File
或 Comment
的对象,或字节数组。您可以在每个请求的文档中检查返回类型。
对象
一些请求将返回一个 object
或需要它在请求中,一个 object
是 Google API 中的类型的表示。
drive-v3
中的所有 objects
都具有相同的结构,一个具有所有字段都是 Option<T>
的结构体。之所以这样做是因为并非所有对象的字段都会被请求填充或需要来创建一个请求。
例如,File
对象有 60 个不同的字段,在创建新文件时必须指定所有字段将是不切实际的。因此,您可以只填充您实际想要设置的字段,其余的留为 None
。这很容易,因为所有对象都实现了 default
特性
use drive_v3::objects::File;
let file = File {
name: Some( "my-file.txt".to_string() ),
mime_type: Some( "text/plain".to_string() ),
description: Some( "this is a text file".to_string() )
..Default::default()
};
至于响应,所有请求都有一个名为 fields()
的函数,您可以将其设置为返回您需要的精确字段,从而提高方法调用的性能。有关字段参数的更多信息,请参阅 Google 的 文档。
更多示例
将新文件上传到 Drive
use drive_v3::objects::{File, UploadType};
// Set what information the uploaded file wil have
let metadata = File {
name: Some( "my-new-file.txt".to_string() ),
mime_type: Some( "text/plain".to_string() ),
..Default::default()
};
// You can set a callback that will be called when a resumable upload progresses
fn progress_callback( total_bytes: usize, uploaded_bytes: usize ) {
println!("Uploaded {} bytes, out of a total of {}.", uploaded_bytes, total_bytes);
}
let my_new_file = drive.files.create()
.upload_type(UploadType::Resumable)
.callback(progress_callback)
.metadata(&metadata)
.content_source("path/to/file.txt")
.execute()?;
assert_eq!(my_new_file.name, metadata.name);
assert_eq!(my_new_file.mime_type, metadata.mime_type);
从 Drive 下载文件
let file_id = "some-file-id";
let file_bytes = drive.files.get_media(&file_id)
// .save_to("my_downloaded_file.txt") // Save the contents to a path
.execute()?;
let content = String::from_utf8_lossy(&file_bytes);
println!("file content: {}", content);
获取有关 Drive 的信息
let information = drive.about.get()
.fields("storageQuota, canCreateDrives, appInstalled")
.execute()?;
println!("look at all this information:\n{}", information);
贡献
欢迎提交拉取请求。对于重大更改,请先打开一个问题。
许可证
MIT
依赖项
~8–22MB
~338K SLoC