object_store_opendal/service/
core.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::borrow::Cow;
19
20use object_store::{
21    Attribute, AttributeValue, GetOptions, GetRange, ObjectMeta, PutOptions, PutResult,
22};
23use opendal::raw::*;
24use opendal::*;
25
26/// Parse OpStat arguments to object_store GetOptions for head requests
27pub fn parse_op_stat(args: &OpStat) -> Result<GetOptions> {
28    let mut options = GetOptions {
29        head: true, // This is a head request
30        ..Default::default()
31    };
32
33    if let Some(version) = args.version() {
34        options.version = Some(version.to_string());
35    }
36
37    if let Some(if_match) = args.if_match() {
38        options.if_match = Some(if_match.to_string());
39    }
40
41    if let Some(if_none_match) = args.if_none_match() {
42        options.if_none_match = Some(if_none_match.to_string());
43    }
44
45    if let Some(if_modified_since) = args.if_modified_since() {
46        options.if_modified_since = Some(if_modified_since);
47    }
48
49    if let Some(if_unmodified_since) = args.if_unmodified_since() {
50        options.if_unmodified_since = Some(if_unmodified_since);
51    }
52
53    Ok(options)
54}
55
56/// Parse OpRead arguments to object_store GetOptions
57pub fn parse_op_read(args: &OpRead) -> Result<GetOptions> {
58    let mut options = GetOptions::default();
59
60    if let Some(version) = args.version() {
61        options.version = Some(version.to_string());
62    }
63
64    if let Some(if_match) = args.if_match() {
65        options.if_match = Some(if_match.to_string());
66    }
67
68    if let Some(if_none_match) = args.if_none_match() {
69        options.if_none_match = Some(if_none_match.to_string());
70    }
71
72    if let Some(if_modified_since) = args.if_modified_since() {
73        options.if_modified_since = Some(if_modified_since);
74    }
75
76    if let Some(if_unmodified_since) = args.if_unmodified_since() {
77        options.if_unmodified_since = Some(if_unmodified_since);
78    }
79
80    if !args.range().is_full() {
81        let range = args.range();
82        match range.size() {
83            Some(size) => {
84                options.range = Some(GetRange::Bounded(range.offset()..range.offset() + size));
85            }
86            None => {
87                options.range = Some(GetRange::Offset(range.offset()));
88            }
89        }
90    }
91
92    Ok(options)
93}
94
95/// Parse OpWrite arguments to object_store PutOptions
96pub fn parse_op_write(args: &OpWrite) -> Result<PutOptions> {
97    let mut opts = PutOptions::default();
98
99    if let Some(content_type) = args.content_type() {
100        opts.attributes.insert(
101            Attribute::ContentType,
102            AttributeValue::from(content_type.to_string()),
103        );
104    }
105
106    if let Some(content_disposition) = args.content_disposition() {
107        opts.attributes.insert(
108            Attribute::ContentDisposition,
109            AttributeValue::from(content_disposition.to_string()),
110        );
111    }
112
113    if let Some(cache_control) = args.cache_control() {
114        opts.attributes.insert(
115            Attribute::CacheControl,
116            AttributeValue::from(cache_control.to_string()),
117        );
118    }
119
120    if let Some(user_metadata) = args.user_metadata() {
121        for (key, value) in user_metadata {
122            opts.attributes.insert(
123                Attribute::Metadata(Cow::from(key.to_string())),
124                AttributeValue::from(value.to_string()),
125            );
126        }
127    }
128    Ok(opts)
129}
130
131/// Convert PutOptions to PutMultipartOptions
132pub fn format_put_multipart_options(opts: PutOptions) -> object_store::PutMultipartOptions {
133    object_store::PutMultipartOptions {
134        attributes: opts.attributes,
135        ..Default::default()
136    }
137}
138
139/// Format PutResult to OpenDAL Metadata
140pub fn format_put_result(result: PutResult) -> Metadata {
141    let mut metadata = Metadata::new(EntryMode::FILE);
142    if let Some(etag) = &result.e_tag {
143        metadata.set_etag(etag);
144    }
145    if let Some(version) = &result.version {
146        metadata.set_version(version);
147    }
148    metadata
149}
150
151/// Format `object_store::ObjectMeta` to `opendal::Metadata`.
152pub fn format_metadata(meta: &ObjectMeta) -> Metadata {
153    let mut metadata = Metadata::new(EntryMode::FILE);
154    metadata.set_content_length(meta.size);
155    metadata.set_last_modified(meta.last_modified);
156    if let Some(etag) = &meta.e_tag {
157        metadata.set_etag(etag);
158    }
159    if let Some(version) = &meta.version {
160        metadata.set_version(version);
161    }
162    metadata
163}