27 个版本 (重大更新)
0.20.0 | 2024 年 6 月 27 日 |
---|---|
0.17.0 | 2024 年 4 月 18 日 |
0.16.0 | 2024 年 2 月 27 日 |
0.15.0 | 2023 年 12 月 3 日 |
0.3.0 | 2022 年 7 月 10 日 |
#60 在 网页开发 中
每月 129,932 次下载
用于 33 个包 (直接使用 10)
305KB
4.5K SLoC
google-cloud-storage
Google Cloud Platform 存储客户端库。
安装
[dependencies]
google-cloud-storage = "version"
快速入门
身份验证
创建针对 Google Cloud 进行身份验证的客户端有两种方式。
自动
函数 with_auth()
将尝试从环境变量 GOOGLE_APPLICATION_CREDENTIALS
、GOOGLE_APPLICATION_CREDENTIALS_JSON
或元数据服务器中读取凭据。
这也在 google-cloud-auth 中进行了描述
use google_cloud_storage::client::{ClientConfig, Client};
async fn run() {
let config = ClientConfig::default().with_auth().await.unwrap();
let client = Client::new(config);
}
手动
当您不能使用 gcloud
身份验证但您有其他方式获取凭据(例如不同的环境变量)时,您可以解析自己的 'credentials-file' 并像这样使用它
use google_cloud_auth::credentials::CredentialsFile;
// or google_cloud_storage::client::google_cloud_auth::credentials::CredentialsFile
use google_cloud_storage::client::{ClientConfig, Client};
async fn run(cred: CredentialsFile) {
let config = ClientConfig::default().with_credentials(cred).await.unwrap();
let client = Client::new(config);
}
匿名访问
为了提供 无需身份验证的匿名访问,请执行以下操作。
use google_cloud_storage::client::{ClientConfig, Client};
async fn run() {
let config = ClientConfig::default().anonymous();
let client = Client::new(config);
}
传递自定义 reqwest 中间件客户端
use google_cloud_storage::client::Client;
use google_cloud_storage::client::ClientConfig;
use google_cloud_storage::http::Error;
use reqwest_middleware::ClientBuilder;
use reqwest_retry::policies::ExponentialBackoff;
use reqwest_retry::RetryTransientMiddleware;
use retry_policies::Jitter;
async fn run() -> Result<(), Error> {
let retry_policy = ExponentialBackoff::builder()
.base(2)
.jitter(Jitter::Full)
.build_with_max_retries(3);
let mid_client = ClientBuilder::new(reqwest::Client::default())
// reqwest-retry already comes with a default retry stategy that matches http standards
// override it only if you need a custom one due to non standard behaviour
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
.build();
Client::new(
ClientConfig {
http: Some(mid_client),
..Default::default()
}
.with_auth()
.await?,
);
Ok(())
}
用法
use google_cloud_storage::client::Client;
use google_cloud_storage::client::ClientConfig;
use google_cloud_storage::sign::SignedURLOptions;
use google_cloud_storage::sign::SignBy;
use google_cloud_storage::sign::SignedURLMethod;
use google_cloud_storage::http::Error;
use google_cloud_storage::http::objects::download::Range;
use google_cloud_storage::http::objects::get::GetObjectRequest;
use google_cloud_storage::http::objects::upload::{Media, UploadObjectRequest, UploadType};
use tokio::task::JoinHandle;
use std::fs::File;
use std::io::BufReader;
use std::io::Read;
async fn run(config: ClientConfig) -> Result<(), Error> {
// Create client.
let mut client = Client::new(config);
// Upload the file
let upload_type = UploadType::Simple(Media::new("file.png"));
let uploaded = client.upload_object(&UploadObjectRequest {
bucket: "bucket".to_string(),
..Default::default()
}, "hello world".as_bytes(), &upload_type).await;
// Download the file
let data = client.download_object(&GetObjectRequest {
bucket: "bucket".to_string(),
object: "file.png".to_string(),
..Default::default()
}, &Range::default()).await;
// Create signed url with the default key and google-access-id of the client
let url_for_download = client.signed_url("bucket", "foo.txt", None, None, SignedURLOptions::default());
let url_for_upload = client.signed_url("bucket", "foo.txt", None, None, SignedURLOptions {
method: SignedURLMethod::PUT,
..Default::default()
});
Ok(())
}
依赖项
~16–31MB
~616K SLoC