forcePathStyle:true,// Must be enabled for RustFS compatibility
forcePathStyle:true,// Must be enabled for RustFS compatibility
maxAttempts:3,
maxAttempts:3,
...
@@ -30,11 +31,27 @@ try {
...
@@ -30,11 +31,27 @@ try {
connectionTimeout:10000,// Increased from 3000 to 10000 ms
connectionTimeout:10000,// Increased from 3000 to 10000 ms
socketTimeout:30000,// Increased from 5000 to 30000 ms
socketTimeout:30000,// Increased from 5000 to 30000 ms
}),
}),
});
};
}catch(error){
// 合并配置
constfinalConfig={
...defaultConfig,
...config,
credentials:{
...defaultConfig.credentials,
...(config.credentials||{})
}
};
returnnewS3Client(finalConfig);
}catch(error){
console.error("Failed to initialize S3 client:",error.message);
console.error("Failed to initialize S3 client:",error.message);
s3=null;
returnnull;
}
}
};
// 默认客户端(向后兼容)
lets3=createS3Client();
/**
/**
* Downloads a file from the specified S3 bucket and key, and writes it to the local file system.
* Downloads a file from the specified S3 bucket and key, and writes it to the local file system.
...
@@ -281,7 +298,29 @@ async function downloadS3Folder(bucketName, folderPrefix, localDestination) {
...
@@ -281,7 +298,29 @@ async function downloadS3Folder(bucketName, folderPrefix, localDestination) {
console.log(`\n完成!共下载 ${downloadedCount} 个文件。`);
console.log(`\n完成!共下载 ${downloadedCount} 个文件。`);
}
}
module.exports={s3,downloadFileFromS3,uploadFileToS3,uploadFolderToS3,downloadS3Folder,deleteS3Folder};// Export the S3 client, download, and upload functions
/**
* Reconfigure the global S3 client with new settings
* @param {Object} config - Configuration object
* @returns {Object} The new S3 client
*/
constreconfigureS3Client=(config={})=>{
constnewClient=createS3Client(config);
if(newClient){
s3=newClient;
}
returns3;
};
module.exports={
s3,
createS3Client,
reconfigureS3Client,
downloadFileFromS3,
uploadFileToS3,
uploadFolderToS3,
downloadS3Folder,
deleteS3Folder
};// Export the S3 client, download, and upload functions
// Example usage - commented out to prevent auto-execution:
// Example usage - commented out to prevent auto-execution: