[英]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.