Initialize the Rust SDK
Configure the endpoint and access credentials for the Simple Log Service (SLS) Rust SDK, and then create an SLS client. You use the client to write and query logs in subsequent steps.
Prerequisites
You have completed "Install the Rust SDK".
You have prepared the AccessKey of a RAM user and granted the required SLS access permissions.
Configure environment variables
Set the following environment variables in the environment where the program runs. The following example is for Linux or macOS:
export ALIBABA_CLOUD_ACCESS_KEY_ID="<your-access-key-id>"
export ALIBABA_CLOUD_ACCESS_KEY_SECRET="<your-access-key-secret>"Replace the AccessKey placeholders with the actual values. Do not hard-code your credentials or commit them to a code repository.
Initialize the client
Save the following code as src/main.rs. This example uses the China (Hangzhou) public endpoint. Replace the endpoint with the endpoint of the region where your Project resides. For endpoints of other regions, see Service endpoints.
use aliyun_log_rust_sdk::{Client, Config, FromConfig};
use std::env;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = Config::builder()
.endpoint("https://cn-hangzhou.log.aliyuncs.com")
.access_key(
env::var("ALIBABA_CLOUD_ACCESS_KEY_ID")?,
env::var("ALIBABA_CLOUD_ACCESS_KEY_SECRET")?,
)
.build()?;
let _client = Client::from_config(config)?;
println!("Client initialization complete");
Ok(())
}Run cargo run. If the program prints "Client initialization complete", the client is created. Initialization does not send a request. Actual access permissions and network connectivity are verified when you call an API.
What to do next
See "Rust SDK Getting Started" to use the client to write and query logs.