Skip to content

Commit

Permalink
Set User Agent Header for S3 Requests (#2137)
Browse files Browse the repository at this point in the history
* Set User Agent Header for S3 Requests

Closes #2136.

This PR configures the S3 backend to use a custom HTTP client
which has a user agent header set.

The user agent header enables `sccache` users to write AWS policies to
accept or reject `sccache` requests to an S3 bucket based on their `sccache`
version.
  • Loading branch information
ajschmidt8 authored Mar 25, 2024
1 parent fce79e3 commit a859c3b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ memcached = ["opendal/services-memcached"]
native-zlib = []
oss = ["opendal/services-oss", "reqsign"]
redis = ["url", "opendal/services-redis"]
s3 = ["opendal/services-s3", "reqsign"]
s3 = ["opendal/services-s3", "reqsign", "reqwest"]
webdav = ["opendal/services-webdav"]
# Enable features that will build a vendored version of openssl and
# statically linked with it, instead of linking against the system-wide openssl
Expand Down
10 changes: 10 additions & 0 deletions src/cache/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
// limitations under the License.

use opendal::layers::LoggingLayer;
use opendal::raw::HttpClient;
use opendal::services::S3;
use opendal::Operator;
use reqwest::ClientBuilder;

use crate::errors::*;

Expand All @@ -29,6 +31,7 @@ impl S3Cache {
server_side_encryption: Option<bool>,
) -> Result<Operator> {
let mut builder = S3::default();
builder.http_client(set_user_agent());
builder.bucket(bucket);
builder.root(key_prefix);

Expand Down Expand Up @@ -63,6 +66,13 @@ impl S3Cache {
}
}

/// Set the user agent (helps with monitoring on the server side)
fn set_user_agent() -> HttpClient {
let user_agent = format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
let client_builder = ClientBuilder::new().user_agent(user_agent);
HttpClient::build(client_builder).unwrap()
}

/// Resolve given endpoint along with use_ssl settings.
fn endpoint_resolver(endpoint: &str, use_ssl: Option<bool>) -> Result<String> {
let endpoint_uri: http::Uri = endpoint
Expand Down

0 comments on commit a859c3b

Please sign in to comment.