我是iOS开发人员,我已开始使用Firebase开发应用程序。最近firebase收到了新功能,云功能。
我想创建我的第一个云功能,它可以生成从上传的照片到存储的缩略图。我从documentation复制了示例代码。
所以我的index.js
看起来像这样:
const functions = require('firebase-functions');
const mkdirp = require('mkdirp-promise');
const admin = require('firebase-admin');
const gcs = require('@google-cloud/storage')();
const spawn = require('child-process-promise').spawn;
const _ = require('lodash');
const LOCAL_TMP_FOLDER = '/tmp/';
// Max height and width of the thumbnail in pixels.
const THUMB_MAX_HEIGHT = 200;
const THUMB_MAX_WIDTH = 200;
// Thumbnail prefix added to file names.
const THUMB_PREFIX = 'thumb_';
admin.initializeApp(functions.config().firebase);
exports.generateThumbnail = functions.storage.object().onChange(event => {
const filePath = event.data.name;
const filePathSplit = filePath.split('/');
const fileName = filePathSplit.pop();
const fileDir = filePathSplit.join('/') + (filePathSplit.length > 0 ? '/' : '');
const thumbFilePath = `${fileDir}${THUMB_PREFIX}${fileName}`;
const tempLocalDir = `${LOCAL_TMP_FOLDER}${fileDir}`;
const tempLocalFile = `${tempLocalDir}${fileName}`;
const tempLocalThumbFile = `${LOCAL_TMP_FOLDER}${thumbFilePath}`;
// Exit if this is triggered on a file that is not an image.
if (!event.data.contentType.startsWith('image/')) {
console.log('This is not an image.');
return;
}
// Exit if the image is already a thumbnail.
if (fileName.startsWith(THUMB_PREFIX)) {
console.log('Already a Thumbnail.');
return;
}
// Create the temp directory where the storage file will be downloaded.
return mkdirp(tempLocalDir).then(() => {
// Download file from bucket.
const bucket = gcs.bucket(event.data.bucket);
return bucket.file(filePath).download({
destination: tempLocalFile
}).then(() => {
console.log('The file has been downloaded to', tempLocalFile);
// Generate a thumbnail using ImageMagick.
return spawn('convert', [tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile]).then(() => {
console.log('Thumbnail created at', tempLocalThumbFile);
// Uploading the Thumbnail.
return bucket.upload(tempLocalThumbFile, {
destination: thumbFilePath
}).then(() => {
console.log('Thumbnail uploaded to Storage at', thumbFilePath);
});
});
});
});
});
我的package.json
:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"dependencies": {
"@google-cloud/storage": "^0.8.0",
"child-process-promise": "^2.2.0",
"firebase-admin": "^4.1.2",
"firebase-functions": "^0.5.1",
"lodash": "^4.17.4",
"mkdirp": "^0.5.1",
"mkdirp-promise": "^4.0.0"
},
"private": true
}
在firebase deploy --only functions
之后我得到了这个:
i deploying functions
i functions: ensuring necessary APIs are enabled...
i runtimeconfig: ensuring necessary APIs are enabled...
✔ functions: all necessary APIs are enabled
✔ runtimeconfig: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (2.64 KB) for uploading
✔ functions: functions folder uploaded successfully
i starting release process (may take several minutes)...
⚠ functions[generateThumbnail]: Deploy Error: Failed to configure trigger GCS Bucket: undefined
我做错了什么?
PS。 我是JS新手。也许这个问题很简单。 :