anthi:_anti_hair_thinning_hair_spray.png 这是 s3 中的文件名,当使用 s3 客户端下载时,它会保存名为 anthi 的文件,文件类型为 0kb,文件类型也显示文件,在 s3 中的任何位置,文件大小为 100kb,文件类型为 png
我正在使用 fs 以相同的名称在本地保存文件,请帮助我
const objects = await s3.listObjectsV2(listParams).promise();
for (const object of objects.Contents) {
if (object.Size >= MIN_FILE_SIZE_IN_BYTES) {
const fileKey = object.Key;
const fileExtension = path.extname(fileKey).toLowerCase();
if (allowedFileExtensions.includes(fileExtension)) {
const localFilePath = path.join(localDownloadFolder, path.basename(fileKey));
const data = await s3.getObject({ Bucket: bucketName, Key: fileKey }).promise();
fs.writeFileSync(localFilePath, data.Body);
// console.log(`File downloaded to: ${localFilePath}`);
}
}
}
我将非常感谢任何建议或意见。
我尝试从 asw s3 存储桶下载文件
无法使用列下载文件名(anthi:_anti_hair_thinning_hair_spray.png)
使用 fs 在文件夹中写入文件
我该如何解决这个问题?
您遇到的问题可能是由于文件名中的冒号 (:) 造成的。你可以试试这个:
const objects = await s3.listObjectsV2(listParams).promise();
for (const object of objects.Contents) {
if (object.Size >= MIN_FILE_SIZE_IN_BYTES) {
const fileKey = object.Key;
const fileExtension = path.extname(fileKey).toLowerCase();
if (allowedFileExtensions.includes(fileExtension)) {
// Replace colon and other invalid characters in the filename
const sanitizedFileName = path.basename(fileKey).replace(/[:]/g, '_');
const localFilePath = path.join(localDownloadFolder, sanitizedFileName);
const data = await s3.getObject({ Bucket: bucketName, Key: fileKey }).promise();
fs.writeFileSync(localFilePath, data.Body);
// console.log(`File downloaded to: ${localFilePath}`);
}
}
}