spring-qiniu

Crates.iospring-qiniu
lib.rsspring-qiniu
version0.1.2
sourcesrc
created_at2024-11-05 04:12:42.558042
updated_at2024-11-05 08:03:45.031687
description简单上传文件到七牛云
homepage
repositoryhttps://gitea.1000duo.cn/qianduo/spring-qiniu.git
max_upload_size
id1436001
size14,334
尹旭 (yinxu46)

documentation

README

spring-qiniu

基于官方 qiniu-sdk 简单实现上传图片、视频到七牛云存储。

该版本善存在缺陷:byte 数据上传采用本地缓存再上传到七牛云。官方提供render上传方法过于曲折暂不使用。

由于 github 访问受阻,使用自建 gitea 仓库,仓库地址:https://gitea.1000duo.cn/qianduo/spring-qiniu.git (账号:global 密码:global123456)

#[cfg(test)]
mod tests {
    use bytes::Bytes;
    use spring_qiniu::config::QiniuConfig;
    use spring_qiniu::upload::QiniuUpload;
    use tokio::fs::File;
    use tokio::io::AsyncReadExt;

    fn get_qiniu_upload() -> QiniuUpload {
        let access_key = "access_key";
        let secret_key = "secret_key";
        let bucket_name = "bucket_name";
        let config = QiniuConfig {
            access_key: access_key.to_string(),
            secret_key: secret_key.to_string(),
            bucket_name: bucket_name.to_string(),
            domain: "https://cdn.domain.cn".to_string(),
            image_size: 1024*1024*2,
            video_size: 1024*1024*10,
            image_ext: vec!["jpg".to_string(), "png".to_string(), "gif".to_string(), "bmp".to_string(), "jpeg".to_string(), "webp".to_string()],
            video_ext: vec!["mp4".to_string(), "avi".to_string(), "wmv".to_string(), "mov".to_string()],
        };
        QiniuUpload::new(config)
    }

    #[tokio::test]
    async fn test_upload_image() {
        let upload = get_qiniu_upload();
        println!("{:?}", upload);
        let resp = upload.upload_image("sys", "test.png").await;
        println!("{:?}", resp);
        if let Ok(file) = resp {
            println!("{:?}", file.url());
        }
    }

    #[tokio::test]
    async fn test_upload_image_render() {
        let upload = get_qiniu_upload();
        println!("{:?}", upload);
        let file = File::open("test.png").await;
        if file.is_err() {
            eprintln!("file open failed");
            return;
        }
        let mut file = file.unwrap();
        println!("{:?}", file);
        let metadata = file.metadata().await.expect("TODO: panic message");
        let mut bytes = Vec::with_capacity(metadata.len() as usize);
        file.read_to_end(&mut bytes).await.expect("TODO: panic message");

        let byte = Bytes::from(bytes);
        let resp = upload.upload_image_bytes("sys", "png", &byte).await;
        println!("{:?}", resp);
        if let Ok(file) = resp {
            println!("{:?}", file.url());
        }
    }

    #[tokio::test]
    async fn test_upload_video() {
        let upload = get_qiniu_upload();
        println!("{:?}", upload);
        let resp = upload.upload_video("sys", "test.mp4").await;
        println!("{:?}", resp);
        if let Ok(file) = resp {
            println!("{:?}", file.url());
        }
    }

    #[tokio::test]
    async fn test_upload_video_render() {
        let upload = get_qiniu_upload();
        println!("{:?}", upload);
        let file = File::open("test.mp4").await;
        if file.is_err() {
            eprintln!("file open failed");
            return;
        }
        let mut file = file.unwrap();
        println!("{:?}", file);
        let metadata = file.metadata().await.expect("TODO: panic message");
        let mut bytes = Vec::with_capacity(metadata.len() as usize);
        file.read_to_end(&mut bytes).await.expect("TODO: panic message");

        let byte = Bytes::from(bytes);
        let resp = upload.upload_video_bytes("sys", "mp4", &byte).await;
        println!("{:?}", resp);
        if let Ok(file) = resp {
            println!("{:?}", file.url());
        }
    }
}
Commit count: 0

cargo fmt