From 2282c0cd6211d248db12874cc2d128352a2512cb Mon Sep 17 00:00:00 2001 From: 7hokerz Date: Wed, 4 Feb 2026 14:13:18 +0900 Subject: [PATCH 1/2] fix: append bucket name to V4 policy URL in emulator mode --- src/file.ts | 6 +++++- test/file.ts | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) 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..f27de05b4 100644 --- a/test/file.ts +++ b/test/file.ts @@ -3578,6 +3578,23 @@ describe('File', () => { ); }); + it('should append bucket name to the URL when using the emulator', done => { + const emulatorHost = 'http://127.0.0.1:9199'; + + process.env.STORAGE_EMULATOR_HOST = emulatorHost; + STORAGE.apiEndpoint = emulatorHost; + STORAGE.customEndpoint = true; + + file.generateSignedPostPolicyV4( + CONFIG, + (err: Error, res: SignedPostPolicyV4Output) => { + 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); From 55fcf7f25723ac42498f47935e3cdc3ca5849eaa Mon Sep 17 00:00:00 2001 From: 7hokerz Date: Mon, 9 Feb 2026 16:10:04 +0900 Subject: [PATCH 2/2] fix: Set temporary env variables for testing and cleanup --- test/file.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/file.ts b/test/file.ts index f27de05b4..14b2070aa 100644 --- a/test/file.ts +++ b/test/file.ts @@ -3580,6 +3580,9 @@ 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; @@ -3588,6 +3591,14 @@ describe('File', () => { 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();