Skip to content

Instantly share code, notes, and snippets.

@elmarx
Last active March 15, 2024 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elmarx/e21d9fa5dba45a0f8fb6c910fd598ac7 to your computer and use it in GitHub Desktop.
Save elmarx/e21d9fa5dba45a0f8fb6c910fd598ac7 to your computer and use it in GitHub Desktop.
access IBM COS with aws-rust-sdk
use aws_sdk_s3::Client;
// sample bucket name of an existing bucket
const BUCKET_NAME: &str = "sweap-ui-test-dev";
/// create a service-key with HMAC enabled,
/// get the content like this: `ibmcloud resource service-key my-key`
///
/// set the environment-variables:
/// AWS_ACCESS_KEY_ID= the cos_hmac_keys.access_key_id
/// AWS_SECRET_ACCESS_KEY = the cos_hmac_keys.secret_access_key
/// AWS_REGION = "dummy"
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config = aws_config::from_env()
// get this from the endpoints section of the Cloud Object Storage resource
.endpoint_url("https://s3.eu-de.cloud-object-storage.appdomain.cloud")
// .region("dummy")
.load()
.await;
let client = Client::new(&config);
let response = client.list_objects_v2().bucket(BUCKET_NAME).send().await?;
for object in response.contents() {
println!("{}", object.key().unwrap_or_default());
}
Ok(())
}
use aws_config::Region;
use aws_credential_types::provider::SharedCredentialsProvider;
use aws_credential_types::Credentials;
use aws_sdk_s3::Client;
/// create a service-key with HMAC enabled,
/// get the content like this: `ibmcloud resource service-key my-key`
const AWS_ACCESS_KEY_ID: &str = "this is cos_hmac_keys.access_key_id";
const AWS_SECRET_ACCESS_KEY: &str = "this is cos_hmac_keys.secret_access_key";
// get this from the endpoints section of the Cloud Object Storage resource
const ENDPOINT_URL: &str = "https://s3.eu-de.cloud-object-storage.appdomain.cloud";
const BUCKET_NAME: &str = "sample bucket name to list";
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let c = Credentials::new(
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
None,
None,
"hardcoded-credentials",
);
let c = SharedCredentialsProvider::new(c);
let config = aws_config::SdkConfig::builder()
.credentials_provider(c)
.endpoint_url(ENDPOINT_URL)
// if not set, will error with "A region must be set when sending requests to S3."
.region(Some(Region::new("dummy")))
.build();
let client = Client::new(&config);
let response = client.list_objects_v2().bucket(BUCKET_NAME).send().await?;
for object in response.contents() {
println!("{}", object.key().unwrap_or_default());
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment