我正在尝试使用Rusoto从存储桶中下载文件,并且正在获取文件内容:
fn get_object(client: &TestClient, bucket: &str, filename: &str) {
let get_req = GetObjectRequest {
bucket: bucket.to_owned(),
key: filename.to_owned(),
..Default::default()
};
let result = client.get_object(&get_req).sync().expect("Couldn't GET object");
let stream = result.body.unwrap();
let body = stream.concat2().wait().unwrap();
assert!(body.len() > 0);
}
如何将这个GetObjectOutput(result)
对象保存到文件中?
答案 0 :(得分:6)
Rusoto现在使用标准库期货,并且不再提供sync
方法,因此先前的答案不再有效。
use futures::stream::TryStreamExt;
use rusoto_core::Region;
use rusoto_s3::{GetObjectRequest, S3Client, S3};
type Error = Box<dyn std::error::Error>;
type Result<T, E = Error> = std::result::Result<T, E>;
const BUCKET_NAME: &str = "my very own bucket name";
#[tokio::main]
async fn main() -> Result<()> {
let client = S3Client::new(Region::UsEast2);
let mut object = client
.get_object(GetObjectRequest {
bucket: BUCKET_NAME.into(),
..Default::default()
})
.await?;
let body = object.body.take().expect("The object has no body");
let body = body.map_ok(|b| b.to_vec()).try_concat().await?;
println!("body length: {}", body.len());
Ok(())
}
需要指定 AWS_ACCESS_KEY_ID
和AWS_SECRET_ACCESS_KEY
。我选择在代码之外设置环境变量。
use rusoto_core::Region;
use rusoto_s3::{GetObjectRequest, S3Client, S3};
use tokio::{fs::File, io};
type Error = Box<dyn std::error::Error>;
type Result<T, E = Error> = std::result::Result<T, E>;
const BUCKET_NAME: &str = "my very own bucket name";
#[tokio::main]
async fn main() -> Result<()> {
let client = S3Client::new(Region::UsEast2);
let mut object = client
.get_object(GetObjectRequest {
bucket: BUCKET_NAME.into(),
..Default::default()
})
.await?;
let body = object.body.take().expect("The object has no body");
let mut body = body.into_async_read();
let mut file = File::create("/tmp/a-place-to-write").await?;
io::copy(&mut body, &mut file).await?;
Ok(())
}
尽管ByteStream
具有诱人的into_blocking_read
方法,但我不建议您使用它。如果尝试在异步上下文中使用它,则会出现恐慌,因为它会启动嵌套的Tokio执行程序。如果您在异步上下文之外使用它,则它将truncate the data,除非您非常注意使异步运行时在周围,但不要在其内部。
另请参阅:
[dependencies]
rusoto_s3 = "0.43.0"
rusoto_core = "0.43.0"
tokio = { version = "0.2.21", features = ["macros"] }
futures = "0.3.5"
答案 1 :(得分:1)
您快到了。您的代码会将对象放入 if error != nil {
ProgressHUD.showError(error!.localizedDescription)
return
}
if let photoUrl = metadata?.downloadURL()?.absoluteString {
onSuccess(photoUrl)
}
}
}
static func sendDataToDatabase(photoUrl: String, videoUrl: String? = nil, ratio: CGFloat, caption: String, onSuccess: @escaping () -> Void) {
let newPostId = Api.Post.REF_POSTS.childByAutoId().key
let newPostReference = Api.Post.REF_POSTS.child(newPostId)
guard let currentUser = Api.User.CURRENT_USER else {
return
}
let words = caption.components(separatedBy: CharacterSet.whitespacesAndNewlines)
for var word in words {
if word.hasPrefix("#") {
word = word.trimmingCharacters(in: CharacterSet.punctuationCharacters)
word = word.trimmingCharacters(in: CharacterSet.symbols)
let newHashReference = Api.HashTag.REF_HASHTAG.child(word.lowercased())
newHashReference.setValue([newPostId: true])
// let hashTagsRef = DataService.dataService.BASE_REF.child("hashTags").child(postKey)
// let data = ["to": "", "by": "\(DataService.dataService.currentUserId!)", "hashTag": word.lowercased(), "comment": self.captionTextView.text] as [String : Any]
// hashTagsRef.setValue(data)
}
}
let currentUserId = currentUser.id
var dict = ["id": currentUserId! ,"photoUrl": photoUrl, "caption": caption, "likeCount": 0, "ratio": ratio] as [String : Any]
if let videoUrl = videoUrl {
dict["videoUrl"] = videoUrl
}
newPostReference.setValue(dict, withCompletionBlock: {
(error, ref) in
if error != nil {
ProgressHUD.showError(error!.localizedDescription)
return
}
Api.Feed.REF_FEED.child(Api.User.CURRENT_USER!.id!).child(newPostId).setValue(true)
let myPostRef = Api.MyPosts.REF_MYPOSTS.child(currentUserId!).child(newPostId)
myPostRef.setValue(true, withCompletionBlock: { (error, ref) in
if error != nil {
ProgressHUD.showError(error!.localizedDescription)
return
}
})
ProgressHUD.showSuccess("Success")
onSuccess()
})
}
}
中的body
中。
要将Vec<u8>
的内容写入文件:
body