FIREBASE_STORAGE_EMULATOR_HOST Environment Variable does not work with Admin SDK
tylerfrydenlund opened this issue · comments
My environment:
- Operating System version: Mac OS Sonoma 14.0 (23A344)
- Firebase Admin SDK version: 9.2.0
- Firebase Product: Storage Emulator
The problem:
When setting the environment variable "FIREBASE_STORAGE_EMULATOR_HOST" to 127.0.0.1:9199 (the default Storage Emulator Port and IP) to use the Firebase Storage Emulator the Admin SDK does not detect this environment variable.
Other Environment variables such as "FIREBASE_DATABASE_EMULATOR_HOST" and "FIRESTORE_EMULATOR_HOST" do work perfectly fine and as expected. They make a connection to emulators on the device.
I have gone as far to test and see if this issue is persistent on other Admin SDK's such as the Node.Js SDK. It is NOT an issue that persists on the SDK for Node.js.
As see here, this was previously an issue filed for the Firebase Admin SDKin the GO Repository for the Admin SDK (see here) and was fixed here (see here) and has been since claimed to be fixed and the logic used in the Node Admin SDK project will be implemented.
The same issue previously on the Go Repo, appears to be present on the Admin Java SDK. Even using the "STORAGE_EMULATOR_HOST" environment variable as it was changed to in the GO repo, does not work.
Documentation on the Firebase Admin SDK docs is also incorrect if this is the case.
![Screenshot 2023-11-03 at 5 32 06 PM](https://private-user-images.githubusercontent.com/56804058/280431488-05cc36ce-9d37-43f3-9002-be18e492c9de.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjM0OTIwNDMsIm5iZiI6MTcyMzQ5MTc0MywicGF0aCI6Ii81NjgwNDA1OC8yODA0MzE0ODgtMDVjYzM2Y2UtOWQzNy00M2YzLTkwMDItYmUxOGU0OTJjOWRlLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODEyVDE5NDIyM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTUyNDBiYWMyZWU0ODE4ODI4NzE3OTE5MjMzZGI1NmQyZTQwNTNmYWQ5ZDRkZDhjYWY2MzdiZmQxNjgzNDQ3OWMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.T_OFeUPWau2otPBq40rmOxoqVnL3a5OQ86yuLzgOyaQ)
Steps to reproduce:
Set either environment variable "STORAGE_EMULATOR_HOST" or "FIREBASE_STORAGE_EMULATOR_HOST" to your Emulator host address and port (the default is 127.0.0.1:9199)
Use the Java Admin SDK to try and make a call like normal.
See it fail and use the production Cloud Storage instance.
Relevant Code:
Environment Variable Set via Intellij Run Configuration
![Screenshot 2023-11-03 at 5 30 56 PM](https://private-user-images.githubusercontent.com/56804058/280431382-a44f53b0-ee73-4bfc-95d1-4aa1415d4d06.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjM0OTIwNDMsIm5iZiI6MTcyMzQ5MTc0MywicGF0aCI6Ii81NjgwNDA1OC8yODA0MzEzODItYTQ0ZjUzYjAtZWU3My00YmZjLTk1ZDEtNGFhMTQxNWQ0ZDA2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODEyVDE5NDIyM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTZmMTI4ZWUxNzcxODdkNTU0OGRlMTNjY2ZmZWE5ZjdiMTdhN2YzNmE4NDlmNDk4NzhlNTkxNjFmZmIyNzVjYmQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.aBKolE9mAX8EO2VwxewxN1rAWNXL-al7cFvwunouhqQ)
InputStream input = this.getClass().getResourceAsStream("/service-account.json");
assert input != null;
GoogleCredentials googleCredentials = GoogleCredentials.fromStream(input);
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(googleCredentials)
.setProjectId("project-id")
.setDatabaseUrl("https://project-id-default-rtdb.firebaseio.com")
.setStorageBucket("project-id.appspot.com")
.build();
FirebaseApp.initializeApp(options);
InputStream fileInput = this.getClass().getResourceAsStream("/someFile.anyExtension");
Bucket bucket = StorageClient.getInstance().bucket();
bucket.create("someFile.anyExtension", fileInput, "application/anyExtension");
To use the firebase storage emulator with the admin sdk use STORAGE_EMULATOR_HOST = http://localhost:9199
in your .env
file.