oittaa / gcp-storage-emulator

Local emulator for Google Cloud Storage
BSD 3-Clause "New" or "Revised" License
155 stars 42 forks source link

Compatibility with GCP Java api #282

Closed niuhr2 closed 3 months ago

niuhr2 commented 3 months ago

This is not really a bug report, more a desired feature of the README. I would like to clearly understand if there is a way to use the GCP java storage api to target the emulator. (I have done the with e.g. the Google Pubsub emulator.) I have not found a way to specify the local emulator.

Describe the bug I cannot find information on usage with the GCP storage java api.

To Reproduce Using the env variable STORAGE_EMULATOR_HOST and the corresponding entry in e.g. Spring configuration then running a Java integration test makes the api target GCP and not the emulator running locally.

Expected behavior The java storage api to use the local emulator.

System (please complete the following information):

niuhr2 commented 3 months ago

After some investigation I was able to use the GCP Java api targeting the emulator by crafting creating the options instead of relying on the autoconfiguration.

An example using a Spring test configuration:

@Bean
    @Primary
    fun storage(): Storage {
        return localhostOptions().getService()
    }
    fun localhostOptions(): StorageOptions {
        return HttpStorageOptions.newBuilder().setHost("localhost:9023")
            .setHeaderProvider(UserAgentHeaderProvider(GcpStorageAutoConfiguration::class.java))
            .setProjectId("test-project-id")
            .build()
    }