【发布时间】:2021-01-20 06:03:52
【问题描述】:
我在 Google Cloud Storage 上有多个文件,分别命名为 0.jpg、1.jpg、2.jpg 等。我想获取每个文件的元数据,而无需单独设置文件名。然后,想要将这些元数据信息发送到 React 应用程序。在 React 应用程序中,当单击图像时,弹出窗口会显示该单击图像的元数据信息。
对于一个文件,我使用了以下代码:
const express = require("express");
const cors = require("cors");
// Imports the Google Cloud client library
const { Storage } = require("@google-cloud/storage");
const bucketName = "bitirme_1";
const filename = "detected/0.jpg";
const storage = new Storage();
const app = express();
app.get("/api/metadata", cors(), async (req, res, next) => {
try {
// Gets the metadata for the file
const [metadata] = await storage
.bucket(bucketName)
.file(filename)
.getMetadata();
const metadatas = [
{id: 0, name: `Date: ${metadata.updated.substring(0,10)}, Time: ${metadata.updated.substring(11,19)}`},
{id: 1, name: metadata.contentType}
];
res.json(metadatas);
} catch (e) {
next(e);
}
});
const port = 5000;
app.listen(port, () => console.log(`Server started on port ${port}`));
我首先设置了存储桶名称。然后,设置文件名数组为(89为文件数):
const filename = Array(89).fill(1).map((_, i) => ('detected/' + i + '.jpg'));
这些文件位于检测到的文件夹中。当我尝试这个时,它给了我这个错误:
错误:没有这样的对象:bitirme_1/detected/0.jpg、detected/1.jpg、detected/2.jpg、detected/3.jpg、detected/4.jpg、detected/5.jpg、detected/6 .jpg, ....
如何解决获取多个文件的元数据问题?
另外,我想获取存储桶(或检测到的文件夹)中的文件数。我搜索了 API,但找不到任何东西。我不想输入文件总数为 89,想从 API 中获取。
【问题讨论】:
-
您是否使用库来获取元数据?如果有,是哪一个?您可以edit 您的帖子包含详细信息,包括获取元数据的代码。
-
我正在使用 google-cloud/storage api 来获取元数据信息。我只用一个文件试过这个。我为它添加了代码
-
您能否编辑您的问题以包含您使用库的 sn-p 和
filename? -
我在一分钟前的问题中添加了它
-
哎呀!看起来我的客户缓存了旧问题。谢谢!
标签: node.js reactjs google-cloud-platform metadata