可以通过以下步骤实现:
npm init -y
npm install @google-cloud/storage
download.js
,并在文件中引入所需的模块:const { Storage } = require('@google-cloud/storage');
const fs = require('fs');
const path = require('path');
const storage = new Storage();
async function downloadFolder(bucketName, folderName, destinationPath) {
const bucket = storage.bucket(bucketName);
const [files] = await bucket.getFiles({ prefix: folderName });
for (const file of files) {
const filePath = file.name;
const destFilePath = path.join(destinationPath, filePath);
if (file.isDirectory()) {
fs.mkdirSync(destFilePath, { recursive: true });
await downloadFolder(bucketName, filePath, destFilePath);
} else {
await file.download({ destination: destFilePath });
}
}
}
const bucketName = 'your-bucket-name';
const folderName = 'your-folder-name';
const destinationPath = 'your-destination-path';
downloadFolder(bucketName, folderName, destinationPath)
.then(() => {
console.log('Folder downloaded successfully.');
})
.catch((error) => {
console.error('Error downloading folder:', error);
});
在上述代码中,需要将your-bucket-name
替换为你的Google Cloud Storage存储桶名称,your-folder-name
替换为要下载的文件夹名称,your-destination-path
替换为下载文件夹的目标路径。
这样,使用Node.js就可以从Google Cloud Storage Bucket下载文件夹了。
推荐的腾讯云相关产品:腾讯云对象存储(COS)
请注意,以上答案仅供参考,具体实现可能需要根据实际情况进行调整。
领取专属 10元无门槛券
手把手带您无忧上云