【发布时间】:2018-03-07 09:26:19
【问题描述】:
我正在使用 Google Cloud Storage,并且有几个存储桶包含不公开共享的对象。下面的屏幕截图中的示例。然而,我能够在不使用 NodeJS 从本地服务器提供任何服务帐户密钥或身份验证令牌的情况下检索文件。
我无法通过 url 格式从浏览器访问文件(这很好): https://www.googleapis.com/storage/v1/b/mygcstestbucket/o/20180221164035-user-IMG_0737.JPG https://storage.googleapis.com/mygcstestbucket/20180221164035-user-IMG_0737.JPG
但是,当我尝试在没有凭据的情况下从 NodeJS 检索文件时,令人惊讶的是,它可以将文件下载到磁盘。我检查了 process.env 以确保没有 GOOGLE_AUTHENTICATION_CREDENTIALS 或任何 pem 密钥,甚至还在命令行上执行 gcloud auth revoke --all 以确保我已注销,但我仍然能够下载文件.这是否意味着我的 GCS 存储桶中的文件没有得到妥善保护?或者我以某种我不知道的方式使用 GCS API 对自己进行身份验证?
任何指导或方向将不胜感激!!
// Imports the Google Cloud client library
const Storage = require('@google-cloud/storage');
// Your Google Cloud Platform project ID
const projectId = [projectId];
// Creates a client
const storage = new Storage({
projectId: projectId
});
// The name for the new bucket
const bucketName = 'mygcstestbucket';
var userBucket = storage.bucket(bucketName);
app.get('/getFile', function(req, res){
let fileName = '20180221164035-user-IMG_0737.JPG';
var file = userBucket.file(fileName);
const options = {
destination: `${__dirname}/temp/${fileName}`
}
file.download(options, function(err){
if(err) return console.log('could not download due to error: ', err);
console.log('File completed');
res.json("File download completed");
})
})
【问题讨论】:
-
你能包括你在哪里运行这段代码吗?例如。这是来自 Cloud Shell、您拥有的计算机、GCE、GAE 等吗?
-
我的猜测是,要么这是在 GCE/容器引擎/应用引擎上运行,要么实际上已设置 GOOGLE_AUTHENTICATION_CREDENTIALS。
-
我在本地计算机上运行。我对我的所有 process.env 变量做了一个控制台日志。没有设置 GOOGLE_AUTHENTICATION_CREDENTIALS.. 我在哪里可以检查它是否存在?
标签: node.js google-cloud-storage