Skip to content

Commit

Permalink
feat(services/oss): add oss cache-control header support
Browse files Browse the repository at this point in the history
  • Loading branch information
ahonn committed Apr 14, 2023
1 parent c424555 commit 3fb91c3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
5 changes: 3 additions & 2 deletions core/src/services/oss/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ impl Accessor for OssBackend {
async fn create(&self, path: &str, _: OpCreate) -> Result<RpCreate> {
let resp = self
.core
.oss_put_object(path, None, None, None, AsyncBody::Empty)
.oss_put_object(path, None, None, None, None, AsyncBody::Empty)
.await?;
let status = resp.status();

Expand Down Expand Up @@ -403,7 +403,7 @@ impl Accessor for OssBackend {

async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
let upload_id = if args.append() {
let resp = self.core.oss_initiate_upload(path).await?;
let resp = self.core.oss_initiate_upload(path, &args).await?;
match resp.status() {
StatusCode::OK => {
let bs = resp.into_body().bytes().await?;
Expand Down Expand Up @@ -494,6 +494,7 @@ impl Accessor for OssBackend {
None,
v.content_type(),
v.content_disposition(),
v.cache_control(),
AsyncBody::Empty,
true,
)?,
Expand Down
23 changes: 21 additions & 2 deletions core/src/services/oss/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::fmt::Formatter;
use std::time::Duration;

use bytes::Bytes;
use http::header::CACHE_CONTROL;
use http::header::CONTENT_DISPOSITION;
use http::header::CONTENT_LENGTH;
use http::header::CONTENT_TYPE;
Expand All @@ -32,6 +33,7 @@ use reqsign::AliyunOssSigner;
use serde::Deserialize;
use serde::Serialize;

use crate::ops::OpWrite;
use crate::raw::*;
use crate::*;

Expand Down Expand Up @@ -105,12 +107,14 @@ impl OssCore {
}

impl OssCore {
#[allow(clippy::too_many_arguments)]
pub fn oss_put_object_request(
&self,
path: &str,
size: Option<usize>,
content_type: Option<&str>,
content_disposition: Option<&str>,
cache_control: Option<&str>,
body: AsyncBody,
is_presign: bool,
) -> Result<Request<AsyncBody>> {
Expand All @@ -130,6 +134,10 @@ impl OssCore {
req = req.header(CONTENT_DISPOSITION, pos);
}

if let Some(cache_control) = cache_control {
req = req.header(CACHE_CONTROL, cache_control)
}

let req = req.body(body).map_err(new_request_build_error)?;
Ok(req)
}
Expand Down Expand Up @@ -241,13 +249,15 @@ impl OssCore {
size: Option<usize>,
content_type: Option<&str>,
content_disposition: Option<&str>,
cache_control: Option<&str>,
body: AsyncBody,
) -> Result<Response<IncomingAsyncBody>> {
let mut req = self.oss_put_object_request(
path,
size,
content_type,
content_disposition,
cache_control,
body,
false,
)?;
Expand Down Expand Up @@ -341,9 +351,14 @@ impl OssCore {
}
}

pub async fn oss_initiate_upload(&self, path: &str) -> Result<Response<IncomingAsyncBody>> {
pub async fn oss_initiate_upload(
&self,
path: &str,
args: &OpWrite,
) -> Result<Response<IncomingAsyncBody>> {
let cache_control = args.cache_control();
let req = self
.oss_initiate_upload_request(path, None, None, AsyncBody::Empty, false)
.oss_initiate_upload_request(path, None, None, cache_control, AsyncBody::Empty, false)
.await?;
self.send(req).await
}
Expand All @@ -354,6 +369,7 @@ impl OssCore {
path: &str,
content_type: Option<&str>,
content_disposition: Option<&str>,
cache_control: Option<&str>,
body: AsyncBody,
is_presign: bool,
) -> Result<Request<AsyncBody>> {
Expand All @@ -367,6 +383,9 @@ impl OssCore {
if let Some(disposition) = content_disposition {
req = req.header(CONTENT_DISPOSITION, disposition);
}
if let Some(cache_control) = cache_control {
req = req.header(CACHE_CONTROL, cache_control);
}

let mut req = req.body(body).map_err(new_request_build_error)?;
self.sign(&mut req).await?;
Expand Down
1 change: 1 addition & 0 deletions core/src/services/oss/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl oio::Write for OssWriter {
Some(bs.len()),
self.op.content_type(),
self.op.content_disposition(),
self.op.cache_control(),
AsyncBody::Bytes(bs),
false,
)?;
Expand Down

0 comments on commit 3fb91c3

Please sign in to comment.