Crates.io | aws_signer |
lib.rs | aws_signer |
version | 0.0.5 |
source | src |
created_at | 2024-08-25 03:30:22.398443 |
updated_at | 2024-08-27 15:56:19.197225 |
description | A Rust library for AWS Signature Version 4 signing |
homepage | https://github.com/ayonsaha2011/aws_signer |
repository | https://github.com/ayonsaha2011/aws_signer |
max_upload_size | |
id | 1350746 |
size | 45,225 |
aws_signer
is a Rust library that implements AWS Signature Version 4 signing for making authenticated requests to AWS services, including generating presigned URLs. This library is compatible with Cloudflare Workers and Cloudflare R2, allowing you to sign HTTP requests in a way that AWS and Cloudflare can verify, ensuring secure communication.
reqwest
for HTTP requestsTo use the aws_signer
library in your Rust project, add it as a dependency in your Cargo.toml
:
[dependencies]
aws_signer = "0.1"
To create a new AWS client with your credentials, use the AwsClient::new
function:
use aws_signer::{AwsClient, AwsOptions, AwsRequestInit};
fn main() {
let client = AwsClient::new(
"your_access_key_id".to_string(),
"your_secret_access_key".to_string(),
None, // Optional session token if required
None, // Service will be guessed if None
None, // Region will be guessed if None
None,
Some(3), // Retries
Some(100), // Initial retry delay in ms
);
// Use the client to sign requests or fetch data
}
You can sign an HTTP request using the sign
method of AwsClient
. This example shows how to sign a request using the reqwest
crate:
use aws_signer::{AwsClient, AwsRequestInit};
use reqwest::{Request, Method, Url};
#[tokio::main]
async fn main() {
let client = AwsClient::new(
"your_access_key_id".to_string(),
"your_secret_access_key".to_string(),
None, // Optional session token if required
None, // Service will be guessed if None
None, // Region will be guessed if None
None,
Some(3), // Retries
Some(100), // Initial retry delay in ms
);
let request = Request::new(
Method::PUT,
Url::parse("https://your-bucket.your-account.r2.cloudflarestorage.com/test-file").unwrap(),
);
match client.fetch(request, None).await {
Ok(response) => {
println!("Response: {:?}", response.text().await.unwrap());
}
Err(err) => {
eprintln!("Error: {}", err);
}
}
}
To generate a presigned URL for a temporary upload to S3 or another AWS service:
use aws_signer::{AwsClient, AwsOptions, AwsRequestInit};
use reqwest::{Request, Method, Url};
#[tokio::main]
async fn main() {
let client = AwsClient::new(
"your_access_key_id".to_string(),
"your_secret_access_key".to_string(),
None, // Optional session token if required
Some("s3".to_string()), // Specify the service
Some("us-east-1".to_string()), // Specify the region
None,
Some(3),
Some(100),
);
let request = Request::new(
Method::PUT,
Url::parse("https://your-bucket.s3.amazonaws.com/your-object-key").unwrap(),
);
match client.sign(request, None).await {
Ok(signed_request) => {
println!("Presigned URL: {}", signed_request.url());
}
Err(err) => {
eprintln!("Error: {}", err);
}
}
}
The library provides flexible configuration options through the AwsOptions
struct. You can customize the signing process, service, region, and more:
access_key_id
: AWS access key IDsecret_access_key
: AWS secret access keysession_token
: Optional session token for temporary credentialsservice
: AWS service name (e.g., "s3", "execute-api")region
: AWS region (e.g., "us-east-1")datetime
: Custom datetime for signingsign_query
: Boolean flag to indicate if query should be signedappend_session_token
: Boolean flag to append session tokenContributions are welcome! Please submit issues or pull requests to help improve the library.
Your support will help in continuous development, maintenance, and adding new features to this library. Thank you!
This library is licensed under the MIT License. See the LICENSE file for details.