Crates.io | aliyun-oss-rust-sdk |
lib.rs | aliyun-oss-rust-sdk |
version | 0.2.1 |
source | src |
created_at | 2024-01-04 10:06:23.235102 |
updated_at | 2024-03-25 14:08:00.838135 |
description | 阿里云 © Alibaba Cloud Official Oss SDK (标准库) |
homepage | |
repository | https://github.com/dounine/aliyun-oss-rust-sdk |
max_upload_size | |
id | 1088318 |
size | 72,605 |
阿里云 © Alibaba Cloud Official Oss SDK (标准库)
添加依赖
[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()方法传递参数
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中的一致
用于前端直传可精确控制上传文件的类型、大小、过期时间、上传目录等
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的参数为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();
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();
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();
oss.delete_object("/hello.txt", 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();
let meta = oss.get_object_metadata("/hello.txt", builder).unwrap();
println!("meta: {:?}", meta);