pub trait ObjectAPI {
    // Required methods
    fn get_object<S: AsRef<str>>(
        &self,
        key: S,
        build: &RequestBuilder
    ) -> Result<Vec<u8>>;
    fn get_upload_object_policy(
        &self,
        build: &PolicyBuilder
    ) -> Result<PolicyResp>;
    fn put_object_from_file<S: AsRef<str>>(
        &self,
        key: S,
        file_path: S,
        build: &RequestBuilder
    ) -> Result<()>;
    fn pub_object_from_buffer<S: AsRef<str>>(
        &self,
        key: S,
        buffer: &[u8],
        build: &RequestBuilder
    ) -> Result<()>;
    fn delete_object<S: AsRef<str>>(
        &self,
        key: S,
        build: &RequestBuilder
    ) -> Result<()>;
}

Required Methods§

source

fn get_object<S: AsRef<str>>( &self, key: S, build: &RequestBuilder ) -> Result<Vec<u8>>

获取对象

使用例子
use aliyun_oss_rust_sdk::object::ObjectAPI;
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()));
source

fn get_upload_object_policy(&self, build: &PolicyBuilder) -> Result<PolicyResp>

获取上传对象的policy

使用例子
use aliyun_oss_rust_sdk::object::{ObjectAPI, 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一致
source

fn put_object_from_file<S: AsRef<str>>( &self, key: S, file_path: S, build: &RequestBuilder ) -> Result<()>

上传文件(本地文件)

使用例子
use aliyun_oss_rust_sdk::object::ObjectAPI;
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();
source

fn pub_object_from_buffer<S: AsRef<str>>( &self, key: S, buffer: &[u8], build: &RequestBuilder ) -> Result<()>

上传文件(内存)

使用例子
use aliyun_oss_rust_sdk::object::ObjectAPI;
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();
source

fn delete_object<S: AsRef<str>>( &self, key: S, build: &RequestBuilder ) -> Result<()>

删除文件

使用例子
use aliyun_oss_rust_sdk::object::ObjectAPI;
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();

Object Safety§

This trait is not object safe.

Implementors§