-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: support v4 signed url endpoint #10490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
7hokerz
wants to merge
1
commit into
firebase:main
Choose a base branch
from
7hokerz:7hokerz/emulator-support-v4-endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+361
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ import { StorageEmulator } from "../index"; | |
| import { EmulatorLogger } from "../../emulatorLogger"; | ||
| import { GetObjectResponse, ListObjectsResponse } from "../files"; | ||
| import type { Request, Response } from "express"; | ||
| import { parseObjectUploadMultipartRequest } from "../multipart"; | ||
| import { parseFormDataMultipartRequest, parseObjectUploadMultipartRequest } from "../multipart"; | ||
| import { Upload, UploadNotActiveError } from "../upload"; | ||
| import { ForbiddenError, NotFoundError } from "../errors"; | ||
| import { reqBodyToBuffer } from "../../shared/request"; | ||
|
|
@@ -368,6 +368,90 @@ export function createCloudEndpoints(emulator: StorageEmulator): Router { | |
| return sendFileBytes(getObjectResponse.metadata, getObjectResponse.data, req, res); | ||
| }); | ||
|
|
||
| gcloudStorageAPI.post( | ||
| "/:bucketId", | ||
| (req, res, next) => { | ||
| adminStorageLayer.createBucket(req.params.bucketId); | ||
| next(); | ||
| }, | ||
| async (req, res) => { | ||
| const contentTypeHeader = req.header("content-type"); | ||
| if (!contentTypeHeader?.includes("multipart/form-data")) { | ||
| return res.status(400).send("Content-Type must be multipart/form-data"); | ||
| } | ||
|
|
||
| try { | ||
| const bodyBuffer = await reqBodyToBuffer(req); | ||
|
|
||
| const formData = parseFormDataMultipartRequest(contentTypeHeader, bodyBuffer); | ||
|
|
||
| const keyPart = formData.find((p) => p.name === "key"); | ||
| const filePart = formData.find((p) => p.type === "file"); | ||
|
|
||
| if (keyPart?.type !== "field" || filePart?.type !== "file") { | ||
| return res.status(400).send("Missing 'key' or file."); | ||
| } | ||
|
|
||
| const metadata: IncomingMetadata = { | ||
| contentType: filePart.contentType, | ||
| metadata: {}, | ||
| }; | ||
|
|
||
| for (const part of formData) { | ||
| if (part.type === "file" || part.name === "key") continue; | ||
|
|
||
| const key = part.name.toLowerCase(); | ||
| const value = part.value.trim(); | ||
|
|
||
| if (key.startsWith("x-goog-meta-")) { | ||
| const metaKey = key.replace("x-goog-meta-", ""); | ||
| metadata.metadata![metaKey] = value; | ||
| } else { | ||
| switch (key) { | ||
| case "content-type": | ||
| metadata.contentType = value; | ||
| break; | ||
| case "cache-control": | ||
| metadata.cacheControl = value; | ||
| break; | ||
| case "content-disposition": | ||
| metadata.contentDisposition = value; | ||
| break; | ||
| case "content-encoding": | ||
| metadata.contentEncoding = value; | ||
| break; | ||
| case "content-language": | ||
| metadata.contentLanguage = value; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+400
to
+428
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. opinion nit: this seems a little easier to read to me |
||
|
|
||
| const upload = uploadService.multipartUpload({ | ||
| bucketId: req.params.bucketId, | ||
| objectId: keyPart.value, | ||
| dataRaw: filePart.data, | ||
| metadata: metadata, | ||
| authorization: req.header("authorization"), | ||
| }); | ||
|
|
||
| await adminStorageLayer.uploadObject(upload); | ||
| return res.sendStatus(204); | ||
| } catch (err) { | ||
| if (err instanceof ForbiddenError) { | ||
| return res.sendStatus(403); | ||
| } | ||
| if (err instanceof NotFoundError) { | ||
| return res.sendStatus(404); | ||
| } | ||
| if (err instanceof Error) { | ||
| return res.status(400).send(err.message); | ||
| } | ||
| throw err; | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| gcloudStorageAPI.post( | ||
| "/b/:bucketId/o/:objectId/:method(rewriteTo|copyTo)/b/:destBucketId/o/:destObjectId", | ||
| (req, res, next) => { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trimming the value of form fields can lead to data corruption for custom metadata (
x-goog-meta-*). Google Cloud Storage preserves leading and trailing spaces in custom metadata values. Since the multipart parser already handles the trailing line separator of the part, additional trimming here is likely unnecessary and potentially incorrect.