You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

服务器端向Storage Bucket上传图片时触发EISDIR错误的排查求助

Fixing EISDIR Error When Uploading to Google Cloud Storage Bucket

Hey there, let's figure out why you're hitting that EISDIR: illegal operation on a directory, read error when uploading files to your GCS bucket. I’ve gone through your code and here are the most likely fixes to try:

1. Sanitize Filenames and Directory Paths

First, check if your file.originalname or directory parameter contains unexpected directory separators (/) or invalid characters. Sometimes user-uploaded filenames include paths (like folder/my-image.jpg), which can confuse GCS's virtual directory system.

Modify your code to clean up the filename:

// Replace slashes in filenames to avoid unintended virtual directories
const sanitizedFilename = file.originalname.replace(/[\/\\]/g, '-');
const filePath = directory ? `${directory}/${sanitizedFilename}` : sanitizedFilename;
const bucketFile = bucket.file(filePath);

Also, make sure the directory parameter doesn’t end with a trailing slash (like uploads/ instead of uploads)—while GCS usually handles this, it can sometimes cause path resolution glitches.

2. Verify the File Buffer is Valid

Even though you confirmed the file path is correct, double-check that file.buffer is actually a valid file buffer (not a reference to a directory). Add a quick sanity check before writing to the stream:

const fs = require('fs').promises;

// Add this inside your try block before creating the stream
const stats = await fs.stat(file.path);
if (!stats.isFile()) {
  reject(new Error(`Expected a file, got a directory at ${file.path}`));
  return;
}

This will catch edge cases where the file object might be pointing to a directory despite your initial checks.

3. Explicitly Set Content Type in the Write Stream

Missing or incorrect content type metadata can sometimes cause unexpected behavior with GCS streams. If your file object has a mimetype property (like when using Multer for uploads), add it to the stream options:

const blobStream = bucketFile.createWriteStream({
  metadata: {
    contentType: file.mimetype
  }
});

This ensures GCS treats the upload as a proper file rather than a directory-like object.

4. Check for Existing Virtual Directory Conflicts

In GCS, "directories" are just prefixes for objects. If there’s already an object with the exact name of your target directory (e.g., an object named my-directory/ with no filename), it might interfere with creating a new file under that prefix.

Run this debug code once to check for conflicts:

bucket.getFiles({ prefix: directory }).then(files => {
  console.log('Existing objects under directory:', files[0].map(f => f.name));
});

If you find an object matching your directory name (with a trailing slash), delete it and try uploading again.

Final Tip

If none of these fixes work, log the full file object and the filePath you’re passing to bucket.file()—sometimes unexpected values in these variables are the hidden root cause.


内容的提问来源于stack exchange,提问作者andrc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 14:07:47