作者:京東科技 賈世聞
物件儲存是雲的基礎元件之一,各大雲廠商都有相關產品。這裡跟大家介紹一下rust與物件儲存交到的基本套路和其中的一些技巧。
基本連線
我們以 [S3 sdk](
https://github.com/awslabs/aws-sdk-rust)為例來說說基本的連線與操作,作者驗證過aws、京東雲、阿里雲。主要的增刪改查功能沒有什麼差別。
- 建立客戶端
let shared_config = SdkConfig::builder()
.credentials_provider(SharedCredentialsProvider::new(Credentials::new(
"LTAI5t7NPuPKsXm6UeSa1",
"DGHuK03ESXQYqQ83buKMHs9NAwz",
None,
None,
"Static",
)))
.endpoint_url("http://oss-cn-beijing.aliyuncs.com")
.region(Region::new("oss-cn-beijing"))
.build();
let s3_config_builder = aws_sdk_s3::config::Builder::from(&shared_config);
let client = aws_sdk_s3::Client::from_conf(s3_config_builder.build());
建立Client所需要的引數主要有你需要訪問的oss的AK、SK,endpoint url 以及服務所在的區域。以上資訊都可以在服務商的幫助文件查詢到。
- 物件列表
let mut obj_list = client
.list_objects_v2()
.bucket(bucket)
.max_keys(max_keys)
.prefix(prefix_str)
.continuation_token(token_str);
let list = obj_list.send().await.unwrap();
println!("{:?}",list.contents());
println!("{:?}",list.next_continuation_token());
使用list_objects_v2函式返回物件列表,相比list_objects函式,list_objects_v2可以透過continuation_token和max_keys控制返回列表的長度。list.contents()返回物件列表陣列,
list.next_continuation_token()返回繼續查詢的token。
- 上傳檔案
let content = ByteStream::from("content in file".as_bytes());
let exp = aws_smithy_types::DateTime::from_secs(100);
let upload = client
.put_object()
.bucket("bucket")
.key("/test/key")
.expires(exp)
.body(content);
upload.send().await.unwrap();
指定bucket及物件路徑,body接受ByteStream型別作為檔案內容,最後設定過期時間expires,無過期時間時不指定該配置即可。
- 下載檔案
let key = "/tmp/test/key".to_string();
let resp = client
.get_object()
.bucket("bucket")
.key(&key)
.send()
.await.unwrap();
let data = resp.body.collect().await.unwrap();
let bytes = data.into_bytes();
let path = std::path::Path::new("/tmp/key")
if let Some(p) = path.parent() {
std::fs::create_dir_all(p).unwrap();
}
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(path).unwrap();
let _ = file.write(&*bytes);
file.flush().unwrap();
透過get_object()函式獲取GetObjectOutput。返回值的body 就是檔案內容,將 body 轉換為 bytes,最後開啟檔案寫入即可。
- 刪除檔案
let mut keys = vec![];
let key1 = ObjectIdentifier::builder()
.set_key(Some("/tmp/key1".to_string()))
.build();
let key2 = ObjectIdentifier::builder()
.set_key(Some("/tmp/key2".to_string()))
.build()
keys.push(key1);
keys.push(key2)
client
.delete_objects()
.bucket(bucket)
.delete(Delete::builder().set_objects(Some(keys)).build())
.send()
.await
.unwrap();
delete_objects 批次刪除物件。首先構建keys vector,定義要刪除的物件,然後透過Delete::builder(),構建 Delete model。
大檔案上傳
let mut file = fs::File::open("/tmp/file_name").unwrap();
let chunk_size = 1024*1024;
let mut part_number = 0;
let mut upload_parts: Vec = Vec::new();
//獲取上傳id
let multipart_upload_res: CreateMultipartUploadOutput = self
.client
.create_multipart_upload()
.bucket("bucket")
.key("/tmp/key")
.send()
.await.unwrap();
let upload_id = match multipart_upload_res.upload_id() {
Some(id) => id,
None => {
return Err(anyhow!("upload id is None"));
}
};
//分段上傳檔案並記錄completer_part
loop {
let mut buf = vec![0; chuck_size];
let read_count = file.read(&mut buf)?;
part_number += 1;
if read_count == 0 {
break;
}
let body = &buf[..read_count];
let stream = ByteStream::from(body.to_vec());
let upload_part_res = self
.client
.upload_part()
.key(key)
.bucket(bucket)
.upload_id(upload_id)
.body(stream)
.part_number(part_number)
.send()
.await.unwrap();
let completer_part = CompletedPart::builder()
.e_tag(upload_part_res.e_tag.unwrap_or_default())
.part_number(part_number)
.build();
upload_parts.push(completer_part);
if read_count != chuck_size {
break;
}
}
// 完成上傳檔案合併
let completed_multipart_upload: CompletedMultipartUpload =
CompletedMultipartUpload::builder()
.set_parts(Some(upload_parts))
.build();
let _complete_multipart_upload_res = self
.client
.complete_multipart_upload()
.bucket("bucket")
.key(key)
.multipart_upload(completed_multipart_upload)
.upload_id(upload_id)
.send()
.await.unwrap();
有時候面對大檔案,比如幾百兆甚至幾個G的檔案,為了節約頻寬和記憶體,我才採取分段上傳的方案,然後在物件儲存的服務端做合併。基本流程是:指定bucket和key,獲取一個上傳id;按流讀取檔案,分段上傳位元組流,並記錄CompletedPart;通知伺服器按照CompletedPart 集合來合併檔案。具體過程程式碼已加註釋,這裡不再累述。
大檔案下載
let mut file = match OpenOptions::new()
.truncate(true)
.create(true)
.write(true)
.open("/tmp/target_file");
let key = "/tmp/test/key".to_string();
let resp = client
.get_object()
.bucket("bucket")
.key(&key)
.send()
.await.unwrap();
let content_len = resp.content_length();
let mut byte_stream_async_reader = resp.body.into_async_read();
let mut content_len_usize: usize = content_len.try_into().unwrap();
loop {
if content_len_usize > chunk_size {
let mut buffer = vec![0; chunk_size];
let _ = byte_stream_async_reader.read_exact(&mut buffer).await.unwrap();
file.write_all(&buffer).unwrap();
content_len_usize -= chunk_size;
continue;
} else {
let mut buffer = vec![0; content_len_usize];
let _ = byte_stream_async_reader.read_exact(&mut buffer).await.unwrap();
file.write_all(&buffer).unwrap();
break;
}
}
file.flush().unwrap();
在從物件儲存服務端下載檔案的過程中也會遇到大檔案問題。為了節約頻寬和記憶體,我們採取讀取位元組流的方式分段寫入檔案。首先get_object()函式獲取ByteStream,透過async_reader流式讀取物件位元組,分段寫入檔案。
物件儲存的相關話題今天先聊到這兒,下期見。