Skip to content

feat: Add IPFS backend #481

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 1 commit into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ OPENDAL_OBS_BUCKET=<bucket>
OPENDAL_OBS_ENDPOINT=<endpoint>
OPENDAL_OBS_ACCESS_KEY_ID=<access_key_id>
OPENDAL_OBS_SECRET_ACCESS_KEY=<secret_access_key>
# ipfs
OPENDAL_IPFS_TEST=false
OPENDAL_IPFS_ROOT=/path/to/dir
OPENDAL_IPFS_ENDPOINT="http://localhost:5001"
65 changes: 65 additions & 0 deletions examples/ipfs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2022 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::env;

use anyhow::Result;
use futures::StreamExt;
use log::info;
use opendal::{services::ipfs, 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 IPFS Example.

Available Environment Values:

- OPENDAL_IPFS_ROOT: root path in mutable file system, default: /
- OPENDAL_IPFS_ENDPOINT: ipfs endpoint, default: localhost:5001
"#
);

let mut builder = ipfs::Backend::build();
// root must be absolute path in MFS.
builder.root(&env::var("OPENDAL_IPFS_ROOT").unwrap_or_else(|_| "/".to_string()));
builder.endpoint(
&env::var("OPENDAL_IPFS_ENDPOINT").unwrap_or_else(|_| "http://localhost:5001".to_string()),
);

let op: Operator = Operator::new(builder.build()?);

let path = "/file.txt";
info!("try to write file: {}", &path);
op.object(path).write("Hello, world!").await?;
info!("write file successful!");

let content = op.object(path).read().await?;
info!("File content: {}", String::from_utf8_lossy(&content));

let root = "/";
let mut list = op.object(root).list().await?;
info!("Listing entries in {}", &root);
while let Some(res) = list.next().await {
let item = res?;
info!("Found entry: {}", item.path())
}

Ok(())
}
1 change: 1 addition & 0 deletions src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ impl Operator {
Scheme::Hdfs => services::hdfs::Backend::from_iter(it)?.into(),
#[cfg(feature = "services-http")]
Scheme::Http => services::http::Backend::from_iter(it)?.into(),
Scheme::Ipfs => services::ipfs::Backend::from_iter(it)?.into(),
Scheme::Memory => services::memory::Builder::default().build()?.into(),
Scheme::Gcs => services::gcs::Backend::from_iter(it)?.into(),
Scheme::S3 => services::s3::Backend::from_iter(it)?.into(),
Expand Down
5 changes: 5 additions & 0 deletions src/scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub enum Scheme {
/// - Custom must not overwrite any existing services name.
/// - Custom must be lowed cases.
Custom(&'static str),
/// [ipfs][crate::services::ipfs]: IPFS mutable file system
Ipfs,
}

impl Scheme {
Expand All @@ -77,6 +79,7 @@ impl Display for Scheme {
Scheme::Gcs => write!(f, "gcs"),
#[cfg(feature = "services-http")]
Scheme::Http => write!(f, "http"),
Scheme::Ipfs => write!(f, "ipfs"),
Scheme::Memory => write!(f, "memory"),
Scheme::Obs => write!(f, "obs"),
Scheme::S3 => write!(f, "s3"),
Expand All @@ -98,6 +101,7 @@ impl FromStr for Scheme {
#[cfg(feature = "services-http")]
"http" | "https" => Ok(Scheme::Http),
"gcs" => Ok(Scheme::Gcs),
"ipfs" => Ok(Scheme::Ipfs),
"memory" => Ok(Scheme::Memory),
"obs" => Ok(Scheme::Obs),
"s3" => Ok(Scheme::S3),
Expand All @@ -116,6 +120,7 @@ impl From<Scheme> for &'static str {
#[cfg(feature = "services-http")]
Scheme::Http => "http",
Scheme::Gcs => "gcs",
Scheme::Ipfs => "ipfs",
Scheme::Memory => "memory",
Scheme::Obs => "obs",
Scheme::S3 => "s3",
Expand Down
Loading