first
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
target/
|
||||
1487
Cargo.lock
generated
Normal file
1487
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
12
Cargo.toml
Normal file
12
Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "rust_s3"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rusoto_core = "0.47.0"
|
||||
rusoto_s3 = "0.47.0"
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
|
||||
15
README.md
Normal file
15
README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
A CLI for uploading files to S3. The code was written in ChatGPT by the GPT4 model. The only change I've made so far is to remove an unused import.
|
||||
|
||||
# Usage:
|
||||
|
||||
```bash
|
||||
> s3c <filename> <bucket_name> <metadata> [key]
|
||||
```
|
||||
|
||||
# Installation
|
||||
|
||||
## Download from Github Releases
|
||||
TODO
|
||||
|
||||
## Homebrew
|
||||
TODO
|
||||
55
src/main.rs
Normal file
55
src/main.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
use rusoto_core::Region;
|
||||
use rusoto_s3::{S3Client, S3, PutObjectRequest};
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
// Parse command line arguments
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() < 4 {
|
||||
println!("Usage: upload_to_s3 <filename> <bucket_name> <metadata> [key]");
|
||||
return Ok(());
|
||||
}
|
||||
let filename = &args[1];
|
||||
let bucket_name = &args[2];
|
||||
let metadata = &args[3];
|
||||
let key = if args.len() >= 5 {
|
||||
&args[4]
|
||||
} else {
|
||||
// If 'key' is not provided, use the file name without path
|
||||
Path::new(filename).file_name().unwrap().to_str().unwrap()
|
||||
};
|
||||
|
||||
// Read file and metadata
|
||||
let mut file = File::open(filename)?;
|
||||
let mut contents = Vec::new();
|
||||
file.read_to_end(&mut contents)?;
|
||||
|
||||
let metadata_vec: Vec<&str> = metadata.split(",").collect::<Vec<&str>>();
|
||||
let mut object_metadata = HashMap::new();
|
||||
for i in (0..metadata_vec.len()).step_by(2) {
|
||||
object_metadata.insert(metadata_vec[i].to_string(), metadata_vec[i + 1].to_string());
|
||||
}
|
||||
|
||||
// Upload file to S3
|
||||
let client = S3Client::new(Region::default());
|
||||
let request = PutObjectRequest {
|
||||
bucket: bucket_name.to_string(),
|
||||
key: key.to_string(),
|
||||
body: Some(contents.into()),
|
||||
metadata: Some(object_metadata),
|
||||
..Default::default()
|
||||
};
|
||||
match client.put_object(request).await {
|
||||
Ok(_) => println!("File uploaded successfully"),
|
||||
Err(e) => println!("Error uploading file: {}", e),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user