我有一个数组entryIdsWithImages
,它们就像谷歌云存储中的文件夹名称一样,我必须从中下载所有图像,也就是说,我想从这些特定的GCP路径下载所有图像。因此,根据@google-cloud/storage
文档,我首先从这些路径中检索所有文件,然后将它们下载到一个临时目录中,稍后将其压缩。然而,我发现下载的图像大小为0B,而它们不在实际存储中。我哪里错了?
await Promise.all(
entryIdsWithImages.map(async (entryId) => {
const prefix = `images/${uid}/${entryId}`;
// download the images for the entry.
const [files] = await bucket.getFiles({
prefix,
});
files.forEach(async (file) => {
// file.name includes the whole path to the file, thus extract only the innermost path, which will be the name of the file
const fileName = file.name.slice(
file.name.lastIndexOf('/') + 1
);
const imgTempFilePath = path.join(imageDirectory, fileName);
try {
await file.download({
destination: `${imgTempFilePath}.jpg`,
});
} catch (e) {
console.log(
`Error downloading the image at ${prefix}/${fileName}: `,
e
);
}
});
})
)
我使用的版本是:"@google-cloud/storage": "^4.7.0"
。我试用了最新的版本,但得到了相同的结果。
云存储API代码对我来说是正确的。我希望这是你的问题:
destination: `${imgTempFilePath}.jpg`,
将文件类型更改为jpg
可能会导致看起来像0K的损坏文件。
这个代码非常适合我的bucket:
const downloadAll = async (bucket) => {
const imageDirectory = 'downloads';
const prefix = `READ`;
// download the images for the entry.
const [files] = await bucket.getFiles({
prefix,
});
files.forEach(async (file) => {
console.log(`file: ${file}`);
// file.name includes the whole path to the file, thus extract only the innermost path, which will be the name of the file
const fileName = file.name.slice(
file.name.lastIndexOf('/') + 1
);
const imgTempFilePath = path.join(imageDirectory, fileName);
try {
await file.download({
destination: `${imgTempFilePath}.jpg`,
});
} catch (e) {
console.log(
`Error downloading the image at ${prefix}/${fileName}: `,
e
);
}
});
}