在rust中如何操作阿里云oss文件,阿里云官方并沒有提供這樣的sdk句伶,我們可以使用社區(qū)的
aliyun-oss-rust-sdk
庫crate來實現(xiàn)。
功能列表
添加依賴
[dependencies]
# 異步
aliyun-oss-rust-sdk = { version = "x.x.x"}
# 同步
aliyun-oss-rust-sdk = { version = "x.x.x", features = ["blocking"] }
# debug日志開啟
aliyun-oss-rust-sdk = { version = "x.x.x", features = ["blocking","debug-print"] }
文件下載
use aliyun_oss_rust_sdk::oss::OSS;
use aliyun_oss_rust_sdk::request::RequestBuilder;
let oss = OSS::from_env();
let build = RequestBuilder::new();
let bytes = oss.get_object("/hello.txt", build).unwrap();
println!("file content: {}", String::from_utf8_lossy(bytes.as_slice()));
簽名下載
自定義域名/限速下載/過期時間/自定義content-type
use aliyun_oss_rust_sdk::oss::{OSS,RequestBuilder};
use aliyun_oss_rust_sdk::url::UrlApi;
let oss = OSS::new(
"my_key_id",
"my_key_secret",
"oss-cn-shanghai.aliyuncs.com",
"my_bucket",
);
let build = RequestBuilder::new()
.with_expire(60)
//.with_cdn("https://mydomain.com") //使用cdn后,無法限制ip訪問
.oss_download_speed_limit(30);
let download_url = oss.sign_download_url(
"/ipas/cn/-10/imem內存修改器_1.0.0.ipa",
&build,
);
println!("download_url: {}", download_url);
簽名上傳
. 允許前端簡單上傳文件,精確控制請用功能4:獲取上傳對象的policy方式上傳
. 自定義域名/限速上傳/過期時間/自定義content-type
use aliyun_oss_rust_sdk::oss::{OSS, RequestBuilder};
use aliyun_oss_rust_sdk::url::UrlApi;
let oss = OSS::from_env();//也可以使用OSS::new()方法傳遞參數(shù)
let build = RequestBuilder::new()
//.with_cdn("https://mydomain.com")
.with_content_type("text/plain") //設置上傳文件的content-type
.with_expire(60); //60秒鏈接過期
let upload_url = oss.sign_upload_url(
"tmp.txt",
&build
);
println!("upload_url: {}", upload_url);
//使用postman測試上傳即可斟览,PS:要注意content-type要和build中的一致
獲取上傳對象的policy
用于前端直傳可精確控制上傳文件的類型、大小灼狰、過期時間袱瓮、上傳目錄等
use aliyun_oss_rust_sdk::entity::PolicyBuilder;
use aliyun_oss_rust_sdk::oss::OSS;
let oss = OSS::from_env();
let policy_builder = PolicyBuilder::new()
.with_expire(60 * 60)//1個小時過期
.with_upload_dir("upload/mydir/")//上傳目錄
.with_content_type("text/plain")//只允許上傳文本.txt
.with_max_upload_size(100 * 1024 * 1024);//只允許文件上傳大小1G以內
let policy = oss.get_upload_object_policy(policy_builder).unwrap();
println!("policy: {:?}", policy);
//使用postman測試上傳
//form-data的參數(shù)為OSSAccessKeyId缤骨、policy、signature尺借、success_action_status绊起、key、file
//key為上傳的文件名包含路徑燎斩、例如:upload/mydir/test.txt
//file為上傳的文件虱歪,類型跟with_content_type一致
上傳本地文件
use aliyun_oss_rust_sdk::oss::OSS;
use aliyun_oss_rust_sdk::request::RequestBuilder;
let oss = OSS::from_env();
let builder = RequestBuilder::new()
.with_expire(60);
let file_path = "./hello.txt";
oss.put_object_from_file("/hello.txt", file_path, builder).unwrap();
上傳內存文件
use aliyun_oss_rust_sdk::oss::OSS;
use aliyun_oss_rust_sdk::request::RequestBuilder;
let oss = OSS::from_env();
let builder = RequestBuilder::new()
.with_expire(60);
let file_path = "./hello.txt";
let buffer = std::fs::read(file_path).unwrap();
oss.pub_object_from_buffer("/hello.txt", buffer.as_slice(), builder).unwrap();
文件刪除
use aliyun_oss_rust_sdk::oss::OSS;
use aliyun_oss_rust_sdk::request::RequestBuilder;
let oss = OSS::from_env();
let builder = RequestBuilder::new()
.with_expire(60);
oss.delete_object("/hello.txt", builder).unwrap();