Node.js用createBlockBlobFromLocalFile传大文件至Azure遇ESOCKETTIMEDOUT错误求助
Hey Emi, sorry to hear you're stuck with this timeout error when uploading files over 10MB to Azure Blob Storage! Let's walk through why this happens and how to fix it.
Why You're Seeing ESOCKETTIMEDOUT
The createBlockBlobFromLocalFile method you're using defaults to uploading the entire file in one go for smaller files. When dealing with larger files (over 10MB), the single upload request takes longer than the default socket timeout threshold, which triggers the ESOCKETTIMEDOUT error.
Solutions to Fix the Issue
1. Increase Timeout Settings
First, you can explicitly set longer timeout values in your options object to give the upload more time to complete. This is a quick fix, though not as robust as the next option for very large files.
Update your options with extended timeouts:
const options = { // Set a longer socket timeout (e.g., 5 minutes = 300000 ms) timeout: 300000, // For older versions of the Azure SDK, you might need to set this too requestOptions: { timeout: 300000 } };
2. Use Chunked (Block) Uploads (Recommended)
Azure Blob Storage supports block-level uploads, where large files are split into smaller chunks (blocks) and uploaded individually. This is more reliable for large files because each chunk has a shorter transfer time, reducing the chance of timeouts. It also lets you retry failed blocks without re-uploading the entire file.
Add a blockSize to your options to enable automatic chunking. Azure allows blocks up to 100MB; a common choice is 4-10MB per block:
const options = { // Split file into 4MB blocks blockSize: 4 * 1024 * 1024, // Extend timeout to account for multiple block uploads timeout: 300000, requestOptions: { timeout: 300000 } }; // Your existing upload code with the updated options blobService.createBlockBlobFromLocalFile(container, file.name, file.path, options, function (error, result, response) { try { if (error) { console.log('***** ERROR file NOT uploaded! ' + JSON.stringify(error)); reject(error); } if (!response.isSuccessful) { reject({error: 'Failed to create blob.'}); } console.log('***** createBlockBlobFromLocalFile FILE uploaded! '); resolve(); } catch (error) { console.log('***** ERROR file NOT uploaded! ' + JSON.stringify(error)); reject(error); } });
3. Update Your Azure SDK (Optional)
If you're using an older version of the azure-storage npm package, consider upgrading to the latest stable release. Newer versions often include fixes for timeout handling and performance improvements for large file uploads.
Additional Tips
- Ensure your network connection is stable—unreliable networks are more likely to cause timeouts even with chunked uploads.
- For files larger than a few hundred MB, you might want to implement progress tracking and retry logic for individual blocks, but the SDK should handle basic retries automatically.
内容的提问来源于stack exchange,提问作者emilio

