22

I'm trying to deploy my api to Google Cloud Functions, and I'm getting this:

EROFS: read-only file system, mkdir '/user_code/uploads'

⚠  functions[post]: Deployment error. Function load error: 
    Code in file index.js can't be loaded. Is there a syntax error in your code? 
    Detailed stack trace: Error: EROFS: read-only file system, mkdir '/user_code/uploads'
    at Error (native)
    at Object.fs.mkdirSync (fs.js:932:18)
    at Function.sync (/user_code/node_modules/multer/node_modules/mkdirp/index.js:71:13)
    at new DiskStorage (/user_code/node_modules/multer/storage/disk.js:21:12)
    at module.exports (/user_code/node_modules/multer/storage/disk.js:65:10)
    at new Multer (/user_code/node_modules/multer/index.js:15:20)
    at multer (/user_code/node_modules/multer/index.js:95:12)
    at Object.<anonymous> (/user_code/api/user.js:105:46)
    at Module._compile (module.js:577:32)
    at Object.Module._extensions..js (module.js:586:10)
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Paulo Brito
  • 453
  • 1
  • 5
  • 15
  • It seams something to do with multer initialization. But the fact the the file system is readonly is still an issue. – Paulo Brito Jul 12 '18 at 14:43

3 Answers3

42

Everything in the Cloud Functions runtime is read-only except for os.tmpdir() (which is likely going to be /tmp, but you shouldn't assume that). If you have any code (in api/user.js for example) that attempt to write anywhere else, it'll error.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • That's exactly what happened. At multer initialization, that I copied from the docs, there's a /uploads folder. Worked before because that folder was included in the deploy. Thanks. – Paulo Brito Jul 12 '18 at 21:19
  • @Doug Stevenson, Does this mean there is no workaround to enable write access to Cloud Storage from Cloud Functions? I'm generating mp3s file from `Text-To-Audio` and was planning to save via Cloud Functions to offload client logic. Worst case scenario I can handle this on the client. – AdamHurwitz Dec 05 '18 at 21:26
  • 1
    @AdamHurwitz Writing to Cloud Storage is in no way related to writing to the local filesystem in Cloud Functions. You're perfectly able to write to Cloud Functions using its SDK, and there's lots of sample code out there that does it. – Doug Stevenson Dec 05 '18 at 21:28
  • When a user uploads a file that needs to be written to Storage later, is writing to /tmp storage recommended? – Akas Antony Sep 12 '19 at 21:22
  • 1
    @AkasAntony Do whatever you find most convenient. But if you can write directly to storage without writing to tmp, that's going to be more memory efficient. – Doug Stevenson Sep 12 '19 at 21:25
  • @DougStevenson - I tried to write to a file and then tried to upload to a GCS bucket from the Cloud Function code. It failed until I tried to write to `/tmp` path. Can you please direct to a sample code that you have mentioned earlier. – Chandan Patra Dec 21 '21 at 04:34
  • Thanks a ton. After hours of online search, this solution worked! – Naga Hemanth Nov 17 '22 at 01:41
15

Same issue for python, but putting this for clarity. My error -

File "/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py", line 753, in download_to_filename
    with open(filename, "wb") as file_obj:
OSError: [Errno 30] Read-only file system: 'testFile.zip'

Get the temp directory as follows (usually /tmp):

import tempfile
tmpdir = tempfile.gettempdir()

Googles documentation can be found here.

While Cloud Storage is the recommended solution for reading and writing files in App Engine, if your app only needs to write temporary files, you can use standard Python 3.7 methods to write files to a directory named /tmp.

All files in this directory are stored in the instance's RAM, therefore writing to /tmp takes up system memory. In addition, files in the /tmp directory are only available to the app instance that created the files. When the instance is deleted, the temporary files are deleted.

xjcl
  • 12,848
  • 6
  • 67
  • 89
Connor
  • 393
  • 2
  • 9
1

Gen1 cloud functions are read-only systems. However, Gen2 cloud functions aren't. I'd recommend changing your function to Gen2

(be careful, this might interfere with another config as gen 2 can be considered as a cloud run)

Xab Ion
  • 1,105
  • 1
  • 11
  • 20
khemi1998
  • 11
  • 1