Google存储空间不是构造函数错误 [英] Google Storage is not a constructor error

查看:103
本文介绍了Google存储空间不是构造函数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个App,我的目标是每次有人将图像上传到Firebase存储时,云功能都会调整该图像的大小.

I am building an App and my objective is every time someone upload an image to firebase storage, the cloud function resize that image.

...
import * as Storage from '@google-cloud/storage'
const gcs = new Storage()
...

exports.resizeImage = functions.storage.object().onFinalize( async object => {
   const bucket = gcs.bucket(object.bucket);
   const filePath = object.name;
   const fileName = filePath.split('/').pop();
   const bucketDir = dirname(filePath);
....

当我尝试部署此功能时,出现此错误:

And when I tried to deploy this funtion I get this error:

Error: Error occurred while parsing your function triggers.

TypeError: Storage is not a constructor

我尝试使用"new Storage()"或仅使用"Storage",但没有任何效果.

I tried with "new Storage()" or just "Storage" and nothing works.

我是这里的新手,所以如果有什么我忘了给你调试的话,请告诉我.

I'm newbie around here so if there's anything I forgot for you to debug this just let me know.

谢谢!

google-cloud/存储:2.0.0

google-cloud/storage: 2.0.0

Node js:v8.11.4

Node js: v8.11.4

推荐答案

The API documentation for Cloud Storage suggests that you should use require to load the module:

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

这适用于2.x之前的Cloud Storage版本.

This applied to versions of Cloud Storage prior to version 2.x.

在2.x版本中,API发生了重大变化.您需要立即执行以下操作:

In 2.x, there was a breaking API change. You need to do this now:

const { Storage } = require('@google-cloud/storage');

如果您想要TypeScript绑定,请考虑通过Firebase Admin SDK使用Cloud Storage. 管理SDK只是包装了Cloud Storage模块,并且还导出了类型绑定随之而来.易于使用:

If you would like TypeScript bindings, consider using Cloud Storage via the Firebase Admin SDK. The Admin SDK simply wraps the Cloud Storage module, and also exports type bindings to go along with it. It's easy to use:

import * as admin from 'firebase-admin'
admin.initializeApp()
admin.storage().bucket(...)

admin.storage()为您提供了要使用的存储对象的引用.

admin.storage() gives you a reference to the Storage object that you're trying to work with.

这篇关于Google存储空间不是构造函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