【问题标题】:Google Cloud Storage (setting up) NodeJS谷歌云存储(设置)NodeJS
【发布时间】:2020-09-27 12:36:32
【问题描述】:

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

https://cloud.google.com/appengine/docs/flexible/nodejs/using-cloud-storage

示例代码:

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.');
});


但是,当您阅读 https://github.com/GoogleCloudPlatform/google-cloud-node/blob/master/docs/authentication.md 时,它说要设置一个配置文件并执行以下操作

{
    "projectId": "grape-spaceship-123",
    "keyFilename": "./PROJECT-XXXXXX.json"
}

keyFilename 链接到 google 生成的 JSON。

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

注意:添加配置文件

const config = require('./config')

【问题讨论】:

    标签: node.js express google-cloud-platform google-cloud-storage


    【解决方案1】:

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

    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');
    

    关于这个的好视频:https://www.youtube.com/watch?v=pGSzMfKBV9Q

    【讨论】:

    • 感谢您的帮助。我知道这看起来很简单,但 Google Docs 并没有给出这样的例子 - 奇怪的是,你会认为像 Google 这样规模的公司会有更清晰的配置文档。
    • 问题是否需要是 -bucket 或 -storage 因为我使用了存储桶并且有效..
    猜你喜欢
    • 2015-05-09
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 2020-09-10
    • 2015-05-14
    • 2016-10-12
    • 1970-01-01
    相关资源
    最近更新 更多