alexcrichton / curl-rust

Rust bindings to libcurl
MIT License
1.02k stars 233 forks source link

-T Transfer local FILE to destination #364

Closed hatembentayeb closed 3 years ago

hatembentayeb commented 3 years ago

Hello i have to upload a file to an azure storage container, i'm wondering if the file upload -T was implemented or not, i'm just confused about this, i have this example working as well with curl and i want to converted to a rust code :

curl -X PUT -T ./local_file -H "x-ms-date: $(date -u)" -H "x-ms-blob-type: BlockBlob" "https://{accountname}.blob.core.windows.net/container/local_file?{sastoken}" is there an example of local file upload ! thanks

hatembentayeb commented 3 years ago

I found a solution :dancers: ! and here it is , i hope it can help someone !

    pub fn upload(&self) {
        //-----------------------------------

        let data = fs::read_to_string("./localfile").unwrap();
        //dbg!(data);
        //println!("{}",data);
        let url = format!("https://{storage_account}.blob.core.windows.net/{container}/{localfile_name}?{sas_token}");
        //dbg!(url);
        let mut data = data.as_bytes();

        let mut easy = Easy::new();
        let mut list = List::new();
        list.append("x-ms-blob-type: BlockBlob")
        .unwrap();

        easy.url(&url).unwrap();
        easy.http_headers(list).unwrap();
        easy.put(true).unwrap();
        easy.post_field_size(data.len() as u64).unwrap();

        let mut transfer = easy.transfer();
        transfer
            .read_function(|buf| Ok(data.read(buf).unwrap_or(0)))
            .unwrap();
        transfer.perform().unwrap();

        //-----------------------------
    }