Skip to content

examples: Improve examples for s3 #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 69 additions & 6 deletions examples/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,53 @@
// limitations under the License.

//! Example for initiating a s3 backend.
use std::env;
use std::sync::Arc;

use anyhow::Result;
use futures::AsyncReadExt;
use log::info;
use opendal::credential::Credential;
use opendal::services::s3;
use opendal::services::s3::Builder;
use opendal::Accessor;
use opendal::Object;
use opendal::Operator;

#[tokio::main]
async fn main() -> Result<()> {
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "debug");
}
env_logger::init();

println!(
r#"OpenDAL S3 Example.

Available Environment Values:

RUST_LOG: log level
OPENDAL_S3_ROOT: root path, default: /
OPENDAL_S3_BUCKET: bukcet name, required.
OPENDAL_S3_ENDPOINT: endpoint of s3 service, default: https://s3.amazonaws.com
OPENDAL_S3_REGION: region of s3 service, could be auto detected.
OPENDAL_S3_ACCESS_KEY_ID: access key id of s3 service, could be auto detected.
OPENDAL_S3_SECRET_ACCESS_KEY: secret access key of s3 service, could be auto detected.

Examples:

OPENDAL_S3_BUCKET=opendal OPENDAL_S3_ACCESS_KEY_ID=minioadmin OPENDAL_S3_SECRET_ACCESS_KEY=minioadminx OPENDAL_S3_ENDPOINT=http://127.0.0.1:9900 OPENDAL_S3_REGION=test cargo run --example s3

"#
);

// Create s3 backend builder.
let mut builder: Builder = s3::Backend::build();
// Set the root for s3, all operations will happen under this root.
//
// NOTE: the root must be absolute path.
builder.root("/path/to/dir");
builder.root(&env::var("OPENDAL_S3_ROOT").unwrap_or_default());
// Set the bucket name, this is required.
builder.bucket("bucket_name");
builder.bucket(&env::var("OPENDAL_S3_BUCKET").expect("env OPENDAL_S3_BUCKET not set"));
// Set the endpoint.
//
// For examples:
Expand All @@ -42,21 +69,57 @@ async fn main() -> Result<()> {
// - "https://cos.ap-seoul.myqcloud.com"
//
// Default to "https://s3.amazonaws.com"
builder.endpoint("https://s3.amazonaws.com");
builder.endpoint(
&env::var("OPENDAL_S3_ENDPOINT").unwrap_or_else(|_| "https://s3.amazonaws.com".to_string()),
);
// Set the region in we have this env.
if let Ok(region) = env::var("OPENDAL_S3_REGION") {
builder.region(&region);
}
// Set the credential.
//
// OpenDAL will try load credential from the env.
// If credential not set and no valid credential in env, OpenDAL will
// send request without signing like anonymous user.
builder.credential(Credential::hmac("access_key_id", "secret_access_key"));
builder.credential(Credential::hmac(
&env::var("OPENDAL_S3_ACCESS_KEY_ID").expect("env OPENDAL_S3_ACCESS_KEY_ID not set"),
&env::var("OPENDAL_S3_SECRET_ACCESS_KEY")
.expect("env OPENDAL_S3_SECRET_ACCESS_KEY not set"),
));
// Build the `Accessor`.
let accessor: Arc<dyn Accessor> = builder.finish().await?;

// `Accessor` provides the low level APIs, we will use `Operator` normally.
let op: Operator = Operator::new(accessor);

let path = uuid::Uuid::new_v4().to_string();

// Create an object handle to start operation on object.
let _: Object = op.object("test_file");
info!("try to write file: {}", &path);
op.object(&path)
.writer()
.write_bytes(String::from("Hello, world!").into_bytes())
.await?;
info!("write file successful!");

info!("try to read file: {}", &path);
let mut content = String::new();
op.object(&path)
.reader()
.read_to_string(&mut content)
.await?;
info!("read file successful, content: {}", content);

info!("try to get file metadata: {}", &path);
let meta = op.object(&path).metadata().await?;
info!(
"get file metadata successful, size: {}B",
meta.content_length()
);

info!("try to delete file: {}", &path);
let _ = op.object(&path).delete().await?;
info!("delete file successful");

Ok(())
}