Skip to content

Allow streaming uploads to S3. #1813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/bin/render-readmes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,12 @@ fn main() {
return;
}
let readme = readme.unwrap();
let content_length = readme.len() as u64;
let content = std::io::Cursor::new(readme);
let readme_path = format!("readmes/{0}/{0}-{1}.html", krate_name, version.num);
config
.uploader
.upload(&client, &readme_path, readme.into_bytes(), "text/html")
.upload(&client, &readme_path, content, content_length, "text/html")
.unwrap_or_else(|_| {
panic!(
"[{}-{}] Couldn't upload file to S3",
Expand Down
7 changes: 4 additions & 3 deletions src/s3/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ impl Bucket {
}
}

pub fn put(
pub fn put<R: std::io::Read + Send + 'static>(
&self,
client: &reqwest::Client,
path: &str,
content: Vec<u8>,
content: R,
content_length: u64,
content_type: &str,
) -> reqwest::Result<reqwest::Response> {
let path = if path.starts_with('/') {
Expand All @@ -59,7 +60,7 @@ impl Bucket {
.header(header::AUTHORIZATION, auth)
.header(header::CONTENT_TYPE, content_type)
.header(header::DATE, date)
.body(content)
.body(reqwest::Body::sized(content, content_length))
.send()?
.error_for_status()
}
Expand Down
46 changes: 27 additions & 19 deletions src/uploaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::util::{human, internal, CargoResult, ChainError, Maximums};

use std::env;
use std::fs::{self, File};
use std::io::{Read, Write};
use std::io::{Cursor, Read};
use std::sync::Arc;

use crate::middleware::app::RequestApp;
Expand Down Expand Up @@ -83,30 +83,29 @@ impl Uploader {

/// Uploads a file using the configured uploader (either `S3`, `Local`).
///
/// It returns a a tuple containing the path of the uploaded file
/// and its checksum.
pub fn upload(
/// It returns the path of the uploaded file.
Copy link
Contributor Author

@smarnach smarnach Aug 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, the return value is never used, so we could completely get rid of it, but I conservatively kept it for now.

pub fn upload<R: std::io::Read + Send + 'static>(
&self,
client: &reqwest::Client,
path: &str,
body: Vec<u8>,
mut content: R,
content_length: u64,
content_type: &str,
) -> CargoResult<(Option<String>, Vec<u8>)> {
let hash = hash(&body);
) -> CargoResult<Option<String>> {
match *self {
Uploader::S3 { ref bucket, .. } => {
bucket
.put(client, path, body, content_type)
.put(client, path, content, content_length, content_type)
.map_err(|e| internal(&format_args!("failed to upload to S3: {}", e)))?;
Ok((Some(String::from(path)), hash))
Ok(Some(String::from(path)))
}
Uploader::Local => {
let filename = env::current_dir().unwrap().join("local_uploads").join(path);
let dir = filename.parent().unwrap();
fs::create_dir_all(dir)?;
let mut file = File::create(&filename)?;
file.write_all(&body)?;
Ok((filename.to_str().map(String::from), hash))
std::io::copy(&mut content, &mut file)?;
Ok(filename.to_str().map(String::from))
}
}
}
Expand All @@ -120,13 +119,20 @@ impl Uploader {
vers: &semver::Version,
) -> CargoResult<Vec<u8>> {
let app = Arc::clone(req.app());
let (_, checksum) = {
let path = Uploader::crate_path(&krate.name, &vers.to_string());
let mut body = Vec::new();
LimitErrorReader::new(req.body(), maximums.max_upload_size).read_to_end(&mut body)?;
verify_tarball(krate, vers, &body, maximums.max_unpack_size)?;
self.upload(app.http_client(), &path, body, "application/x-tar")?
};
let path = Uploader::crate_path(&krate.name, &vers.to_string());
let mut body = Vec::new();
LimitErrorReader::new(req.body(), maximums.max_upload_size).read_to_end(&mut body)?;
verify_tarball(krate, vers, &body, maximums.max_unpack_size)?;
let checksum = hash(&body);
let content_length = body.len() as u64;
let content = Cursor::new(body);
self.upload(
app.http_client(),
&path,
content,
content_length,
"application/x-tar",
)?;
Ok(checksum)
}

Expand All @@ -138,7 +144,9 @@ impl Uploader {
readme: String,
) -> CargoResult<()> {
let path = Uploader::readme_path(crate_name, vers);
self.upload(http_client, &path, readme.into_bytes(), "text/html")?;
let content_length = readme.len() as u64;
let content = Cursor::new(readme);
self.upload(http_client, &path, content, content_length, "text/html")?;
Ok(())
}
}
Expand Down