简体   繁体   English

Docker 中的 Apollo 服务器上传文件失败

[英]Apollo Server Upload File failes in Docker

UPD After several hours I've got a bright idea of trying another node version in docker. UPD几个小时后,我有了在 docker 中尝试另一个节点版本的好主意。 Alpine gave that error while 10.15 works fine. Alpine 给出了这个错误,而 10.15 工作正常。

I have the following function in my resolver to upload the file through apollo graphql我的解析器中有以下 function 通过 apollo graphql 上传文件

return new Promise(function(resolve, reject) {
const { createReadStream, filename, mimetype } = file
if (!(mimetype === 'image/png' || mimetype === 'image/jpeg' || mimetype === 'image/jpg')){
  const error = new Error('Forbidden file type ', mimetype)
  error.code = 415
  reject(error)
}

const savedFileName = uuidv4()+path.extname(filename)
const savedFile = path.join(__dirname, '..', 'images', subDir, savedFileName)
const imagePath = path.join('/images', subDir, savedFileName).replace(/\\/g, "/")

const readStream  = createReadStream()

const writeStream = fs.createWriteStream(savedFile, { flags : 'w' });
readStream.pipe(writeStream)

readStream.on('error', () => console.log('reading error'))
writeStream.on('finish', () => resolve({file: savedFile, imageUrl: imagePath}))
writeStream.on('error', (error) => {

  //const err = new Error('Error writing file')
  //error.code = 422
  reject(error)
})

It works fine in my development enviroment in Windows.它在我的 Windows 开发环境中运行良好。 I tried to create docker containers for my project and this snippet fails with in error:我尝试为我的项目创建 docker 容器,但此代码段失败并出现错误:

internal/fs/streams.js:120
node_1      | function _openReadFs(stream) {
node_1      |                     ^
node_1      |
node_1      | RangeError: Maximum call stack size exceeded
node_1      |     at _openReadFs (internal/fs/streams.js:120:21)
node_1      |     at ReadStream.<anonymous> (internal/fs/streams.js:116:3)
node_1      |     at ReadStream.deprecated [as open] (internal/util.js:70:15)
node_1      |     at ReadStream.open (/app/node_modules/fs-capacitor/lib/index.js:90:11)
node_1      |     at _openReadFs (internal/fs/streams.js:123:12)
node_1      |     at ReadStream.<anonymous> (internal/fs/streams.js:116:3)
node_1      |     at ReadStream.deprecated [as open] (internal/util.js:70:15)
node_1      |     at ReadStream.open (/app/node_modules/fs-capacitor/lib/index.js:90:11)
node_1      |     at _openReadFs (internal/fs/streams.js:123:12)
node_1      |     at ReadStream.<anonymous> (internal/fs/streams.js:116:3)
node_1      |     at ReadStream.deprecated [as open] (internal/util.js:70:15)
node_1      |     at ReadStream.open (/app/node_modules/fs-capacitor/lib/index.js:90:11)
node_1      |     at _openReadFs (internal/fs/streams.js:123:12)
node_1      |     at ReadStream.<anonymous> (internal/fs/streams.js:116:3)
node_1      |     at ReadStream.deprecated [as open] (internal/util.js:70:15)
node_1      |     at ReadStream.open (/app/node_modules/fs-capacitor/lib/index.js:90:11)
node_1      |     at _openReadFs (internal/fs/streams.js:123:12)
node_1      |     at ReadStream.<anonymous> (internal/fs/streams.js:116:3)
node_1      |     at ReadStream.deprecated [as open] (internal/util.js:70:15)
node_1      |     at ReadStream.open (/app/node_modules/fs-capacitor/lib/index.js:90:11)
node_1      |     at _openReadFs (internal/fs/streams.js:123:12)
node_1      |     at ReadStream.<anonymous> (internal/fs/streams.js:116:3)
node_1      | [nodemon] app crashed - waiting for file changes before starting...

I'm new to docker and especcialy to linux.我是 docker 的新手,尤其是 linux 的新手。 Maybe there is a difference how reading and piping processed in linux.在 linux 中读取和管道处理的方式可能有所不同。 But I don't understand where to look.但我不明白在哪里看。 Is it a Apollo problem or docker problem or my linux poor knowledge problem.是阿波罗问题还是 docker 问题还是我的 linux 知识贫乏问题。 Will be grateful for any advice.将不胜感激任何建议。

This is due to node deprecating some internal stream APIs, see DEP0135: WriteStream.open() and ReadStream.open() are internal .这是由于节点弃用了一些内部 stream API,请参阅DEP0135:WriteStream.open() 和 ReadStream.open() 是内部的

WriteStream.open() and ReadStream.open() are undocumented internal APIs that do not make sense to use in userland. WriteStream.open()ReadStream.open()是未记录的内部 API,在用户空间中使用没有意义。 File streams should always be opened through their corresponding factory methods fs.createWriteStream() and fs.createReadStream() ) or by passing a file descriptor in options.文件流应始终通过其相应的工厂方法fs.createWriteStream()fs.createReadStream() ) 或通过在选项中传递文件描述符来打开。

There is an issue filed to try and get this all sorted: Upgrade graphql-upload to 9.0 to support Node.JS 13 .有一个问题试图解决这个问题: Upgrade graphql-upload to 9.0 to support Node.JS 13 The crux is that fs-capacitor has been updated to address this, but everything that depends on it needs to update accordingly.关键是fs-capacitor已更新以解决此问题,但依赖它的所有内容都需要相应更新。

For now, you'll need to use node 12 or at least something prior to 13.现在,您需要使用节点 12 或至少 13 之前的东西。

I resolved my issue by using the more current version of node by explicitly defining it in my 'Dockerfile', my local environment is running v11.14.0, you can check yours via我通过在我的“Dockerfile”中明确定义节点来解决我的问题,我的本地环境正在运行 v11.14.0,您可以通过

node -v

Once I modified my 'Dockerfile' to use一旦我修改了我的“Dockerfile”以使用

FROM node:11

in my opening line it worked first try after that.在我的开场白中,它在那之后第一次尝试。

Hope this helps you希望这可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM