/** * Background Cloud Function to be triggered by Pub/Sub. * This function is exported by index.js, and executed when * the trigger topic receives a message. * * @param {object} event The Cloud Functions event. * @param {function} callback The callback function. */
exports.helloPubSub = (event, callback) => {const pubsubMessage = event.data;
const name = pubsubMessage.data
? Buffer.from(pubsubMessage.data, 'base64').toString()
: 'World';
console.log(`Hello, ${name}!`);
callback();
};
$ curl -X POST "https://[YOUR_REGION].[YOUR_PROJECT_ID].cloudfunctions.net/sendgridEmail?sg_key=[YOUR_SENDGRID_KEY]"--data'{"to":"[YOUR_SENDER_ADDR]","from":"[YOUR_RECIPIENT_ADDR]","subject":"Hello from Sendgrid!","body":"Hello World!"}'--header"Content-Type: application/json"
$ curl -X POST "https://us-central1.my_project/sendgridEmail?sg_key=hogehogehogehogehoge"--data'{"to":"to@example.com","from":"from@example.com","subject":"Hello from Sendgrid!","body":"Hello World!"}'--header"Content-Type: application/json"
これを実行すると、以下のエラーが発生しました。
curl: (51) SSL: no alternative certificate subject name matches target host name 'us-central1.my_project.cloudfunctions.net'
$ curl -kX POST "https://us-central1.my_project/sendgridEmail?sg_key=hogehogehogehogehoge"--data'{"to":"to@example.com","from":"from@example.com","subject":"Hello from Sendgrid!","body":"Hello World!"}'--header"Content-Type: application/json"
$ gcloud beta functions logs read--limit100
....
D sendgridEmail vra---gkc 2019-01-1018:09:22.766 Function execution started
I sendgridEmail vra---gkc 2019-01-1018:09:22.773 Sending email to: to@example.com
I sendgridEmail vra---gkc 2019-01-1018:09:23.120 Email sent to: to@example.com
D sendgridEmail vra---gkc 2019-01-1018:09:23.122 Function execution took 356 ms, finished with status code: 202
D sendgridWebhook n64---gwu 2019-01-1102:05:42.699 Function execution started
I sendgridWebhook n64---gwu 2019-01-1102:05:42.707 Saving events to 1547172342706000-6630d5c0-d6f8-40d6-a630-9a844e4239b3.json in bucket gcf_sendgrid_tutorial_bucket
I sendgridWebhook n64---gwu 2019-01-1102:05:42.999 JSON written to 1547172342706000-6630d5c0-d6f8-40d6-a630-9a844e4239b3.json
D sendgridWebhook n64---gwu 2019-01-1102:05:43.001 Function execution took 302 ms, finished with status code: 200
D sendgridLoad 3537170430653002019-01-1111:29:34.810 Function execution started
I sendgridLoad 3537170430653002019-01-1111:29:35.217 Starting job for1547206173966000-13e6adf5-271c-42c5-bac3-116d89ef3ea6.json
I sendgridLoad 3537170430653002019-01-1111:29:35.218 Job failed for1547206173966000-13e6adf5-271c-42c5-bac3-116d89ef3ea6.json
E sendgridLoad 3537170430653002019-01-1111:29:35.241 TypeError: table.import is not a function
at Promise.resolve.then.then (/user_code/index.js:340:26)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
D sendgridLoad 3537170430653002019-01-1111:29:35.251 Function execution took 442 ms, finished with status: 'error'
// [START functions_sendgrid_load]/** * Cloud Function triggered by Cloud Storage when a file is uploaded. * * @param {object} event The Cloud Functions event. * @param {object} event.data A Cloud Storage file object. * @param {string} event.data.bucket Name of the Cloud Storage bucket. * @param {string} event.data.name Name of the file. * @param {string} [event.data.timeDeleted] Time the file was deleted if this is a deletion event. * @see https://cloud.google.com/storage/docs/json_api/v1/objects#resource */
exports.sendgridLoad = event => {const file = event.data;
if (file.resourceState === 'not_exists') {// This was a deletion event, we don't want to process thisreturn;
}return Promise.resolve()
.then(() => {if (!file.bucket) {thrownew Error(
'Bucket not provided. Make sure you have a "bucket" property in your request'
);
}elseif (!file.name) {thrownew Error(
'Filename not provided. Make sure you have a "name" property in your request'
);
}return getTable();
})
.then(([table]) => {const fileObj = storage.bucket(file.bucket).file(file.name);
console.log(`Starting job for ${file.name}`);
const metadata = {
autodetect: true,
sourceFormat: 'NEWLINE_DELIMITED_JSON',
};
// tableのプロパティを全て出力させるfor(var n in table){
console.log("table: " + n);
}// Error: table.import is not function ... why?
console.log("table has import: " + ("import"in table));
//return table.import(fileObj, metadata);return table.load(fileObj, metadata);
})
//.then(([job]) => job.promise())
.then(([job]) => {for(var n in job){// jobのプロパティを全て出力させる
console.log("job: " + n);
}})
.then(() => console.log(`Job complete for ${file.name}`))
.catch(err => {
console.log(`Job failed for ${file.name}`);
return Promise.reject(err);
});
};
// [END functions_sendgrid_load]
以下はその実行結果
D sendgridLoad 3573987895153662019-01-1504:02:31.061 Function execution started
I sendgridLoad 3573987895153662019-01-1504:02:32.245 Starting job for1547524950091000-396529d3-8fad-4bcb-b527-f1c24a3bb25b.json
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: domain
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: _events
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: _eventsCount
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: _maxListeners
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: metadata
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: baseUrl
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: parent
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: id
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: createMethod
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: methods
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: interceptors
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: Promise
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: requestModule
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: bigQuery
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: dataset
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: createReadStream
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: getRows_
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: setMaxListeners
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: getMaxListeners
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: emit
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: addListener
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: on
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: prependListener
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: once
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: prependOnceListener
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: removeListener
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: removeAllListeners
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: listeners
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: listenerCount
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table: eventNames
I sendgridLoad 3573987895153662019-01-1504:02:32.247 table has import: false
I sendgridLoad 3573987895153662019-01-1504:02:34.446 job: kind
I sendgridLoad 3573987895153662019-01-1504:02:34.447 job: etag
I sendgridLoad 3573987895153662019-01-1504:02:34.447 job: id
I sendgridLoad 3573987895153662019-01-1504:02:34.447 job: selfLink
I sendgridLoad 3573987895153662019-01-1504:02:34.447 job: jobReference
I sendgridLoad 3573987895153662019-01-1504:02:34.447 job: configuration
I sendgridLoad 3573987895153662019-01-1504:02:34.447 job: status
I sendgridLoad 3573987895153662019-01-1504:02:34.447 job: statistics
I sendgridLoad 3573987895153662019-01-1504:02:34.447 job: user_email
I sendgridLoad 3573987895153662019-01-1504:02:34.447 Job completefor1547524950091000-396529d3-8fad-4bcb-b527-f1c24a3bb25b.json
D sendgridLoad 3573987895153662019-01-1504:02:34.455 Function execution took 3395 ms, finished with status: 'ok'