使用Deployment Manager在GCS存储桶创建文件夹遇400错误求助
Got it, let's break down why you're seeing that 400 error and how to resolve it.
Root Cause
When you try to create an object (even an empty folder) in GCS using the storage.v1.object resource in Deployment Manager, the default API endpoint doesn't support direct object uploads. The error message tells you exactly this: upload requests need to use the /upload/ path and include the uploadType parameter, which the standard storage.v1.object endpoint doesn't handle automatically.
Also, remember that in GCS, folders aren't actual entities—they're just zero-byte objects with names ending in a trailing slash (/). So to create a "folder", you need to create such an empty object.
Solution 1: Use Cloud Build with gsutil (Simpler Approach)
This is the most straightforward way to create folders in GCS via Deployment Manager. We'll use Cloud Build to run the gsutil mkdir command, which handles all the API endpoint details for you.
Here's the updated Deployment Manager config:
resources: - type: storage.v1.bucket name: test-bucket properties: project: my-project name: test-bucket - type: gcp-types/cloudbuild-v1:projects.builds name: create-test-folder properties: projectId: my-project steps: - name: gcr.io/google.com/cloudsdktool/cloud-sdk args: ["gsutil", "mkdir", "gs://test-bucket/test/"] metadata: dependsOn: - test-bucket # Ensure the bucket exists before creating the folder
Why this works:
gsutil mkdirautomatically creates the empty trailing-slash object needed for the folder.- Cloud Build runs the command in a serverless environment, so no need to manage VMs.
- It handles the correct API endpoint and parameters under the hood.
Solution 2: Configure storage.v1.object for Upload
If you prefer to use the native storage.v1.object resource, you need to override the API URL to use the upload endpoint and send an empty payload.
Here's the corrected config:
resources: - type: storage.v1.bucket name: test-bucket properties: project: my-project name: test-bucket - type: storage.v1.object name: test-folder properties: project: my-project bucket: test-bucket name: test/ # Trailing slash is required for folder visualization url: https://www.googleapis.com/upload/storage/v1/b/test-bucket/o?uploadType=media data: "" # Empty payload for the folder object metadata: dependsOn: - test-bucket
Key Fixes:
url: Points to the upload-specific endpoint withuploadType=media(required for uploading objects, including empty ones).name: Ends with/so GCS recognizes it as a folder.data: Empty string to create the zero-byte object.
Bonus Note
You don't actually need to create folders in GCS to upload files into them. If you upload an object named test/file.txt, the test/ folder will automatically appear in the GCS console. Creating the folder explicitly is only necessary if you want it to exist without any files inside.
内容的提问来源于stack exchange,提问作者Radhika

