diff --git a/src/file.ts b/src/file.ts index 5c6f4e681..7a85ae96e 100644 --- a/src/file.ts +++ b/src/file.ts @@ -2937,7 +2937,11 @@ class File extends ServiceObject { let url: string; - if (this.storage.customEndpoint) { + const EMULATOR_HOST = process.env.STORAGE_EMULATOR_HOST; + + if (this.storage.customEndpoint && typeof EMULATOR_HOST === 'string') { + url = `${this.storage.apiEndpoint}/${this.bucket.name}`; + } else if (this.storage.customEndpoint) { url = this.storage.apiEndpoint; } else if (options.virtualHostedStyle) { url = `https://${this.bucket.name}.storage.${universe}/`; diff --git a/test/file.ts b/test/file.ts index b9a96f8cb..14b2070aa 100644 --- a/test/file.ts +++ b/test/file.ts @@ -3578,6 +3578,34 @@ describe('File', () => { ); }); + it('should append bucket name to the URL when using the emulator', done => { + const emulatorHost = 'http://127.0.0.1:9199'; + const originalApiEndpoint = STORAGE.apiEndpoint; + const originalCustomEndpoint = STORAGE.customEndpoint; + const originalEnvHost = process.env.STORAGE_EMULATOR_HOST; + + process.env.STORAGE_EMULATOR_HOST = emulatorHost; + STORAGE.apiEndpoint = emulatorHost; + STORAGE.customEndpoint = true; + + file.generateSignedPostPolicyV4( + CONFIG, + (err: Error, res: SignedPostPolicyV4Output) => { + STORAGE.apiEndpoint = originalApiEndpoint; + STORAGE.customEndpoint = originalCustomEndpoint; + if (originalEnvHost) { + process.env.STORAGE_EMULATOR_HOST = originalEnvHost; + } else { + delete process.env.STORAGE_EMULATOR_HOST; + } + + assert.ifError(err); + assert.strictEqual(res.url, `${emulatorHost}/${BUCKET.name}`); + done(); + } + ); + }); + describe('expires', () => { it('should accept Date objects', done => { const expires = new Date(Date.now() + 1000 * 60);