Skip to content

Commit d3f0b18

Browse files
feat: (storage) add flag to force empty content type (#14320) (#23568)
[upstream:f43bc3edeeebf4b23b39e0d06e5c3fe7b5aa37f1] Signed-off-by: Modular Magician <[email protected]>
1 parent a1ebbb0 commit d3f0b18

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

.changelog/14320.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
storage: added `force_empty_content_type` flag to force empty content type in resource `google_storage_bucket_object`
3+
```

google/services/storage/resource_storage_bucket_object.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,20 @@ func ResourceStorageBucketObject() *schema.Resource {
9999
},
100100

101101
"content_type": {
102-
Type: schema.TypeString,
103-
Optional: true,
104-
ForceNew: true,
105-
Computed: true,
106-
Description: `Content-Type of the object data. Defaults to "application/octet-stream" or "text/plain; charset=utf-8".`,
102+
Type: schema.TypeString,
103+
Optional: true,
104+
ForceNew: true,
105+
Computed: true,
106+
ConflictsWith: []string{"force_empty_content_type"},
107+
Description: `Content-Type of the object data. Defaults to "application/octet-stream" or "text/plain; charset=utf-8".`,
108+
},
109+
110+
"force_empty_content_type": {
111+
Type: schema.TypeBool,
112+
Optional: true,
113+
ForceNew: true,
114+
ConflictsWith: []string{"content_type"},
115+
Description: `Flag to set empty Content-Type.`,
107116
},
108117

109118
"content": {
@@ -395,7 +404,11 @@ func resourceStorageBucketObjectCreate(d *schema.ResourceData, meta interface{})
395404

396405
insertCall := objectsService.Insert(bucket, object)
397406
insertCall.Name(name)
398-
insertCall.Media(media)
407+
if v, ok := d.GetOk("force_empty_content_type"); ok && v.(bool) {
408+
insertCall.Media(media, googleapi.ContentType(""))
409+
} else {
410+
insertCall.Media(media)
411+
}
399412

400413
// This is done late as we need to add headers to enable customer encryption
401414
if v, ok := d.GetOk("customer_encryption"); ok {

google/services/storage/resource_storage_bucket_object_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,26 @@ func TestAccStorageObject_content(t *testing.T) {
145145
"google_storage_bucket_object.object", "storage_class", "STANDARD"),
146146
),
147147
},
148+
{
149+
Config: testGoogleStorageBucketsObjectEmptyContentType(bucketName),
150+
Check: resource.ComposeTestCheckFunc(
151+
testAccCheckGoogleStorageObject(t, bucketName, objectName, dataMd5),
152+
resource.TestCheckResourceAttr(
153+
"google_storage_bucket_object.object", "content_type", ""),
154+
resource.TestCheckResourceAttr(
155+
"google_storage_bucket_object.object", "storage_class", "STANDARD"),
156+
),
157+
},
158+
{
159+
Config: testGoogleStorageBucketsObjectContent(bucketName),
160+
Check: resource.ComposeTestCheckFunc(
161+
testAccCheckGoogleStorageObject(t, bucketName, objectName, dataMd5),
162+
resource.TestCheckResourceAttr(
163+
"google_storage_bucket_object.object", "content_type", "text/plain; charset=utf-8"),
164+
resource.TestCheckResourceAttr(
165+
"google_storage_bucket_object.object", "storage_class", "STANDARD"),
166+
),
167+
},
148168
},
149169
})
150170
}
@@ -724,6 +744,23 @@ resource "google_storage_bucket_object" "object" {
724744
`, bucketName, objectName, content)
725745
}
726746

747+
func testGoogleStorageBucketsObjectEmptyContentType(bucketName string) string {
748+
return fmt.Sprintf(`
749+
resource "google_storage_bucket" "bucket" {
750+
name = "%s"
751+
location = "US"
752+
force_destroy = true
753+
}
754+
755+
resource "google_storage_bucket_object" "object" {
756+
name = "%s"
757+
bucket = google_storage_bucket.bucket.name
758+
content = "%s"
759+
force_empty_content_type = true
760+
}
761+
`, bucketName, objectName, content)
762+
}
763+
727764
func testGoogleStorageBucketsFolder(bucketName, folderName string) string {
728765
return fmt.Sprintf(`
729766
resource "google_storage_bucket" "bucket" {

website/docs/r/storage_bucket_object.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ One of the following is required:
103103

104104
* `source_md5hash` - (Optional) User-provided md5hash to trigger replacement of object in storage bucket, Must be Base 64 MD5 hash of the object data. The usual way to set this is filemd5("file.zip"), where "file.zip" is the local filename
105105

106+
* `force_empty_content_type` - (Optional) When set to true, it ensure the object's Content-Type is empty.
107+
106108
---
107109

108110
<a name="nested_customer_encryption"></a>The `customer_encryption` block supports:

0 commit comments

Comments
 (0)