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
// Copyright 2024 Yen Harvey
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! # COS 上传库
//!
//! `cos_upload` 是一个用于将文件临时上传到腾讯云对象存储(COS)的 Rust 库。
//! 支持普通上传和分块上传,可以根据文件大小(默认 5MB)自动选择合适的上传方式。
//!
//! ## 功能亮点
//! - 支持普通上传和分块上传,根据文件大小自动选择
//! - 支持自定义对象元数据(例如用户 ID、用户名、上传时间等)
//!
//! ## 示例
//!
//! 以下是一个基本的使用示例,展示了如何上传文件,并附带自定义的元数据。
//!
//! ```rust
//! use anyhow::Result;
//! use chrono::Utc;
//! use cos_upload::{Config, Uploader};
//! use std::collections::HashMap;
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! dotenv::dotenv().ok();
//!
//! // 从环境变量创建配置
//! let config = Config::from_env()?;
//!
//! // 或者手动创建配置
//! // let config = Config::new(
//! // "secret_id".to_string(),
//! // "secret_key".to_string(),
//! // "region".to_string(),
//! // "bucket".to_string()
//! // );
//!
//! // 创建上传器实例
//! let uploader = Uploader::new(config);
//!
//! // 创建并添加对象元数据
//! let mut metadata = HashMap::new();
//! metadata.insert("user-id".to_string(), "123".to_string());
//! metadata.insert("username".to_string(), "sample_user".to_string());
//! metadata.insert("source".to_string(), "sample_source".to_string());
//! metadata.insert("upload-time".to_string(), Utc::now().to_rfc3339());
//!
//! // 上传文件,使用通用文件路径示例
//! let file_path = "path/to/local/testfile";
//! let object_key = "uploads/user_123/sample_file"; // 按用户组织路径
//!
//! match uploader.upload_file(file_path, object_key, Some(metadata)).await {
//! Ok(url) => println!("文件上传成功。URL: {}", url),
//! Err(e) => eprintln!("文件上传失败: {}", e),
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ## 注意事项
//! - 在运行此示例之前,请确保设置了正确的环境变量或在代码中提供了准确的配置数据。
//! - 使用 `metadata` 字典来存储和传递自定义的元数据信息,这些信息将附加到上传的对象中,便于后续查询。
//! - 文件路径和对象键(`object_key`)可以根据业务需求自定义,例如按用户 ID 组织的路径结构,以更好地管理上传的资源。
pub use Config;
pub use Uploader;