如何解决如何使用NodeJs客户端而不是存储对象获取谷歌云存储的公共网址列表
运行 (new Storage()).bucket('my-bucket-name').getFiles()
时,我得到一个带有 this structure 的对象列表。所有项目都设置为公开,我宁愿不处理对象以通过“手工”(https://storage.cloud.google.com/[object.Metadata.bucket]/[object.Metadata.name]
)拼凑公共网址,我想知道 GCP 的 NodeJs 客户端是否提供这样的东西。>
谢谢!
解决方法
正如您在帖子中提到的,没有直接的方法可以通过 Google 现有的客户端库来做到这一点。有一些对象可以让您直接获取 URL,但并非所有对象都可以。
因此,对代码中的 URL 进行拼接会更安全。正如您在 Google 文档中通过 this document 所提到的,您可以使用 URL 模式 http(s)://storage.googleapis.com/[bucket]/[object]
来快速构建 URL。
给定API的响应,可以通过一个小循环比如
function main(bucketName = 'my-bucket') {
// The ID of your GCS bucket
const bucketName = 'your-unique-bucket-name';
// The string for the URL
const url = 'https://storage.googleapis.com/';
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
async function listFiles() {
// Lists files in the bucket
const [files] = await storage.bucket(bucketName).getFiles();
console.log('URLs:');
files.forEach(file => {
console.log(url.concat(bucketName,'/',file.name));
});
}
listFiles().catch(console.error);
}
这是改编自 GCPs GitHub
上列出文件的示例代码版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。