Skip to content

GetStorageDefaultBucket through Storage API #6471

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

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
- Fix blocking functions in the emulator when using multiple codebases (#6504).
- Add force flag call-out for bypassing prompts (#6506).
- Breaking: dropped support for running the CLI on Node.js v16.
- Added support for running the CLI on Node.js v20. Installations from https://firebase.tools will now use Node.js 20.
- Switched Storage deployment to use GetDefaultBucket endpoint to fetch default Storage bucket. (#6467)
- Fixed an issue with emulating blocking functions when using multiple codebases (#6504).
- Added force flag call-out for bypassing prompts (#6506).
22 changes: 17 additions & 5 deletions src/gcp/storage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Readable } from "stream";
import * as path from "path";

import { storageOrigin } from "../api";
import { firebaseStorageOrigin, storageOrigin } from "../api";
import { Client } from "../apiv2";
import { FirebaseError } from "../error";
import { logger } from "../logger";
import { getFirebaseProject } from "../management/projects";
import { ensure } from "../ensureApiEnabled";

/** Bucket Interface */
interface BucketResponse {
Expand Down Expand Up @@ -133,22 +133,34 @@ interface ListBucketsResponse {
];
}

interface GetDefaultBucketResponse {
name: string;
location: string;
bucket: {
name: string;
};
}

/** Response type for obtaining the storage service agent */
interface StorageServiceAccountResponse {
email_address: string;
kind: string;
}

export async function getDefaultBucket(projectId: string): Promise<string> {
await ensure(projectId, "firebasestorage.googleapis.com", "storage", false);
try {
const metadata = await getFirebaseProject(projectId);
if (!metadata.resources?.storageBucket) {
const localAPIClient = new Client({ urlPrefix: firebaseStorageOrigin, apiVersion: "v1alpha" });
const response = await localAPIClient.get<GetDefaultBucketResponse>(
`/projects/${projectId}/defaultBucket`
);
if (!response.body?.bucket.name) {
logger.debug("Default storage bucket is undefined.");
throw new FirebaseError(
"Your project is being set up. Please wait a minute before deploying again."
);
}
return metadata.resources.storageBucket;
return response.body.bucket.name.split("/").pop()!;
} catch (err: any) {
logger.info(
"\n\nThere was an issue deploying your functions. Verify that your project has a Google App Engine instance setup at https://console.cloud.google.com/appengine and try again. If this issue persists, please contact support."
Expand Down