Posted at

Rust S3から大きいファイルをダウンロード

実装メモ

use std::fs::File;

use rusoto_s3::{S3Client, S3, GetObjectRequest, HeadObjectRequest};
use std::io::Write;
use futures::stream::Stream;
use futures::future::Future;

fn main() -> std::io::Result<()> {

// std::env::set_var("AWS_ACCESS_KEY_ID", "YOUR AWS ACCESS KEY ID HERE");
// std::env::set_var("AWS_SECRET_ACCESS_KEY", "YOUR AWS SECRET ACCESS KEY HERE");

let bucket_name = String::from("your-bucket-name");
let s3_object_key = String::from("test.json");
let region = "us-east-1".parse().unwrap();
let client = S3Client::new(region);

let mut request = HeadObjectRequest::default();
request.bucket = bucket_name.clone();
request.key = s3_object_key.clone();
let result = client.head_object(request).sync().unwrap();

let from = 0;
// you can write the following request inside foreach by specified buffer capacity.
let to = result.content_length.unwrap();
let mut request = GetObjectRequest::default();
request.bucket = bucket_name.clone();
request.key = s3_object_key.clone();
let range = format!("bytes={}-{}", from, to);
request.range = Some(range);
let result = client.get_object(request).sync().unwrap();
let body = result.body.unwrap().concat2().wait().unwrap();

let mut file = File::create("test.json")?;
file.write_all(&body)?;

Ok(())
}