Node.js 谷歌云存储(设置)节点

Node.js 谷歌云存储(设置)节点,node.js,express,google-cloud-platform,google-cloud-storage,Node.js,Express,Google Cloud Platform,Google Cloud Storage,因此,我正在查看来自Google的示例代码,但我不知道如何激活配置文件 示例代码: const {format} = require('util'); const express = require('express'); const Multer = require('multer'); const bodyParser = require('body-parser'); // By default, the client will authenticate using the servi

因此,我正在查看来自Google的示例代码,但我不知道如何激活配置文件

示例代码:

const {format} = require('util');
const express = require('express');
const Multer = require('multer');
const bodyParser = require('body-parser');

// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GOOGLE_CLOUD_PROJECT environment variable. See
// https://github.com/GoogleCloudPlatform/google-cloud-node/blob/master/docs/authentication.md
// These environment variables are set automatically on Google App Engine
const {Storage} = require('@google-cloud/storage');

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

const app = express();
app.set('view engine', 'pug');
app.use(bodyParser.json());

// Multer is required to process file uploads and make them available via
// req.files.
const multer = Multer({
  storage: Multer.memoryStorage(),
  limits: {
    fileSize: 5 * 1024 * 1024, // no larger than 5mb, you can change as needed.
  },
});

// A bucket is a container for objects (files).
const bucket = storage.bucket(process.env.GCLOUD_STORAGE_BUCKET);

// Display a form for uploading files.
app.get('/', (req, res) => {
  res.render('form.pug');
});

// Process the file upload and upload to Google Cloud Storage.
app.post('/upload', multer.single('file'), (req, res, next) => {
  if (!req.file) {
    res.status(400).send('No file uploaded.');
    return;
  }

  // Create a new blob in the bucket and upload the file data.
  const blob = bucket.file(req.file.originalname);
  const blobStream = blob.createWriteStream();

  blobStream.on('error', (err) => {
    next(err);
  });

  blobStream.on('finish', () => {
    // The public URL can be used to directly access the file via HTTP.
    const publicUrl = format(
      `https://storage.googleapis.com/${bucket.name}/${blob.name}`
    );
    res.status(200).send(publicUrl);
  });

  blobStream.end(req.file.buffer);
});

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`App listening on port ${PORT}`);
  console.log('Press Ctrl+C to quit.');
});


但是,当您阅读它时,它说要设置一个配置文件并执行以下操作

{
    "projectId": "grape-spaceship-123",
    "keyFilename": "./PROJECT-XXXXXX.json"
}
keyFilename链接到google生成的JSON

但是现在我如何告诉上面的示例代码使用它呢

注意:添加配置文件

const config = require('./config')

我为自己创建了存储及其工作:

import { Storage }  from '@google-cloud/storage';//may be you need to use require()
import * as path from 'path';

const storage = new Storage({
  keyFilename: path.join(__dirname, '../********************.json'),
  projectId: '***********Id'
})

const fileBucket = storage.bucket('***********-storage');

这方面的视频很好:

谢谢你的帮助。我知道这看起来很简单,但谷歌文档没有给出这样的例子——奇怪的是,你会认为像谷歌这样规模的公司会有更清晰的关于配置的文档。问题是,当我使用bucket时,它需要是-bucket还是-storage,而这是有效的。。