Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions functions/http/.gcloudignore
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions functions/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions functions/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 24 additions & 0 deletions functions/http/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
});