You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by "imWildCat (via GitHub)" <gi...@apache.org> on 2024/06/24 18:51:11 UTC
[D] Best practice to read remote data after API breaking change in ~0.45? [opendal]
GitHub user imWildCat created a discussion: Best practice to read remote data after API breaking change in ~0.45?
Hello!
In version ~ 0.45, there's a breaking change of the read api.
Before that, it looks like (https://github.com/duyet/opendal-examples.git):
```rs
let mut builder = Fs::default();
// Set the root for fs, all operations will happen under this root.
//
// NOTE: the root must be absolute path.
builder.root("/tmp");
// `Accessor` provides the low level APIs, we will use `Operator` normally.
let op: Operator = Operator::new(builder)?
// Init with logging layer enabled.
.layer(LoggingLayer::default())
.finish();
// Read the file
let content = op.read("/hello.txt").await?;
assert_eq!(&content, b"Hello");
```
where the `fn read()` would return something like naive bytes.
However, this got changed.
```
binary operation `==` cannot be applied to type `&Buffer`rustc[Click for full compiler diagnostic](rust-analyzer-diagnostics-view:/diagnostic%20message%20%5B0%5D?0#file%3A%2F%2F%2FVolumes%2FQuickMac%2Ftemp%2F202406%2Fopendal-examples%2Fexamples%2Flocal_fs.rs)
```
I'm wondering the best practice to read remote file contents.
Previously, I found some bug of my code:
```rs
let mut remote_data_raw = operator.read(&remote_path).await?.current().to_vec();
```
where the `current()` method does not return the full bytes.
I have a naive fix about this:
```rs
let remote_data_raw = operator
.read(&remote_path)
.await?
.read_all() // any better method for serde_json::from_xxx?
.await?
.to_bytes();
let remote_data: T = serde_json::from_slice(&remote_data_raw)?;
```
GitHub link: https://github.com/apache/opendal/discussions/4802
----
This is an automatically sent email for commits@opendal.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@opendal.apache.org
Re: [D] Best practice to read remote data after API breaking change in ~0.45? [opendal]
Posted by "Xuanwo (via GitHub)" <gi...@apache.org>.
GitHub user Xuanwo added a comment to the discussion: Best practice to read remote data after API breaking change in ~0.45?
Hi, you can use opendal with serde_json in this way:
```rust
let remote_data_raw = operator.read(&remote_path).await?;
let remote_data: T = serde_json::from_reader(remote_data_raw.reader())?;
```
For more usage about our `Reader` and `Buffer` API, please refer to:
- https://docs.rs/opendal/0.47.1/opendal/struct.Reader.html
- https://docs.rs/opendal/0.47.1/opendal/struct.Buffer.html
GitHub link: https://github.com/apache/opendal/discussions/4802#discussioncomment-9877449
----
This is an automatically sent email for commits@opendal.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@opendal.apache.org
Re: [D] Best practice to read remote data after API breaking change in ~0.45? [opendal]
Posted by "imWildCat (via GitHub)" <gi...@apache.org>.
GitHub user imWildCat added a comment to the discussion: Best practice to read remote data after API breaking change in ~0.45?
wow! thank you very much!
this is really straight-forward!
https://docs.rs/opendal/0.47.1/opendal/struct.Buffer.html#:~:text=fn%20test(mut%20buf%3A%20Buffer)%20%2D%3E%20Vec%3CString%3E%20%7B%0A%20%20%20%20serde_json%3A%3Afrom_reader(buf.reader()).unwrap()%0A%7D
GitHub link: https://github.com/apache/opendal/discussions/4802#discussioncomment-9877597
----
This is an automatically sent email for commits@opendal.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@opendal.apache.org