当前位置: 首页 > 知识库问答 >
问题:

Google云存储(设置)NodeJS

沃驰
2023-03-14

因此,我正在查看来自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')

共有1个答案

蓟雪峰
2023-03-14

我为我创建了存储和它的工作:

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

 类似资料:
  • 我是谷歌云平台的新手,我正在学习本教程 https://cloud.google.com/docs/authentication/getting-started#auth-cloud-implicit-nodeJS 在我使用Google Cloud设置环境变量后,这一行会导致问题 当我注释掉这行时,它会正常工作,否则它会抛出一个错误

  • 我的网站文件托管在Google Cloud Storage,每次我更新一个文件(比如。png或。xml)时,我总是要等一段时间才能看到新文件出现在我的页面上。 有没有办法覆盖单个对象的默认缓存设置(max-age=3600)?尝试用“no store”、“no cache”、“max-age=0”编辑“cache-control”元数据,但它们似乎都不起作用,仍然在响应头中获得“cache-con

  • 谷歌云存储中的文件更改需要多长时间才能传播? 我遇到了一个非常令人沮丧的问题,我改变了一个文件的内容,并通过gsutil重新上传,但是这个改变几个小时后才显示出来。有没有办法强制一个改变的文件立即传播所有内容? 如果我查看谷歌云存储控制台中的文件,它会看到新文件,但如果我点击公共网址,它是旧版本,在某些情况下,是2个版本前的版本。 有没有我没有设置的标题? 编辑: 我尝试了,但它没有帮助,但也许旧

  • 我在我创建的谷歌云存储上有一个存储桶。我想测试一些内置ACL,如public read、public read write等。但是,一旦我使用gsutil setacl命令更改了ACL,例如:

  • 我注意到谷歌云存储没有提供任何移动SDK,所以我们可以用来与存储通信,那么我们需要创建自定义API来进行通信吗?我看到有一种方法可以在现有的Firebase项目中添加额外的桶,这会在Firebase中起作用吗?或者我们有没有一种方法可以将Firebase bucket添加到谷歌云存储中?

  • 我遇到了这个问题,找不到一个简单的php示例,可以在上传到google云存储时设置对象缓存控制。我知道它是一个关于对象的setMetadata,但我不知道如何做到这一点。使用gsutil并不能削减它,因为它对于web应用程序来说不是动态的。 到目前为止,这是我所拥有的,但setMetadata行抛出错误。谁能帮忙纠正那条线吗?请注意,授权令牌在以下内容之前已经获得