在存储触发的谷歌云功能中提供上下文



我正在谷歌云功能中尝试此代码,该功能由云存储节点10触发。

我不知道如何提供上下文。Event: ${context.eventId}

我尝试过:module.exports.getDevices(context(=getDevices;

我尝试过:module.exports.getDevices=getDevices(上下文(;

async function getDevices() {
console.log(`  Event: ${context.eventId}`);
const cloudRegion = '....';
const projectId = '.....';
const registryId = '.....';
const iot = require('@google-cloud/iot');
const iotClient = new iot.v1.DeviceManagerClient({});
const parentName = iotClient.registryPath(projectId, cloudRegion, registryId);
try {
const responses = await iotClient.listDevices({parent: parentName});

const devices = responses[0];     
if (devices.length > 0) {
console.log('Current devices in registry:');
} else {
console.log('No devices in registry.');
}
for (let i = 0; i < devices.length; i++) {
const device = devices[i];
console.log(`Device ${i}: `, device);
}
} catch (err) {
console.error('Could not list devices', err);
}
}
module.exports.getDevices = getDevices;

同样,这是可行的,但我想要和异步功能,就像上一个一样:

exports.helloGCS = (file, context) => {
console.log(`  Event: ${context.eventId}`);
};

如果您想要一个异步函数,只需将其声明为:

exports.helloGCS = async (file, context) => {
console.log(`  Event: ${context.eventId}`);
};

如果你想让它调用一个命名函数:

async function getDevices(file, context) {
console.log(`  Event: ${context.eventId}`);
}
exports.helloGCS = getDevices;

相关内容

  • 没有找到相关文章

最新更新