7个版本
0.1.6 | 2023年2月25日 |
---|---|
0.1.5 | 2023年2月25日 |
#323 in 身份验证
每月39次下载
18KB
364 代码行
对象存储
该库的目标是创建一个可以使用“阿里云oss”和“腾讯云cos”的库,简化使用步骤,让开发能够专注于业务逻辑。
oss
oss桶列表
use object_storage::oss::Auth;
#[tokio::main]
async fn main() {
let auth = Auth::new("your aliyun oss AccessKeyId", "your aliyun oss AccessKeySecret").await;
let buck_list = auth.list_bucket().await.unwarp();
println!("{:#?}", buck_list);
}
oss上传对象
简单单个文件上传
use object_storage::oss::{Auth,Bucket};
#[tokio::main]
async fn main() {
let auth = Auth::new("your aliyun oss AccessKeyId", "your aliyun oss AccessKeySecret").await;
let bucket = Bucket::new(auth, "your endpoint", "your bucket name").await.unwrap();
let object_url = bucket.read_put_object("path").await.unwarp();
}
tokio
使用tokio同时上传多个文件
use object_storage::oss::{Auth, Bucket};
use std::sync::Arc;
use tokio::sync::Mutex;
#[tokio::main]
async fn main() {
let auth = Auth::new("your aliyun oss AccessKeyId", "your aliyun oss AccessKeySecret").await;
let bucket = Arc::new(Mutex::new(Bucket::new(auth, "your endpoint", "your bucket name").await.unwrap()));
let mut handles = Vec::new();
handles.push(tokio::spawn({
let bucket = bucket.clone();
async move {
let guard = bucket.lock().await;
guard.read_put_object("/Users/joeywang/Downloads/1.jpg").await
}
}));
handles.push(tokio::spawn({
let bucket = bucket.clone();
async move {
let guard = bucket.lock().await;
guard.read_put_object("/Users/joeywang/Downloads/2.jpg").await
}
}));
handles.push(tokio::spawn({
let bucket = bucket.clone();
async move {
let guard = bucket.lock().await;
guard.read_put_object("/Users/joeywang/Downloads/3.jpg").await
}
}));
handles.push(tokio::spawn({
let bucket = bucket.clone();
async move {
let guard = bucket.lock().await;
guard.read_put_object("/Users/joeywang/Downloads/4.png").await
}
}));
handles.push(tokio::spawn({
let bucket = bucket.clone();
async move {
let guard = bucket.lock().await;
guard.read_put_object("/Users/joeywang/Downloads/5.png").await
}
}));
handles.push(tokio::spawn({
let bucket = bucket.clone();
async move {
let guard = bucket.lock().await;
guard.read_put_object("/Users/joeywang/Downloads/6.png").await
}
}));
handles.push(tokio::spawn({
let bucket = bucket.clone();
async move {
let guard = bucket.lock().await;
guard.read_put_object("/Users/joeywang/Downloads/7.jpg").await
}
}));
handles.push(tokio::spawn({
let bucket = bucket.clone();
async move {
let guard = bucket.lock().await;
guard.read_put_object("/Users/joeywang/Downloads/8.jpg").await
}
}));
for handle in handles {
match handle.await {
Ok(result) => {
match result {
Ok(url) => println!("Upload succeeded: {}", url),
Err(e) => eprintln!("Upload failed: {}", e),
}
},
Err(e) => eprintln!("Failed to spawn task: {}", e),
}
}
}
依赖
~8–23MB
~327K SLoC