Skip to content

Commit

Permalink
GetStorageDefaultBucket through Storage API (#6471)
Browse files Browse the repository at this point in the history
* GetStorageDefaultBucket through Storage API

* pretty it up

* Update CHANGELOG.md

---------

Co-authored-by: joehan <[email protected]>
  • Loading branch information
abhis3 and joehan authored Nov 27, 2023
1 parent dea0741 commit dc02291
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
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

0 comments on commit dc02291

Please sign in to comment.