如何在 Node.js 中使用 gs:// URI 从 Google Cloud Storage 下载对象?

问题描述 投票:0回答:2

我有 Google Cloud Storage 中对象的

gs://
字符串 URI(有关 gs:// URI 含义的解释,请参阅
这个问题
)。 如何使用 Node.js 下载这些对象? docs 仅包含带有存储桶名称和文件路径的示例:

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const myBucket = storage.bucket('my-bucket');

const file = myBucket.file('my-file');

我可以直接从

file
URI(作为字符串传递)创建
gs://
,还是需要手动将 URI 解析为存储桶名称和文件路径?

javascript google-cloud-storage
2个回答
0
投票

您正在寻找的是使用对象 URI 从云存储获取文件,即

gs://bucket_name/file_name
。答案是不可能完成您正在寻找的任务。此外,NodeJS 中云存储的官方客户端库没有提供任何此类方法来执行此操作。因此,您必须解析对象 URI 并首先使用存储客户端获取存储桶,然后使用存储桶获取对象,如此处或在此github存储库中所述。


0
投票

@google-cloud/storage
v7.10.0 (2024-04-15) 开始,现在可以从 URL 创建文件对象。

文档

示例:

import { Storage, File } from '@google-cloud/storage';

const storage = new Storage();

const uri = 'gs://example-bucket/example-file';

const file = File.from(uri, storage).download();
© www.soinside.com 2019 - 2024. All rights reserved.