Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

fix: StorageOptions.apiEndpoint overrides simple and resumable uploads - #1161

Merged
jkwlui merged 17 commits into
masterfrom
api-endpoint-upload
May 14, 2020
Merged

jkwlui merged 17 commits into
masterfrom
api-endpoint-upload

Conversation

@jkwlui

@jkwlui jkwlui commented Apr 22, 2020 •

Copy link
Copy Markdown
Contributor
  • StorageOptions.apiEndpoint overrides simple upload url, if set.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Apr 22, 2020
@jkwlui jkwlui self-assigned this Apr 22, 2020
@jkwlui jkwlui linked an issue Apr 22, 2020 that may be closed by this pull request
@codecov

codecov Bot commented Apr 22, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #1161 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1161   +/-   ##
=======================================
  Coverage   99.17%   99.18%           
=======================================
  Files          12       12           
  Lines       11454    11471   +17     
  Branches      594      530   -64     
=======================================
+ Hits        11360    11377   +17     
  Misses         94       94           
Impacted Files Coverage Δ
src/file.ts 99.94% <100.00%> (-0.01%) ⬇️
src/storage.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d680e9f...33a0151. Read the comment docs.

Comment thread src/storage.ts Outdated
process.env.STORAGE_EMULATOR_HOST ||
`https://${options.apiEndpoint}/storage/v1`;

// Overrides options.apiEndpoint.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user provides "options.apiEndpoint", that should override the env var. Similar to options.keyFilename beating ADC.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/storage.ts
`https://${options.apiEndpoint}/storage/v1`;

// Overrides options.apiEndpoint.
const EMULATOR_HOST = process.env.STORAGE_EMULATOR_HOST;

@stephenplusplus stephenplusplus Apr 22, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's document the format expected for the env var. It looks like it needs to be a full protocol://host/path, however in the following line, the protocol will be lost.

To compare, nodejs-datastore accepts 'https://localhost:8080' or 'localhost:8080'

https://github.com/googleapis/nodejs-datastore/blob/68790d26d69f48d780930f90be6758f7431f61c9/src/index.ts#L97
https://github.com/googleapis/nodejs-datastore/blob/68790d26d69f48d780930f90be6758f7431f61c9/src/index.ts#L866

Can/should we accept both here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure what we want to do here -

So we have STORAGE_EMULATOR_HOST that provides the full URL to override baseUrl upon which all requests are based, except simple upload and resumable upload.

StorageOptions.apiEndpoint also does not override simple upload and resumable upload, since it only affects the baseUrl set, as far as I can tell.

Simple upload currently uses STOARGE_UPLOAD_BASE_URL which is hardcoded to https://storage.googleapis.com/upload/storage/v1/b.

Resumable upload currently uses https://${this.apiEndpoint}/upload/storage/v1/b, where apiEndpoint defaults to storage.googleapis.com, which is overridable through cfg.apiEndpoint passed to resumableUpload.createURI, but apiEndpoint doesn't seem to be passed.
https://github.com/googleapis/gcs-resumable-upload/blob/master/src/index.ts#L212
https://github.com/googleapis/nodejs-storage/blob/master/src/file.ts#L1520-L1536

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for tracking all of those scenarios down. It is a bit messy, eh?

So we have STORAGE_EMULATOR_HOST that provides the full URL to override baseUrl upon which all requests are based, except simple upload and resumable upload.

AFAIK, STORAGE_EMULATOR_HOST is not an official, Google-made env var. So we get a bit of play here to define it ourselves.

We should have it also affect the simple and resumable paths.

StorageOptions.apiEndpoint also does not override simple upload and resumable upload, since it only affects the baseUrl set, as far as I can tell.

We should make sure it overrides it when apiEndpoint is set, too.

Simple upload currently uses [...] https://storage.googleapis.com/upload/storage/v1/b
Resumable upload currently uses https://${this.apiEndpoint}/upload/storage/v1/b

