Skip to content

Commit

Permalink
feat: gcs multipart upload support cache control
Browse files Browse the repository at this point in the history
  • Loading branch information
fatelei committed Aug 31, 2023
1 parent 3a76ec2 commit 9d7c67b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
9 changes: 6 additions & 3 deletions core/src/services/gcs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,12 @@ impl Accessor for GcsBackend {
}

async fn create_dir(&self, path: &str, _: OpCreateDir) -> Result<RpCreateDir> {
let mut req = self
.core
.gcs_insert_object_request(path, Some(0), None, AsyncBody::Empty)?;
let mut req = self.core.gcs_insert_object_request(
path,
Some(0),
&OpWrite::default(),
AsyncBody::Empty,
)?;

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

Expand Down
22 changes: 16 additions & 6 deletions core/src/services/gcs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Write;
Expand Down Expand Up @@ -208,7 +209,7 @@ impl GcsCore {
&self,
path: &str,
size: Option<u64>,
content_type: Option<&str>,
op: &OpWrite,
body: AsyncBody,
) -> Result<Request<AsyncBody>> {
let p = build_abs_path(&self.root, path);
Expand All @@ -233,7 +234,16 @@ impl GcsCore {

req = req.header(CONTENT_LENGTH, size.unwrap_or_default());

if let Some(storage_class) = &self.default_storage_class {
let mut meta_data = HashMap::new();
if !op.content_type().is_none() {
meta_data.insert("storageClass".to_string(), op.content_type().to_string());
}

if !op.cache_control().is_none() {
meta_data.insert("cacheControl".to_string(), op.cache_control().to_string());
}

if !meta_data.is_empty() {
let mut multipart = Multipart::new();

multipart = multipart.part(
Expand All @@ -242,12 +252,12 @@ impl GcsCore {
CONTENT_TYPE,
"application/json; charset=UTF-8".parse().unwrap(),
)
.content(json!({"storageClass": storage_class}).to_string()),
.content(json!(meta_data).to_string()),
);

let mut media_part = FormDataPart::new("media").header(
CONTENT_TYPE,
content_type
op.content_type()
.unwrap_or("application/octet-stream")
.parse()
.unwrap(),
Expand All @@ -268,8 +278,8 @@ impl GcsCore {
let req = multipart.apply(Request::post(url))?;
Ok(req)
} else {
if let Some(content_type) = content_type {
req = req.header(CONTENT_TYPE, content_type);
if !op.content_type().is_none() {
req = req.header(CONTENT_TYPE, op.content_type());
}

let req = req.body(body).map_err(new_request_build_error)?;
Expand Down
2 changes: 1 addition & 1 deletion core/src/services/gcs/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl GcsWriter {
let mut req = self.core.gcs_insert_object_request(
&percent_encode_path(&self.path),
Some(size),
self.op.content_type(),
&self.op,
body,
)?;

Expand Down

0 comments on commit 9d7c67b

Please sign in to comment.