列出 node.js 应用中的 gcloud 存储桶文件
List gcloud bucket files in a node.js app
我想在我的 node.js 应用程序上显示我的 google 存储桶存储文件列表,我想知道这是否可行,或者我是否必须通过其他方式?谢谢
这里是我在Node.js10中为GAE标准写的一个例子,你可以使用和改编:
app.js
'use strict';
const express = require('express');
const {Storage} = require('@google-cloud/storage');
const app = express();
app.get('/', async(req, res) => {
let bucketName = '<BUCKET-NAME>'
// Initiate a Storage client
const storage = new Storage();
// List files in the bucket and store their name in the array called 'result'
const [files] = await storage.bucket(bucketName).getFiles();
let result = [];
files.forEach(file => {
result.push(file.name);
});
// Send the result upon a successful request
res.status(200).send(result).end();
});
// Start the server
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
module.exports = app;
package.json
{
"name": "nodejs-app",
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "^4.16.3",
"@google-cloud/storage": "^4.1.3"
}
}
app.yaml
runtime: nodejs10
为了获得包含 URLs 的列表而不是文件名,请更改 app.js 中的以下部分上面提供的示例:
const [files] = await storage.bucket(bucketName).getFiles();
let result = [];
files.forEach(file => {
result.push("https://storage.cloud.google.com/" + bucketName + "/" + file.name);
});
编辑:获取对象的元数据
您可以使用以下代码来获取对象的元数据:
const [files] = await storage.bucket(bucketName).getFiles();
let result = [];
for (const file of files) {
const [metadata] = await file.getMetadata();
result.push(metadata.size);
};
res.status(200).send(result).end();
我想在我的 node.js 应用程序上显示我的 google 存储桶存储文件列表,我想知道这是否可行,或者我是否必须通过其他方式?谢谢
这里是我在Node.js10中为GAE标准写的一个例子,你可以使用和改编:
app.js
'use strict';
const express = require('express');
const {Storage} = require('@google-cloud/storage');
const app = express();
app.get('/', async(req, res) => {
let bucketName = '<BUCKET-NAME>'
// Initiate a Storage client
const storage = new Storage();
// List files in the bucket and store their name in the array called 'result'
const [files] = await storage.bucket(bucketName).getFiles();
let result = [];
files.forEach(file => {
result.push(file.name);
});
// Send the result upon a successful request
res.status(200).send(result).end();
});
// Start the server
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
module.exports = app;
package.json
{
"name": "nodejs-app",
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "^4.16.3",
"@google-cloud/storage": "^4.1.3"
}
}
app.yaml
runtime: nodejs10
为了获得包含 URLs 的列表而不是文件名,请更改 app.js 中的以下部分上面提供的示例:
const [files] = await storage.bucket(bucketName).getFiles();
let result = [];
files.forEach(file => {
result.push("https://storage.cloud.google.com/" + bucketName + "/" + file.name);
});
编辑:获取对象的元数据
您可以使用以下代码来获取对象的元数据:
const [files] = await storage.bucket(bucketName).getFiles();
let result = [];
for (const file of files) {
const [metadata] = await file.getMetadata();
result.push(metadata.size);
};
res.status(200).send(result).end();