简体   繁体   中英

Node.js Google Cloud Storage Get Multiple Files' Metadata

I have several files on Google Cloud Storage that are named as 0.jpg, 1.jpg, 2.jpg, etc. I want to get the metadata for each file without setting file names separately. Then, want to send these metadata information to React application. In React application, when one clicks on an image, the popup displays the metadata information for this clicked image.

For only one file, I used the following code:

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}`));

I first set the bucket name. Then, set filename array as (89 is the number of files):

const filename = Array(89).fill(1).map((_, i) => ('detected/' + i + '.jpg'));

These files are in detected folder. When I try this, it gives me this error:

Error: No such object: bitirme_1/detected/0.jpg, detected/1.jpg, detected/2.jpg, detected/3.jpg, detected/4.jpg,detected/5.jpg,detected/6.jpg, ....

How can I solve the getting multiple files' metadata issue?

Also, I want to get the number of files in a bucket (or, in the detected folder). I searched the API but cannot found anything. I do not want to enter the total number of files as 89, want to get it from the API.

I found the solution for finding the number of files in a bucket or a folder in a bucket. This is the solution:

const [files] = await storage.bucket(bucketName).getFiles();

const fileStrings = files.map(file => file.name);

const fileSliced = fileStrings.map(el => el.slice(9, 11));

for (i = 0; i < fileSliced.length; i++) {
  if (fileSliced[i].includes('.')) {
    fileSliced[i] = fileSliced[i].slice(0, 1);
  }
}

const fileNumbers = fileSliced.map(function(item) {
  return parseInt(item, 10);
});

const numOfFiles = Math.max(...fileNumbers) + 1;

console.log(numOfFiles);

First, I got the all files with file names in a string array. In my case, file names are detected/0.jpg, detected/1.jpg, detected/2.jpg, etc. I just only want to the number part of the file name; hence, I sliced the string array starting from 9th index up to 11th index(not included). As a result, I got only the numbers except one digit numbers.

To handle one digit case, which have '.' at the end of the sliced name, I also removed '.' from these one digit file names.

As a result, I got ['0', '1', '2', '3', ...]. Next, I convert this string array to number array using parseInt function. Finally, to get the number of files, I got the maximum of the array and add 1 to this number.

I have an image detail component that includes location of sender ip, download option and exiting the popup page. This detail popup page opens at /#i where i is the name of image file, such as 1.jpg, 2.jpg. So, for example when I click the first image, the popup page opens at /#1. In this popup page, I want to get metadata information for the opened image. But, I could not find a solution for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM