NodeJS gcloud - 使用公共读取属性/自定义缓存过期上传到谷歌存储

时间:2014-12-02 11:39:54

标签: node.js google-cloud-storage gcloud gcloud-node

我正在尝试使用gcloud库(NodeJS)上传到Google存储空间。

我需要启用public-read属性并将cache-expiration设置为5分钟。

我正在使用这个(简化的)代码:

storage = gcloud.storage({options}
bucker = storage.bucket('name');
fs.createReadStream(srcPath).pipe(bucket.file(targetFile).createWriteStream()).on('error', function(err) 

如何设置适当的ACL /缓存过期? (我发现了这个,但不知道该怎么做: https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.11.0/storage?method=acl

感谢您的帮助

3 个答案:

答案 0 :(得分:4)

您可以按照here

说明设置预定义的ACL
yourBucket.acl.default.add({
  entity: "allUsers",
  role: gcloud.storage.acl.READER_ROLE
}, function (err) {})

关于缓存控制,我不相信您可以将其设置为默认值,但您可以在上传文件时进行设置:

var opts = { metadata: { cacheControl: "public, max-age=300" } }
bucket.file(targetFile).createWriteStream(opts)

参考:https://cloud.google.com/storage/docs/reference-headers#cachecontrol

答案 1 :(得分:3)

Api改变了,使用:

var gcloud = require('gcloud')({
  projectId: 'your_id',
  keyFilename: 'your_path'
});

var storage = gcloud.storage();
var bucket = storage.bucket('bucket_name');

bucket.acl.default.add({
    entity: 'allUsers',
    role: storage.acl.READER_ROLE
}, function(err) {});

要将整个存储桶公开,您还可以使用:

bucket.makePublic

来源:https://github.com/GoogleCloudPlatform/gcloud-node/blob/v0.16.0/lib/storage/bucket.js#L607

或者只是文件:

var bucketFile = bucket.file(filename);

// If you upload a new file, make sure to do this 
// in the callback of upload success otherwise it will throw a 404 error

bucketFile.makePublic(function(err) {});

来源:https://github.com/GoogleCloudPlatform/gcloud-node/blob/v0.16.0/lib/storage/file.js#L1241(链接可能会更改,请在源代码中查找makePublic。)

或者:

bucketFile.acl.add({
    scope: 'allUsers',
    role: storage.acl.READER_ROLE
}, function(err, aclObject) {});

这是详细版本。

来源:https://github.com/GoogleCloudPlatform/gcloud-node/blob/v0.16.0/lib/storage/file.js#L116

答案 2 :(得分:1)

斯蒂芬的评论是准确的,但它并不适合我,因为价值没有得到确定。经过一些试验和错误后,请调出cacheControl(无破折号)以使其正常工作。在撰写本文时,无需在任何需要采用此格式的文档中记录。我假设其他字段会有同样的问题。

var opts = { metadata: { "cacheControl": "public, max-age=300" } }
bucket.file(targetFile).createWriteStream(opts)