通过firebase功能调整图像大小

时间:2019-04-14 19:40:21

标签: reactjs google-cloud-functions firebase-storage

我正在尝试使用Firebase功能自动调整图像大小。我相信我已经完成了文档的所有工作,但是仍然出现错误。我的功能

index.js

exports.resizeProfileImg = functions.storage.object().onFinalize((object) => 
{resizeProfileImg.handler(object)
})

resizeProfileImg.js

 const functions = require('firebase-functions');
 const gcs = require('@google-cloud/storage')();
 const os = require('os');
 const path = require('path');

 const spawn = require('child-process-promise').spawn

 exports.handler = ((object) => {
 const bucket = object.bucket;
 const contentType = object.contentType;
 const filePath = object.name

 const destBucket = gcs.bucket(bucket)
 const tempFilePath = path.join(os.tmpdir(), path.basename(filePath))
 const metadata = { contentType: contentType }

return destBucket.file(filePath).download({
    destination: tempFilePath
}).then(() => {
    return spawn('convert', [tempFilePath, '-resize', '150x150', tempFilePath])
}).then(() => {
    return destBucket.upload(tempFilePath, 
        {
            metadata: metadata
        })
})
});

package.json

"dependencies": {
  "@google-cloud/storage": "^2.5.0",
  "child-process-promise": "^2.2.1",
  "firebase-admin": "~7.0.0",
  "firebase-functions": "^2.2.0"
  },    
"engines": {
"node": "8"
}

我相信问题出在

const gcs = require('@google-cloud/storage')();

控制台中的错误消息

解析函数触发器时发生错误。 require(...)不是函数

如果我将其更改为

const gcs = require('@google-cloud/storage');

然后该函数部署没有任何错误,但是我在Firebase控制台中出现了错误

TypeError gcs不是函数

0 个答案:

没有答案