Skip to content
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
26 changes: 6 additions & 20 deletions core/src/services/gcs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,7 @@ impl Accessor for GcsBackend {
}

async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
let resp = self
.core
.gcs_get_object(path, args.range(), args.if_match(), args.if_none_match())
.await?;
let resp = self.core.gcs_get_object(path, &args).await?;

if resp.status().is_success() {
let meta = parse_into_metadata(path, resp.headers())?;
Expand Down Expand Up @@ -422,10 +419,7 @@ impl Accessor for GcsBackend {
return Ok(RpStat::new(Metadata::new(EntryMode::DIR)));
}

let resp = self
.core
.gcs_get_object_metadata(path, args.if_match(), args.if_none_match())
.await?;
let resp = self.core.gcs_get_object_metadata(path, &args).await?;

if resp.status().is_success() {
// read http response body
Expand Down Expand Up @@ -549,19 +543,11 @@ impl Accessor for GcsBackend {
async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
// We will not send this request out, just for signing.
let mut req = match args.operation() {
PresignOperation::Stat(v) => {
PresignOperation::Stat(v) => self.core.gcs_head_object_xml_request(path, v)?,
PresignOperation::Read(v) => self.core.gcs_get_object_xml_request(path, v)?,
PresignOperation::Write(v) => {
self.core
.gcs_head_object_xml_request(path, v.if_match(), v.if_none_match())?
}
PresignOperation::Read(v) => self.core.gcs_get_object_xml_request(
path,
v.range(),
v.if_match(),
v.if_none_match(),
)?,
PresignOperation::Write(_) => {
self.core
.gcs_insert_object_xml_request(path, None, AsyncBody::Empty)?
.gcs_insert_object_xml_request(path, v, AsyncBody::Empty)?
}
};

Expand Down
61 changes: 22 additions & 39 deletions core/src/services/gcs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,7 @@ impl GcsCore {
}

impl GcsCore {
pub fn gcs_get_object_request(
&self,
path: &str,
range: BytesRange,
if_match: Option<&str>,
if_none_match: Option<&str>,
) -> Result<Request<AsyncBody>> {
pub fn gcs_get_object_request(&self, path: &str, args: &OpRead) -> Result<Request<AsyncBody>> {
let p = build_abs_path(&self.root, path);

let url = format!(
Expand All @@ -164,14 +158,14 @@ impl GcsCore {

let mut req = Request::get(&url);

if let Some(if_match) = if_match {
if let Some(if_match) = args.if_match() {
req = req.header(IF_MATCH, if_match);
}
if let Some(if_none_match) = if_none_match {
if let Some(if_none_match) = args.if_none_match() {
req = req.header(IF_NONE_MATCH, if_none_match);
}
if !range.is_full() {
req = req.header(http::header::RANGE, range.to_header());
if !args.range().is_full() {
req = req.header(http::header::RANGE, args.range().to_header());
}

let req = req
Expand All @@ -185,24 +179,22 @@ impl GcsCore {
pub fn gcs_get_object_xml_request(
&self,
path: &str,
range: BytesRange,
if_match: Option<&str>,
if_none_match: Option<&str>,
args: &OpRead,
) -> Result<Request<AsyncBody>> {
let p = build_abs_path(&self.root, path);

let url = format!("{}/{}/{}", self.endpoint, self.bucket, p);

let mut req = Request::get(&url);

if let Some(if_match) = if_match {
if let Some(if_match) = args.if_match() {
req = req.header(IF_MATCH, if_match);
}
if let Some(if_none_match) = if_none_match {
if let Some(if_none_match) = args.if_none_match() {
req = req.header(IF_NONE_MATCH, if_none_match);
}
if !range.is_full() {
req = req.header(http::header::RANGE, range.to_header());
if !args.range().is_full() {
req = req.header(http::header::RANGE, args.range().to_header());
}

let req = req
Expand All @@ -215,11 +207,9 @@ impl GcsCore {
pub async fn gcs_get_object(
&self,
path: &str,
range: BytesRange,
if_match: Option<&str>,
if_none_match: Option<&str>,
args: &OpRead,
) -> Result<Response<IncomingAsyncBody>> {
let mut req = self.gcs_get_object_request(path, range, if_match, if_none_match)?;
let mut req = self.gcs_get_object_request(path, args)?;

self.sign(&mut req).await?;
self.send(req).await
Expand Down Expand Up @@ -316,7 +306,7 @@ impl GcsCore {
pub fn gcs_insert_object_xml_request(
&self,
path: &str,
content_type: Option<&str>,
args: &OpWrite,
body: AsyncBody,
) -> Result<Request<AsyncBody>> {
let p = build_abs_path(&self.root, path);
Expand All @@ -325,7 +315,7 @@ impl GcsCore {

let mut req = Request::put(&url);

if let Some(content_type) = content_type {
if let Some(content_type) = args.content_type() {
req = req.header(CONTENT_TYPE, content_type);
}

Expand All @@ -342,12 +332,7 @@ impl GcsCore {
Ok(req)
}

pub fn gcs_head_object_request(
&self,
path: &str,
if_match: Option<&str>,
if_none_match: Option<&str>,
) -> Result<Request<AsyncBody>> {
pub fn gcs_head_object_request(&self, path: &str, args: &OpStat) -> Result<Request<AsyncBody>> {
let p = build_abs_path(&self.root, path);

let url = format!(
Expand All @@ -359,11 +344,11 @@ impl GcsCore {

let mut req = Request::get(&url);

if let Some(if_none_match) = if_none_match {
if let Some(if_none_match) = args.if_none_match() {
req = req.header(IF_NONE_MATCH, if_none_match);
}

if let Some(if_match) = if_match {
if let Some(if_match) = args.if_match() {
req = req.header(IF_MATCH, if_match);
}

Expand All @@ -378,20 +363,19 @@ impl GcsCore {
pub fn gcs_head_object_xml_request(
&self,
path: &str,
if_match: Option<&str>,
if_none_match: Option<&str>,
args: &OpStat,
) -> Result<Request<AsyncBody>> {
let p = build_abs_path(&self.root, path);

let url = format!("{}/{}/{}", self.endpoint, self.bucket, p);

let mut req = Request::head(&url);

if let Some(if_none_match) = if_none_match {
if let Some(if_none_match) = args.if_none_match() {
req = req.header(IF_NONE_MATCH, if_none_match);
}

if let Some(if_match) = if_match {
if let Some(if_match) = args.if_match() {
req = req.header(IF_MATCH, if_match);
}

Expand All @@ -405,10 +389,9 @@ impl GcsCore {
pub async fn gcs_get_object_metadata(
&self,
path: &str,
if_match: Option<&str>,
if_none_match: Option<&str>,
args: &OpStat,
) -> Result<Response<IncomingAsyncBody>> {
let mut req = self.gcs_head_object_request(path, if_match, if_none_match)?;
let mut req = self.gcs_head_object_request(path, args)?;

self.sign(&mut req).await?;

Expand Down