From 71bf492b6fc3c76670629118c5c2c2639fc31e49 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Thu, 29 Feb 2024 14:26:26 -0800 Subject: [PATCH] blob/gcsblob: Ensure driver sets Content-Type auto-detection properly https://github.com/google/go-cloud/pull/3371 made it possible to disable Go Cloud's Content-Type auto-detection via the `DisableContentTypeDetection` option. However, the Google Cloud Storage (GCS) driver still performed auto-detection even if this option were enabled, so it was previously not possible to keep `Content-Type` blank. This commit adds the `DisableContentTypeDetection` option to the the driver and passes along the value to make it possible to keep Content-Type blank in the GCS object metadata. This enables the use of the `Response-Content-Type` override in a signed URL. This commit needs cloud.google.com/go/storage v1.39 (https://github.com/googleapis/google-cloud-go/pull/9431). --- blob/blob.go | 17 +++++++++-------- blob/driver/driver.go | 4 ++++ blob/gcsblob/gcsblob.go | 1 + 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/blob/blob.go b/blob/blob.go index a1a14d85d4..d64ed22fc2 100644 --- a/blob/blob.go +++ b/blob/blob.go @@ -1083,14 +1083,15 @@ func (b *Bucket) NewWriter(ctx context.Context, key string, opts *WriterOptions) opts = &WriterOptions{} } dopts := &driver.WriterOptions{ - CacheControl: opts.CacheControl, - ContentDisposition: opts.ContentDisposition, - ContentEncoding: opts.ContentEncoding, - ContentLanguage: opts.ContentLanguage, - ContentMD5: opts.ContentMD5, - BufferSize: opts.BufferSize, - MaxConcurrency: opts.MaxConcurrency, - BeforeWrite: opts.BeforeWrite, + CacheControl: opts.CacheControl, + ContentDisposition: opts.ContentDisposition, + ContentEncoding: opts.ContentEncoding, + ContentLanguage: opts.ContentLanguage, + ContentMD5: opts.ContentMD5, + BufferSize: opts.BufferSize, + MaxConcurrency: opts.MaxConcurrency, + BeforeWrite: opts.BeforeWrite, + DisableContentTypeDetection: opts.DisableContentTypeDetection, } if len(opts.Metadata) > 0 { // Services are inconsistent, but at least some treat keys diff --git a/blob/driver/driver.go b/blob/driver/driver.go index 1fbae1a2d9..0fa885f173 100644 --- a/blob/driver/driver.go +++ b/blob/driver/driver.go @@ -100,6 +100,10 @@ type WriterOptions struct { // Metadata holds key/value strings to be associated with the blob. // Keys are guaranteed to be non-empty and lowercased. Metadata map[string]string + // When true, the driver should attempt to disable any automatic + // content-type detection that the provider applies on writes with an + // empty ContentType. + DisableContentTypeDetection bool // BeforeWrite is a callback that must be called exactly once before // any data is written, unless NewTypedWriter returns an error, in // which case it should not be called. diff --git a/blob/gcsblob/gcsblob.go b/blob/gcsblob/gcsblob.go index 737b2c12d8..8ff2890834 100644 --- a/blob/gcsblob/gcsblob.go +++ b/blob/gcsblob/gcsblob.go @@ -628,6 +628,7 @@ func (b *bucket) NewTypedWriter(ctx context.Context, key string, contentType str w.ChunkSize = bufferSize(opts.BufferSize) w.Metadata = opts.Metadata w.MD5 = opts.ContentMD5 + w.ForceEmptyContentType = opts.DisableContentTypeDetection return w }