roeap / object-store-python

Python bindings and arrow integration for the rust object_store crate.
Apache License 2.0
51 stars 8 forks source link

Add `get_opts` and `get_ranges` #9

Open kylebarron opened 7 months ago

kylebarron commented 7 months ago

I had tried with

#[pyclass(name = "GetResult")]
#[derive(Debug)]
pub struct PyGetResult {
    rt: Arc<Runtime>,
    result: Arc<Mutex<GetResult>>,
}

#[pymethods]
impl PyGetResult {
    pub fn bytes(&self) -> PyResult<Cow<[u8]>> {
        let lock = self.rt.block_on(self.result.lock());
        let obj = self
            .rt
            .block_on(lock.bytes())
            .map_err(ObjectStoreError::from)?;
        Ok(Cow::Owned(obj.to_vec()))
    }

    pub fn bytes_async<'a>(&'a self, py: Python<'a>) -> PyResult<&PyAny> {
        pyo3_asyncio::tokio::future_into_py(py, async move {
            let obj = self
                .result
                .lock()
                .await
                .bytes()
                .await
                .map_err(ObjectStoreError::from)?;
            Ok(Cow::<[u8]>::Owned(obj.to_vec()))
        })
    }
}

This is implemented on top of #6, and is expected to be reviewed after that.