#oss #sdk #aliyun #prelude #header #object

aliyun-oss-sdk

阿里云 OSS SDK

1 个不稳定版本

0.2.1 2021 年 9 月 9 日

#13 in #oss

Apache-2.0

30KB
844 行代码(不含注释)

入门

列出您的存储桶

use oss_rust_sdk::prelude::*;
let oss_instance = OSS::new("your_AccessKeyId", "your_AccessKeySecret", "your_Endpoint", "your_Bucket");
let list_your_Buckets = oss_instance.list_your_Bucket(None).unwrap();

let id = list_your_Buckets.id();
let your_Buckets = list_your_Buckets.your_Buckets();
let your_Bucket_names: Vec<&str> = your_Buckets.iter().map(|obj| obj.name()).collect();

获取对象

use oss_rust_sdk::prelude::*;
use std::collections::HashMap;

let oss_instance = OSS::new("your_AccessKeyId", "your_AccessKeySecret", "your_Endpoint", "your_Bucket");

/// if have extra header
let mut extra_header = HashMap::new();
extra_header.insert("content-type", "text/plain");
/// if have oss_sub_resource
let mut oss_sub_resource = HashMap::new();
oss_sub_resource.insert("acl", None);
oss_sub_resource.insert("response-content-type", Some("ContentType"));

let result = oss_instance.get_object("object", extar_header, oss_sub_resource);
/// or you may just get object
/// let result = oss_instance.get_object("object", None, None);
assert_eq!(result.is_ok(), true);
let buffer = result.unwrap();

异步获取对象

use tokio::runtime::Runtime;

fn async_get_object_demo() {
    let oss_instance = OSS::new("your_AccessKeyId", "your_AccessKeySecret", "your_Endpoint", "your_Bucket");

    let mut rt = Runtime::new().expect("failed to start runtime");

    rt.block_on(async move {
        let _result = oss_instance.async_get_object("objectName", None, None).await.unwrap();
        println!("buffer = {:?}", String::from_utf8(result.unwrap()));
    });
}

or

async fn async_get_object_demo() -> Reuslt<String, Error> {
    let oss_instance = OSS::new("your_AccessKeyId", "your_AccessKeySecret", "your_Endpoint", "your_Bucket");
    let buf = oss_instance.async_get_object("objectName", None, None).await?;
    String::from_utf8(buf)?
}

通过文件上传对象

use oss_rust_sdk::prelude::*;
let filename = "filename";
let oss_instance = OSS::new("your_AccessKeyId", "your_AccessKeySecret", "your_Endpoint", "your_Bucket");
let result = oss_instance.put_object_from_file(filename, "object", None, None);
assert_eq!(result.is_ok(), true)

通过缓冲区上传对象

let buffer = "some thing you want put to oss";
let oss_instance = OSS::new("your_AccessKeyId", "your_AccessKeySecret", "your_Endpoint", "your_Bucket");
let result = oss_instance.put_object_from_buffer(buffer.as_bytes(), "object", None, None);
assert_eq!(result.is_ok(), true)

异步上传对象

let buffer = "test async put object from buffer";
let oss_instance = OSS::new("your_AccessKeyId", "your_AccessKeySecret", "your_Endpoint", "your_Bucket");
let mut headers = HashMap::new();
headers.insert("content-type", "text/plain");

oss_instance.async_put_object_from_buffer(buffer.as_bytes(),"your_object_name", headers,None).await?;

复制对象

use oss_rust_sdk::prelude::*;
let oss_instance = OSS::new("your_AccessKeyId", "your_AccessKeySecret", "your_Endpoint", "your_Bucket");
let result = oss_instance.copy_object_from_object("src_object", "dest_object", None, None);
assert_eq!(result.is_ok(), true)

删除对象

use oss_rust_sdk::prelude::*;
let oss_instance = OSS::new("your_AccessKeyId", "your_AccessKeySecret", "your_Endpoint", "your_Bucket");
let result = oss_instance.delete_object("object");
assert_eq!(result.is_ok(), true)

如果您想在创建 oss 实例后更改您的存储桶,可以使用 oss_instance.set_your_Bucket("your_Bucket") 进行更改

待办事项

  • 完成对象 API
  • 完成存储桶 API
  • 支持异步 API

许可证

  • Apache License 2.0.

依赖项

~9–14MB
~270K SLoC