Let's just sync these up.

Overall, what I think would be least surprising for users:

For here-ish:

const url =

const url =
  sanitize(options.apiEndpoint) || 
  sanitize(process.env.STORAGE_EMULATOR_HOST) ||
  'https://storage.googleapis.com'

url += '/storage/v1'

function sanitize(url) {
  // prepend default protocol if necessary
}

I know those upload URLs are hardcoded in file.ts, so we'd have to pick them up from the instance instead.

  • Users would always handle the full URL path, as opposed to having to figure out which part we cut out
  • It would allow both protocol and port to be overridden to match their mock env

Is this solution possible, or maybe not even a good option for what you think users would want?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it appears our benchmarking fake serves the API without the '/storage/v1' prefix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably change our benchmark mock? I still have the same concerns about what a user would find obvious when it comes to what path we send a request to. The easiest thing IMO is to only allow overriding the section in bold: protocol://host:port/path, regardless of when path starts with "storage/v1" or anything else.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkwlui Is this how it works currently? ^ the part about replacing the section in bold.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for bringing it up @stephenplusplus. I've decided to keep STORAGE_EMULATOR_HOST working as it is right now, since other languages also depend on the benchmark mock. I understand this is inflexible and potentially confusing, but since this should be a private option I think it's okay for now.

Comment thread src/storage.ts Resolved
@ojhaujjwal

ojhaujjwal commented May 4, 2020 •

Copy link
Copy Markdown

Can we please prioritize this? I am affected by this. #1178

@jkwlui

jkwlui commented May 8, 2020

Copy link
Copy Markdown
Contributor Author

Yes @ojhaujjwal, sorry for the delay!

Comment thread src/file.ts Outdated

const apiEndpoint = this.storage.apiEndpoint;
const bucketName = this.bucket.name;
const uri = `https://${apiEndpoint}/upload/storage/v1/b/${bucketName}/o`;

@stristr stristr May 10, 2020 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const uri = `https://${apiEndpoint}/upload/storage/v1/b/${bucketName}/o`;
const uri = `${apiEndpoint}/upload/storage/v1/b/${bucketName}/o`;

@jkwlui
jkwlui requested a review from a team May 12, 2020 18:16
@jkwlui

jkwlui commented May 13, 2020

Copy link
Copy Markdown
Contributor Author

@stephenplusplus can you help take a look so I'm not missing anything?

Comment thread src/storage.ts Resolved
@stephenplusplus

Copy link
Copy Markdown
Contributor

@jkwlui I pinged you in a couple of threads in this PR. It looks good to me after those.

@jkwlui
jkwlui requested a review from stephenplusplus May 13, 2020 22:03
@stristr

stristr commented May 13, 2020

Copy link
Copy Markdown

Hey @jkwlui, one more thing—looks like startResumableUpload_ is not passing apiEndpoint in

const uploadStream = resumableUpload.upload(...);

@jkwlui jkwlui added the kokoro:run Add this label to force Kokoro to re-run the tests. label May 14, 2020
@jkwlui jkwlui changed the title fix: StorageOptions.apiEndpoint overrides simple upload url fix: StorageOptions.apiEndpoint overrides simple and resumable uploads May 14, 2020
@jkwlui
jkwlui merged commit cf8ea5c into master May 14, 2020
@jkwlui
jkwlui deleted the api-endpoint-upload branch May 14, 2020 22:25
@rajwilkhu

Copy link
Copy Markdown

Thanks for pushing this change - was going to create a PR to do the same until I saw this. When is this scheduled for release?

@stristr

stristr commented May 24, 2020

Copy link
Copy Markdown

When is this scheduled for release?

@rajwilkhu, this was released in version 5.0.1.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla: yes This human has signed the Contributor License Agreement. kokoro:run Add this label to force Kokoro to re-run the tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Simple file upload doesn't respect apiEndpoint

7 participants