简体   繁体   English

如何在 firebase 云函数中包含用于 Google 云存储的 Typescript 类型

[英]How to include Typescript types for Google cloud storage in firebase cloud functions

I have a Firebase cloud function written in Typescript where I access a Google storage bucket.我有一个 Firebase 云 function 写在 Typescript 中,我可以在其中访问 Google 存储桶。

If I get the bucket like this:如果我得到这样的桶:

const storageBucket = admin.storage().bucket(storageBucketName);

VScode shows storageBucket has the type Bucket. VScode 显示 storageBucket 的类型为 Bucket。

But if I try to use this as parameter on a function:但是,如果我尝试将其用作 function 上的参数:

async function deleteOldBackup(storageBucket: Bucket) {

the type Bucket is underline in red as "cannot find Bucket". Bucket 类型在红色下划线为“找不到 Bucket”。

If I try to import the Bucket type like this:如果我尝试像这样导入 Bucket 类型:

import { Bucket, File } from "@google-cloud/storage"

I get the error that Bucket from admin is different than the Bucket in my deleteOldBackup functions.我收到来自管理员的 Bucket 与我的 deleteOldBackup 函数中的 Bucket 不同的错误。

How can I get propper Typescript type for google storage in my firebase cloud functions?如何在我的 firebase 云功能中获得适当的 Typescript 类型用于谷歌存储?

Update dependencies to the latest versions and it'll work.将依赖项更新到最新版本,它将起作用。

I've got these and the types are matching:我有这些并且类型匹配:

"dependencies": {
  "firebase-admin": "^9.3.0",
  "firebase-functions": "^3.11.0",
  "@google-cloud/storage": "^5.4.0"
}

The way that you have to import the Client Library is like this line:“const {Storage} = require('@google-cloud/storage');”您必须导入客户端库的方式如下:“const {Storage} = require('@google-cloud/storage');”

If you want, for example, list the objects in your bucket.例如,如果您想列出存储桶中的对象。 You have to use something like this code:您必须使用类似以下代码的内容:

/ /

**
 * TODO(developer): Uncomment the following line before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function listFiles() {
  // Lists files in the bucket
  const [files] = await storage.bucket(bucketName).getFiles();

  console.log('Files:');
  files.forEach(file => {
    console.log(file.name);
  });
}

listFiles().catch(console.error);

You can find more information about this example in the link and more information about the client library in the link .您可以在链接中找到有关此示例的更多信息,并在链接中找到有关客户端库的更多信息。

Hm... this works for me now:嗯......这现在对我有用:

import { File } from "@google-cloud/storage";

type GetFileHelperResult = {
  file?: File;
};

I'm using:我在用着:

"firebase": "8.2.6",
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.11.0",

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript 种类型,带有 Google Cloud Functions 和 Cloud Events - TypeScript types with Google Cloud Functions and Cloud Events 安装@ google-cloud / storage后如何解决Firebase Functions中的Typescript类型错误 - How to resolve Typescript type error in Firebase Functions after installing @google-cloud/storage 如何在 Firebase 云函数 Typescript 中定义 const - How to define const in Firebase Cloud Functions Typescript Firebase 云函数:保存 TypeScript Map() - 如何? - Firebase Cloud Functions: save TypeScript Map() - how? 在 firebase 云功能中具有 typescript 的 firebase-admin - firebase-admin with typescript in firebase cloud functions Typescript 中 Firebase 存储的云函数(存储类型上不存在属性“引用”) - Cloud Functions for Firebase Storage in Typescript (Property 'ref' does not exist on type Storage) 如何在打字稿/云函数中将地图推送到 firebase 数据库? - How to push a map to firebase database in typescript/cloud functions? 带Typescript的Firebase Cloud Functions,如何投射复杂的界面 - Firebase Cloud Functions with Typescript, how to cast a complex interface 如何在 firebase 云函数和 ZD18B8624A0F5F721DADDD7B82365FC562 之间共享 typescript class - How to share typescript class between firebase cloud functions and angular 如何将 Firebase 参数化配置与 Typescript 一起用于 Cloud Functions? - How can I use Firebase Parameterized configuration with Typescript for Cloud Functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM