我正在尝试使用 rusoto 从 S3 存储桶中的现有文件中获取正文。我body.unwrap
返回一个空白字符串,我不明白为什么。我没有任何问题或错误,我的文件很好。
看起来我有时会从 S3 收到空内容。这是一个快速而肮脏的补丁,还有其他更好的方法吗?喜欢使用 S3 哈希?
pub fn get_object(
access_key_id: &str,
secret_access_key: &str,
region: &Region,
bucket_name: &str,
object_key: &str,
) -> Result<FileContent, Error> {
let credentials = StaticProvider::new(
access_key_id.to_string(),
secret_access_key.to_string(),
None,
None,
);
let client = Client::new_with(credentials, HttpClient::new().unwrap());
let s3_client = S3Client::new_with_client(client, region.clone());
let mut or = GetObjectRequest::default();
or.bucket = bucket_name.to_string();
or.key = object_key.to_string();
let _ = env_logger::try_init();
let get_object_output = s3_client.get_object(or);
let r = async_run(get_object_output);
let _err = Error::new(
ErrorKind::Other,
format!(
"something goes wrong while getting object {} in the S3 bucket {}",
object_key, bucket_name
),
);
match r {
Err(err) => {
warn!("{}", err);
Err(_err)
}
Ok(x) => {
let mut s = String::new();
x.body.unwrap().into_blocking_read().read_to_string(&mut s);
if s.is_empty() {
// It looks like we receive sometimes empty content from s3. This is a quick and dirty patch, is there another better way ? Like using s3 Hash ?
return Err(Error::new(
ErrorKind::InvalidData,
"file content is empty - which is not the expected content - what's wrong?",
));
}
Ok(s)
}
}
}
这是我的 Tokio 集成:
use std::future::Future;
use tokio::runtime::Runtime;
pub fn async_run<F: Future>(future: F) -> F::Output {
// TODO improve - is it efficient to create a Runtime at each exec?
let mut runtime = Runtime::new().expect("unable to create a tokio runtime");
runtime.block_on(future)
}
Tokio 似乎没有按预期工作。
我解决了我的问题:我的网速非常低,我使用 AWS CLI 以防 rusoto 出现多次故障。