diff --git a/functions/http/.gcloudignore b/functions/http/.gcloudignore new file mode 100644 index 0000000000..ccc4eb240e --- /dev/null +++ b/functions/http/.gcloudignore @@ -0,0 +1,16 @@ +# This file specifies files that are *not* uploaded to Google Cloud Platform +# using gcloud. It follows the same syntax as .gitignore, with the addition of +# "#!include" directives (which insert the entries of the given .gitignore-style +# file at that point). +# +# For more information, run: +# $ gcloud topic gcloudignore +# +.gcloudignore +# If you would like to upload your .git directory, .gitignore file or files +# from your .gitignore file, remove the corresponding line +# below: +.git +.gitignore + +node_modules diff --git a/functions/http/index.js b/functions/http/index.js index b7c0ef6f94..c9522f58c9 100644 --- a/functions/http/index.js +++ b/functions/http/index.js @@ -194,7 +194,8 @@ exports.uploadFile = (req, res) => { // [END functions_http_form_data] // [START functions_http_signed_url] -const storage = require('@google-cloud/storage')(); +const {Storage} = require('@google-cloud/storage'); +const storage = new Storage(); /** * HTTP function that generates a signed URL @@ -208,7 +209,7 @@ exports.getSignedUrl = (req, res) => { // TODO(developer) check that the user is authorized to upload // Get a reference to the destination file in GCS - const file = storage.bucket('my-bucket').file(req.body.filename); + const file = storage.bucket(req.body.bucket).file(req.body.filename); // Create a temporary upload URL const expiresAtMs = Date.now() + 300000; // Link expires in 5 minutes diff --git a/functions/http/package.json b/functions/http/package.json index d475eb752a..a6275b3b89 100644 --- a/functions/http/package.json +++ b/functions/http/package.json @@ -18,10 +18,11 @@ "@google-cloud/nodejs-repo-tools": "^3.0.0", "ava": "0.25.0", "proxyquire": "2.1.0", - "sinon": "4.4.2" + "sinon": "4.4.2", + "uuid": "^3.3.2" }, "dependencies": { - "@google-cloud/storage": "1.7.0", + "@google-cloud/storage": "2.3.1", "busboy": "^0.2.14", "escape-html": "^1.0.3", "safe-buffer": "5.1.2" diff --git a/functions/http/test/index.test.js b/functions/http/test/index.test.js index a6323fc387..603b374437 100644 --- a/functions/http/test/index.test.js +++ b/functions/http/test/index.test.js @@ -19,6 +19,7 @@ const Buffer = require('safe-buffer').Buffer; const proxyquire = require(`proxyquire`).noCallThru(); const sinon = require(`sinon`); const test = require(`ava`); +const uuid = require(`uuid`); const tools = require(`@google-cloud/nodejs-repo-tools`); function getSample() { @@ -222,3 +223,26 @@ test.serial(`http:cors: should respond to main request (auth)`, t => { t.true(mocks.res.send.calledOnceWith(`Hello World!`)); }); + +test.serial(`http:getSignedUrl: should process example request`, async t => { + const mocks = getMocks(); + const httpSample = getSample(); + + const reqMock = { + method: 'POST', + body: { + bucket: 'nodejs-docs-samples', + filename: `gcf-gcs-url-${uuid.v4}`, + contentType: 'application/octet-stream', + }, + }; + + httpSample.sample.getSignedUrl(reqMock, mocks.res); + + // Instead of modifying the sample to return a promise, + // use a delay here and keep the sample idiomatic + await new Promise(resolve => setTimeout(resolve, 300)); + + t.false(mocks.res.status.called); + t.true(mocks.res.send.calledOnce); +});