### Create MinIO Client with Custom Endpoint Source: https://context7.com/remotefs-rs/remotefs-rs-aws-s3/llms.txt Initializes an `AwsS3Fs` client for S3-compatible storage like MinIO. This involves specifying a custom endpoint, enabling path-style access, and providing credentials. The example demonstrates connecting to MinIO and changing directories. ```rust use remotefs::RemoteFs; use remotefs_aws_s3::AwsS3Fs; use std::path::Path; use std::sync::Arc; fn main() -> Result<(), Box> { let runtime = Arc::new(tokio::runtime::Runtime::new()?); // Initialize MinIO client with custom endpoint let mut client = AwsS3Fs::new("my-bucket", &runtime) .endpoint("http://localhost:9000") // MinIO endpoint .new_path_style(true) // Required for MinIO .access_key("minioadmin") .secret_access_key("minioadmin"); client.connect()?; println!("Connected to MinIO"); // Navigate directories client.change_dir(Path::new("/data"))?; println!("Changed to /data directory"); client.disconnect()?; Ok(()) } ``` -------------------------------- ### Install remotefs-aws-s3 Dependencies Source: https://context7.com/remotefs-rs/remotefs-rs-aws-s3/llms.txt Add the necessary dependencies to your `Cargo.toml` file to use the `remotefs-aws-s3` library. This includes `remotefs`, `remotefs-aws-s3`, and `tokio` for asynchronous operations. ```toml [dependencies] remotefs = "0.3" remotefs-aws-s3 = "^0.4" tokio = { version = "1", features = ["rt-multi-thread"] } ``` -------------------------------- ### Get File and Directory Metadata using Rust Source: https://context7.com/remotefs-rs/remotefs-rs-aws-s3/llms.txt Retrieves metadata for files and directories in an AWS S3 bucket, including size and modification time. This function utilizes the remotefs and remotefs-aws-s3 crates. It can check for existence and return detailed file information. ```rust use remotefs::RemoteFs; use remotefs_aws_s3::AwsS3Fs; use std::path::Path; use std::sync::Arc; fn main() -> Result<(), Box> { let runtime = Arc::new(tokio::runtime::Runtime::new()?); let mut client = AwsS3Fs::new("my-bucket", &runtime) .region("us-west-2") .access_key("AKIAIOSFODNN7EXAMPLE") .secret_access_key("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"); client.connect()?; // Check if file exists let exists = client.exists(Path::new("/documents/hello.txt"))?; println!("File exists: {}", exists); // Get file metadata let file = client.stat(Path::new("/documents/hello.txt"))?; println!("Name: {}", file.name()); println!("Path: {}", file.path().display()); println!("Size: {} bytes", file.metadata().size); println!("Is file: {}", file.is_file()); println!("Is directory: {}", file.is_dir()); if let Some(modified) = file.metadata().modified { println!("Last modified: {:?}", modified); } if let Some(ext) = file.extension() { println!("Extension: {}", ext); } client.disconnect()?; Ok(()) } ``` -------------------------------- ### Configure AWS S3 Authentication Methods (Rust) Source: https://context7.com/remotefs-rs/remotefs-rs-aws-s3/llms.txt Illustrates various ways to configure authentication for AwsS3Fs. This includes using default credentials (environment variables or AWS config files), explicit access keys and secret keys, temporary security tokens, and specific AWS profiles. Each method requires the `remotefs` and `remotefs-aws-s3` crates. ```rust use remotefs::RemoteFs; use remotefs_aws_s3::AwsS3Fs; use std::sync::Arc; fn main() -> Result<(), Box> { let runtime = Arc::new(tokio::runtime::Runtime::new()?); // Method 1: Anonymous/Default credentials (reads from environment/AWS config) // Uses AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY environment variables // or ~/.aws/credentials file let mut client = AwsS3Fs::new("my-bucket", &runtime) .region("us-west-2"); // Method 2: Explicit credentials let mut client = AwsS3Fs::new("my-bucket", &runtime) .region("us-west-2") .access_key("AKIAIOSFODNN7EXAMPLE") .secret_access_key("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"); // Method 3: With session/security tokens (for temporary credentials) let mut client = AwsS3Fs::new("my-bucket", &runtime) .region("us-west-2") .access_key("AKIAIOSFODNN7EXAMPLE") .secret_access_key("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY") .session_token("FwoGZXIvYXdzEBY...") .security_token("AQoDYXdzEJr..."); // Method 4: Using a specific AWS profile let mut client = AwsS3Fs::new("my-bucket", &runtime) .region("us-west-2") .profile("production"); client.connect()?; client.disconnect()?; Ok(()) } ``` -------------------------------- ### Perform Directory Operations on S3 Source: https://context7.com/remotefs-rs/remotefs-rs-aws-s3/llms.txt Demonstrates common directory operations using the `AwsS3Fs` client, including creating, changing into, listing contents of, and removing directories. It also shows recursive directory removal. Requires a connected client and appropriate permissions. ```rust use remotefs::RemoteFs; use remotefs::fs::UnixPex; use remotefs_aws_s3::AwsS3Fs; use std::path::Path; use std::sync::Arc; fn main() -> Result<(), Box> { let runtime = Arc::new(tokio::runtime::Runtime::new()?); let mut client = AwsS3Fs::new("my-bucket", &runtime) .region("us-west-2") .access_key("AKIAIOSFODNN7EXAMPLE") .secret_access_key("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"); client.connect()?; // Create a new directory client.create_dir(Path::new("/uploads"), UnixPex::from(0o755))?; println!("Created /uploads directory"); // Change to the new directory client.change_dir(Path::new("/uploads"))?; println!("Current directory: {}", client.pwd()?.display()); // List directory contents let entries = client.list_dir(Path::new("/uploads"))?; for entry in entries { println!(" {} - {} bytes", entry.name(), entry.metadata().size); } // Remove empty directory client.remove_dir(Path::new("/uploads"))?; println!("Removed /uploads directory"); // Remove directory and all contents recursively client.remove_dir_all(Path::new("/old-data"))?; client.disconnect()?; Ok(()) } ``` -------------------------------- ### Create AWS S3 Client with Explicit Credentials Source: https://context7.com/remotefs-rs/remotefs-rs-aws-s3/llms.txt Initializes an `AwsS3Fs` client for AWS S3 using explicit access key and secret access key. It requires a tokio runtime and allows specifying the region and profile. The client is then connected to the S3 bucket, and basic filesystem operations like `pwd()` are demonstrated. ```rust use remotefs::RemoteFs; use remotefs_aws_s3::AwsS3Fs; use std::path::Path; use std::sync::Arc; fn main() -> Result<(), Box> { // Create tokio runtime (required for AWS SDK) let runtime = Arc::new(tokio::runtime::Runtime::new()?); // Initialize S3 client with explicit credentials let mut client = AwsS3Fs::new("my-bucket-name", &runtime) .region("us-west-2") .profile("default") .access_key("AKIAIOSFODNN7EXAMPLE") .secret_access_key("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"); // Connect to S3 client.connect()?; println!("Connected to S3 bucket"); // Get current working directory let pwd = client.pwd()?; println!("Working directory: {}", pwd.display()); // Disconnect when done client.disconnect()?; Ok(()) } ``` -------------------------------- ### Access Underlying S3 Client for Advanced Operations (Rust) Source: https://context7.com/remotefs-rs/remotefs-rs-aws-s3/llms.txt Demonstrates how to obtain a reference to the raw AWS SDK S3Client from an AwsS3Fs instance. This allows for advanced S3 operations not directly exposed by the RemoteFs trait, such as creating buckets. Requires the `remotefs` and `remotefs-aws-s3` crates. ```rust use remotefs::RemoteFs; use remotefs_aws_s3::AwsS3Fs; use std::sync::Arc; fn main() -> Result<(), Box> { let runtime = Arc::new(tokio::runtime::Runtime::new()?); let mut client = AwsS3Fs::new("my-bucket", &runtime) .region("us-west-2") .access_key("AKIAIOSFODNN7EXAMPLE") .secret_access_key("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"); client.connect()?; // Get reference to underlying AWS S3 client for advanced operations if let Some(s3_client) = client.client() { // Use s3_client for operations like creating buckets, // setting bucket policies, etc. let fut = s3_client .create_bucket() .bucket("new-bucket-name") .send(); runtime.block_on(fut)?; println!("Created new bucket via raw S3 client"); } client.disconnect()?; Ok(()) } ``` -------------------------------- ### Upload Files to S3 using Rust Source: https://context7.com/remotefs-rs/remotefs-rs-aws-s3/llms.txt Uploads files to an AWS S3 bucket using multipart upload for efficiency with large files. Requires the remotefs and remotefs-aws-s3 crates. It takes a file path, metadata, and a reader as input, returning the number of bytes written. ```rust use remotefs::RemoteFs; use remotefs::fs::Metadata; use remotefs_aws_s3::AwsS3Fs; use std::io::Cursor; use std::path::Path; use std::sync::Arc; fn main() -> Result<(), Box> { let runtime = Arc::new(tokio::runtime::Runtime::new()?); let mut client = AwsS3Fs::new("my-bucket", &runtime) .region("us-west-2") .access_key("AKIAIOSFODNN7EXAMPLE") .secret_access_key("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"); client.connect()?; // Prepare file data let file_content = b"Hello, S3! This is my file content."; let reader = Cursor::new(file_content); // Create metadata let mut metadata = Metadata::default(); metadata.size = file_content.len() as u64; // Upload file to S3 let bytes_written = client.create_file( Path::new("/documents/hello.txt"), &metadata, Box::new(reader) )?; println!("Uploaded {} bytes to /documents/hello.txt", bytes_written); // Upload a larger file from disk let file = std::fs::File::open("/local/path/to/large-file.zip")?; let file_size = file.metadata()?.len(); let mut metadata = Metadata::default(); metadata.size = file_size; let bytes_written = client.create_file( Path::new("/backups/large-file.zip"), &metadata, Box::new(file) )?; println!("Uploaded {} bytes", bytes_written); client.disconnect()?; Ok(()) } ``` -------------------------------- ### Add remotefs-aws-s3 to Project Dependencies (TOML) Source: https://github.com/remotefs-rs/remotefs-rs-aws-s3/blob/main/README.md This snippet shows how to add the remotefs-aws-s3 crate and its core dependency to your Rust project's Cargo.toml file. It specifies version constraints for both crates, ensuring compatibility. ```toml remotefs = "0.3" remotefs-aws-s3 = "^0.4" ``` -------------------------------- ### Download Files from S3 using Rust Source: https://context7.com/remotefs-rs/remotefs-rs-aws-s3/llms.txt Downloads files from an AWS S3 bucket to a local writer or memory buffer. This function requires the remotefs and remotefs-aws-s3 crates. It takes a file path and a mutable writer as input, returning the number of bytes read. ```rust use remotefs::RemoteFs; use remotefs_aws_s3::AwsS3Fs; use std::fs::File; use std::io::Write; use std::path::Path; use std::sync::Arc; fn main() -> Result<(), Box> { let runtime = Arc::new(tokio::runtime::Runtime::new()?); let mut client = AwsS3Fs::new("my-bucket", &runtime) .region("us-west-2") .access_key("AKIAIOSFODNN7EXAMPLE") .secret_access_key("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"); client.connect()?; // Download to memory buffer let mut buffer = Vec::new(); let bytes_read = client.open_file( Path::new("/documents/hello.txt"), Box::new(&mut buffer) )?; println!("Downloaded {} bytes", bytes_read); println!("Content: {}", String::from_utf8_lossy(&buffer)); // Download to file let local_file = File::create("/local/path/downloaded-file.txt")?; let bytes_read = client.open_file( Path::new("/documents/hello.txt"), Box::new(local_file) )?; println!("Saved {} bytes to local file", bytes_read); client.disconnect()?; Ok(()) } ``` -------------------------------- ### Delete Files from S3 using Rust Source: https://context7.com/remotefs-rs/remotefs-rs-aws-s3/llms.txt Removes individual files from an AWS S3 bucket. This function uses the remotefs and remotefs-aws-s3 crates. It first checks if the file exists before attempting deletion, ensuring idempotency. ```rust use remotefs::RemoteFs; use remotefs_aws_s3::AwsS3Fs; use std::path::Path; use std::sync::Arc; fn main() -> Result<(), Box> { let runtime = Arc::new(tokio::runtime::Runtime::new()?); let mut client = AwsS3Fs::new("my-bucket", &runtime) .region("us-west-2") .access_key("AKIAIOSFODNN7EXAMPLE") .secret_access_key("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"); client.connect()?; // Check if file exists before deletion if client.exists(Path::new("/temp/old-file.txt"))? { client.remove_file(Path::new("/temp/old-file.txt"))?; println!("File deleted successfully"); } client.disconnect()?; Ok(()) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.