1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*!
# aliyun-oss-rust-sdk
[![Latest Version](https://img.shields.io/crates/v/aliyun-oss-rust-sdk.svg)](https://crates.io/crates/aliyun-oss-rust-sdk)

阿里云 © Alibaba Cloud Official Oss SDK (标准库)

[![QQ群](https://img.shields.io/badge/QQ%E7%BE%A4-799168925-blue)](http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=dLoye8pBcO60zGzqLjGO0l-GgMIaf6wQ&authKey=LfxBdZ5A%2F9eWJbKpzTcuWPjmQu5UdIJ3TVTpqRAQYkCID50WLkYoIXcGxGKzupG3&noverify=0&group_code=799168925)

# 使用指南

1. [文件下载](#文件下载)
2. [签名下载](#签名下载)
3. [签名上传](#签名上传)
4. [获取上传对象的policy](#获取上传对象的policy)
5. [上传本地文件](#上传本地文件)
6. [上传内存文件](#上传内存文件)
7. [文件删除](#文件删除)

添加依赖
```toml
[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"] }
```

## 文件下载
```rust
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
```rust
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
```rust
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中的一致
```

## 获取上传对象的policy
用于前端直传可精确控制上传文件的类型、大小、过期时间、上传目录等
```rust
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一致
```

## 上传本地文件
```rust
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();
```
## 上传内存文件
```rust
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();
```
## 文件删除
```rust
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();
```
 */
pub mod auth;
pub mod oss;
pub mod request;
pub mod url;
mod util;

#[cfg(feature = "blocking")]
pub mod blocking;
#[cfg(not(feature = "blocking"))]
pub mod async_impl;
pub mod entity;
pub mod error;
pub(crate) mod macros;