Skip to content

Instantly share code, notes, and snippets.

@chindit
Created November 24, 2022 19:16
Show Gist options
  • Save chindit/7df0a3c446d868f1465702661ed3b224 to your computer and use it in GitHub Desktop.
Save chindit/7df0a3c446d868f1465702661ed3b224 to your computer and use it in GitHub Desktop.
Compress & upload a file
use flate2::write::{GzEncoder};
use flate2::Compression;
use std::fs::File;
use reqwest::blocking::Client;
use std::error;
use std::fs;
use reqwest::header::CONTENT_LENGTH;
use reqwest::header::AUTHORIZATION;
use reqwest::blocking::multipart::Form;
fn main() -> Result<(), Box<dyn error::Error>> {
println!("Starting compression");
let tar_gz = File::create("archive.tar.gz")?;
let enc = GzEncoder::new(tar_gz, Compression::default());
let mut tar = tar::Builder::new(enc);
tar.append_dir_all("", "/local/dir")?;
println!("Compression success");
println!("Starting upload");
let client = Client::new();
let filesize = fs::metadata("archive.tar.gz")?.len();
let form = Form::new()
// Adding just a simple text field...
.text("username", "My-USER")
// And a file...
.file("file[]", "archive.tar.gz")?;
let res = client
.post("https://upload.1fichier.com/upload.cgi?id=AUB3EB9LVH")
.multipart(form)
.header(reqwest::header::CONTENT_LENGTH, filesize)
.header(reqwest::header::AUTHORIZATION, "Bearer API_KEY")
.send()?;
println!("{:?}", res);
println!("Upload success");
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment