### Install Aliyun OSS Rust SDK Source: https://context7.com/dounine/aliyun-oss-rust-sdk/llms.txt Instructions for adding the aliyun-oss-rust-sdk dependency to a Rust project's Cargo.toml file. It shows how to include the default asynchronous version, the synchronous (blocking) version using a feature flag, and how to enable debug logging. ```toml [dependencies] # Asynchronous (default) aliyun-oss-rust-sdk = "0.2.2" # Synchronous (blocking) aliyun-oss-rust-sdk = { version = "0.2.2", features = ["blocking"] } # With debug logging aliyun-oss-rust-sdk = { version = "0.2.2", features = ["blocking", "debug-print"] } ``` -------------------------------- ### Download Object from OSS in Rust Source: https://context7.com/dounine/aliyun-oss-rust-sdk/llms.txt Shows how to download an object from an Alibaba Cloud OSS bucket using the `get_object` method. The function retrieves the object's content as bytes, which are then converted to a UTF-8 string. Error handling for the download operation is included. This example requires the `tokio` runtime for asynchronous execution. ```rust use aliyun_oss_rust_sdk::oss::OSS; use aliyun_oss_rust_sdk::request::RequestBuilder; #[tokio::main] async fn main() { let oss = OSS::from_env(); let builder = RequestBuilder::new(); // Download file from OSS match oss.get_object("/path/to/file.txt", builder).await { Ok(bytes) => { let content = String::from_utf8_lossy(&bytes); println!("File content: {}", content); // Output: File content: Hello, World! } Err(e) => eprintln!("Download failed: {}", e), } } ``` -------------------------------- ### Upload Local File to OSS in Rust Source: https://context7.com/dounine/aliyun-oss-rust-sdk/llms.txt Provides an example of uploading a local file to an Alibaba Cloud OSS bucket using the `put_object_from_file` method. The code demonstrates how to configure request options, such as an expiration time, using `RequestBuilder`. It includes error handling for the upload process and assumes the use of the `tokio` runtime. ```rust use aliyun_oss_rust_sdk::oss::OSS; use aliyun_oss_rust_sdk::request::RequestBuilder; #[tokio::main] async fn main() { let oss = OSS::from_env(); let builder = RequestBuilder::new() .with_expire(60); // Request expires in 60 seconds let local_file = "./data/document.txt"; let oss_path = "/uploads/document.txt"; match oss.put_object_from_file(oss_path, local_file, builder).await { Ok(()) => println!("File uploaded successfully to {}", oss_path), Err(e) => eprintln!("Upload failed: {}", e), } } ``` -------------------------------- ### Get Object Metadata using Aliyun OSS Rust SDK Source: https://context7.com/dounine/aliyun-oss-rust-sdk/llms.txt Retrieves metadata for an object in Aliyun OSS without downloading the content. It requires the OSS client and a request builder. The output includes details like Content-Type, Content-Length, ETag, Last-Modified, and CRC64. ```rust use aliyun_oss_rust_sdk::oss::OSS; use aliyun_oss_rust_sdk::request::RequestBuilder; #[tokio::main] async fn main() { let oss = OSS::from_env(); let builder = RequestBuilder::new(); match oss.get_object_metadata("/uploads/document.pdf", builder).await { Ok(metadata) => { println!("Content-Type: {:?}", metadata.content_type()); println!("Content-Length: {:?}", metadata.content_length()); println!("ETag: {:?}", metadata.etag()); println!("Last-Modified: {:?}", metadata.last_modified()); println!("CRC64: {:?}", metadata.crc64()); // Output example: // Content-Type: Some("application/pdf") // Content-Length: Some("1048576") // ETag: Some("5d41402abc4b2a76b9719d911017c592") // Last-Modified: Some(2024-01-15T10:30:00Z) } Err(e) => eprintln!("Failed to get metadata: {}", e), } } ``` -------------------------------- ### Handle OSS Errors with OssError Enum (Rust) Source: https://context7.com/dounine/aliyun-oss-rust-sdk/llms.txt Illustrates how to handle errors from the Aliyun OSS Rust SDK using the unified `OssError` enum. This example shows how to match on different error variants like `RequestError`, `IoError`, `JsonError`, and generic `Err` messages to provide specific error feedback. ```rust use aliyun_oss_rust_sdk::oss::OSS; use aliyun_oss_rust_sdk::request::RequestBuilder; use aliyun_oss_rust_sdk::error::OssError; #[tokio::main] async fn main() { let oss = OSS::from_env(); let builder = RequestBuilder::new(); match oss.get_object("/nonexistent/file.txt", builder).await { Ok(bytes) => println!("Downloaded {} bytes", bytes.len()), Err(OssError::RequestError(e)) => { eprintln!("HTTP request failed: {}", e); } Err(OssError::IoError(e)) => { eprintln!("IO operation failed: {}", e); } Err(OssError::JsonError(e)) => { eprintln!("JSON parsing failed: {}", e); } Err(OssError::Err(msg)) => { eprintln!("OSS error: {}", msg); // Example: "get object status: 404 Not Found error: NoSuchKey" } Err(e) => eprintln!("Other error: {}", e), } } ``` -------------------------------- ### Delete Object from OSS in Rust Source: https://context7.com/dounine/aliyun-oss-rust-sdk/llms.txt Demonstrates how to delete an object from an Alibaba Cloud OSS bucket using the `delete_object` method. The example includes setting request options like expiration time via `RequestBuilder` and incorporates error handling for the deletion process. This asynchronous operation requires the `tokio` runtime. ```rust use aliyun_oss_rust_sdk::oss::OSS; use aliyun_oss_rust_sdk::request::RequestBuilder; #[tokio::main] async fn main() { let oss = OSS::from_env(); let builder = RequestBuilder::new() .with_expire(60); match oss.delete_object("/uploads/old-file.txt", builder).await { Ok(()) => println!("File deleted successfully"), Err(e) => eprintln!("Delete failed: {}", e), } } ``` -------------------------------- ### Upload Memory Buffer to OSS in Rust Source: https://context7.com/dounine/aliyun-oss-rust-sdk/llms.txt Illustrates how to upload data directly from a memory buffer (byte slice) to an Alibaba Cloud OSS bucket using the `pub_object_from_buffer` method. This method is useful for uploading data generated in memory without needing to write it to a file first. The example shows setting content type and expiration using `RequestBuilder` and includes asynchronous execution with `tokio`. ```rust use aliyun_oss_rust_sdk::oss::OSS; use aliyun_oss_rust_sdk::request::RequestBuilder; #[tokio::main] async fn main() { let oss = OSS::from_env(); let builder = RequestBuilder::new() .with_expire(60) .with_content_type("application/json"); // Create data in memory let json_data = r#"{"name": "example", "value": 42}"#; let buffer = json_data.as_bytes(); match oss.pub_object_from_buffer("/data/config.json", buffer, builder).await { Ok(()) => println!("Buffer uploaded successfully"), Err(e) => eprintln!("Upload failed: {}", e), } } ``` -------------------------------- ### Initialize OSS Client in Rust Source: https://context7.com/dounine/aliyun-oss-rust-sdk/llms.txt Demonstrates how to create an instance of the OSS client. It covers initialization using environment variables (OSS_KEY_ID, OSS_KEY_SECRET, OSS_ENDPOINT, OSS_BUCKET) and direct initialization with explicit credentials and bucket information. It also shows how to enable debug logging if the 'debug-print' feature is enabled. ```rust use aliyun_oss_rust_sdk::oss::OSS; // Initialize from environment variables // Required: OSS_KEY_ID, OSS_KEY_SECRET, OSS_ENDPOINT, OSS_BUCKET let oss = OSS::from_env(); // Or initialize with explicit credentials let oss = OSS::new( "your_access_key_id", "your_access_key_secret", "oss-cn-shanghai.aliyuncs.com", "your_bucket_name", ); // Enable debug logging (requires "debug-print" feature) oss.open_debug(); ``` -------------------------------- ### Configure RequestBuilder Options (Rust) Source: https://context7.com/dounine/aliyun-oss-rust-sdk/llms.txt Demonstrates how to configure request parameters using the `RequestBuilder` pattern in the Aliyun OSS Rust SDK. This allows customization of various aspects of API calls, including expiration times, CDN usage, content types, download limits, IP restrictions, and custom headers. ```rust use aliyun_oss_rust_sdk::request::RequestBuilder; fn main() { let builder = RequestBuilder::new() // Set URL expiration time (in seconds) .with_expire(3600) // Use CDN domain instead of direct OSS endpoint .with_cdn("https://cdn.example.com") // Set Content-Type for uploads .with_content_type("application/octet-stream") // Use HTTP instead of HTTPS .with_http() // Limit download speed (minimum 30 KB/s) .oss_download_speed_limit(50) // 50 KB/s // Restrict download to specific IP with subnet mask .oss_download_allow_ip("10.0.0.1", 24) // Allow forwarded IP checking .oss_ac_forward_allow() // Set response Content-Disposition for downloads .response_content_disposition("document.pdf") // Set response Content-Encoding .response_content_encoding(&"gzip".to_string()) // Add custom OSS headers .oss_header_put("x-oss-meta-author", "John Doe") // Add custom parameters .parameters_put("response-cache-control", "no-cache"); println!("Builder configured: {:?}", builder); } ``` -------------------------------- ### Generate Signed Download URL with Aliyun OSS Rust SDK Source: https://context7.com/dounine/aliyun-oss-rust-sdk/llms.txt Creates a pre-signed URL for downloading an object from Aliyun OSS. This URL can include optional configurations like CDN usage, speed limits, and IP restrictions. The URL is valid for a specified expiration time. ```rust use aliyun_oss_rust_sdk::oss::OSS; use aliyun_oss_rust_sdk::request::RequestBuilder; use aliyun_oss_rust_sdk::url::UrlApi; fn main() { let oss = OSS::new( "your_key_id", "your_key_secret", "oss-cn-shanghai.aliyuncs.com", "your_bucket", ); let builder = RequestBuilder::new() .with_expire(3600) // URL valid for 1 hour .oss_download_speed_limit(100) // Limit speed to 100 KB/s .oss_download_allow_ip("192.168.1.1", 32); // Restrict to specific IP let download_url = oss.sign_download_url("/files/large-video.mp4", &builder); println!("Signed download URL: {}", download_url); // Output: https://your_bucket.oss-cn-shanghai.aliyuncs.com/files/large-video.mp4?Expires=...&OSSAccessKeyId=...&Signature=... // With CDN domain let cdn_builder = RequestBuilder::new() .with_cdn("https://cdn.example.com") .with_expire(600); let cdn_url = oss.sign_download_url("/files/image.png", &cdn_builder); println!("CDN download URL: {}", cdn_url); } ``` -------------------------------- ### Generate Signed Upload URL with Aliyun OSS Rust SDK Source: https://context7.com/dounine/aliyun-oss-rust-sdk/llms.txt Generates a pre-signed URL that allows clients to upload objects directly to Aliyun OSS. The URL includes an expiration time and requires the `Content-Type` header to match during the upload process. This is useful for client-side uploads. ```rust use aliyun_oss_rust_sdk::oss::OSS; use aliyun_oss_rust_sdk::request::RequestBuilder; use aliyun_oss_rust_sdk::url::UrlApi; fn main() { let oss = OSS::from_env(); let builder = RequestBuilder::new() .with_content_type("image/png") // Must match the Content-Type header when uploading .with_expire(300); // URL valid for 5 minutes let upload_url = oss.sign_upload_url("/uploads/user-avatar.png", &builder); println!("Signed upload URL: {}", upload_url); // Output: https://bucket.oss-cn-shanghai.aliyuncs.com/uploads/user-avatar.png?Expires=...&OSSAccessKeyId=...&Signature=... // Client can now PUT to this URL with matching Content-Type header } ``` -------------------------------- ### Generate Upload Policy for Browser Uploads (Rust) Source: https://context7.com/dounine/aliyun-oss-rust-sdk/llms.txt Generates a policy for browser-based direct uploads to Aliyun OSS. This allows fine-grained control over file types, sizes, and destinations. It requires the `aliyun-oss-rust-sdk` crate and uses `PolicyBuilder` to define upload constraints. ```rust use aliyun_oss_rust_sdk::oss::OSS; use aliyun_oss_rust_sdk::entity::PolicyBuilder; fn main() { let oss = OSS::from_env(); let policy_builder = PolicyBuilder::new() .with_expire(3600) // Policy valid for 1 hour .with_upload_dir("uploads/images/") // Restrict to specific directory .with_content_type("image/jpeg") // Only allow JPEG images .with_max_upload_size(10 * 1024 * 1024); // Max 10 MB match oss.get_upload_object_policy(policy_builder) { Ok(policy) => { println!("Access ID: {}", policy.access_id); println!("Host: {}", policy.host); println!("Policy: {}", policy.policy); println!("Signature: {}", policy.signature); println!("Success Status: {}", policy.success_action_status); // Output: // Access ID: LTAI5txxxxxx // Host: https://bucket.oss-cn-shanghai.aliyuncs.com // Policy: eyJleHBpcmF0aW9uIj... // Signature: 8dKz3hxxxx= // Success Status: 200 // Use these values in HTML form for direct browser upload: //
// // // // // // // //
} Err(e) => eprintln!("Failed to generate policy: {}", e), } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.