javascript - 如何使用 Elastic Beanstalk 创建 "tmp"目录?

标签 javascript node.js bash amazon-web-services amazon-elastic-beanstalk

我正在使用 Node.js,需要将文件保存到我的应用程序中的 tmp 目录。问题是 Elastic Beanstalk 没有将应用程序目录设置为可由应用程序写入。所以当我尝试创建临时目录时,我得到了这个错误

fs.js:653
  return binding.mkdir(pathModule._makeLong(path),
                 ^
Error: EACCES, permission denied '/var/app/tmp/'
    at Object.fs.mkdirSync (fs.js:653:18)
    at Promise.<anonymous> (/var/app/current/routes/auth.js:116:18)
    at Promise.<anonymous> (/var/app/current/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
    at Promise.emit (events.js:95:17)
    at Promise.emit (/var/app/current/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
    at Promise.fulfill (/var/app/current/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
    at /var/app/current/node_modules/mongoose/lib/query.js:1394:13
    at model.Document.init (/var/app/current/node_modules/mongoose/lib/document.js:250:11)
    at completeOne (/var/app/current/node_modules/mongoose/lib/query.js:1392:10)
    at Object.cb (/var/app/current/node_modules/mongoose/lib/query.js:1151:11)

我已经尝试了几件事,例如 .ebextensions/scripts/app-setup.sh 中的 app-setup.sh 脚本,看起来像这样

#!/bin/bash

# Check if this is the very first time that this script is running
if ([ ! -f /root/.not-a-new-instance.txt ]) then
    newEC2Instance=true
fi



# Get the directory of 'this' script
dirCurScript=$(dirname "${BASH_SOURCE[0]}")

# Fix the line endings of all files
find $dirCurScript/../../ -type f | xargs dos2unix -q -k

# Get the app configuration environment variables
source $dirCurScript/../../copy-to-slash/root/.elastic-beanstalk-app
export ELASTICBEANSTALK_APP_DIR="/$ELASTICBEANSTALK_APP_NAME"

appName="$ELASTICBEANSTALK_APP_NAME"
dirApp="$ELASTICBEANSTALK_APP_DIR"

dirAppExt="$ELASTICBEANSTALK_APP_DIR/.ebextensions"
dirAppTmp="$ELASTICBEANSTALK_APP_DIR/tmp"

dirAppData="$dirAppExt/data"
dirAppScript="$dirAppExt/scripts"


# Create tmp directory
mkdir -p $dirApp/tmp

# Set permissions
chmod 777 $dirApp
chmod 777 $dirApp/tmp

# Ensuring all the required environment settings after all the above setup
if ([ -f ~/.bash_profile ]) then
    source ~/.bash_profile
fi

# If new instance, now it is not new anymore
if ([ $newEC2Instance ]) then
    echo -n "" > /root/.not-a-new-instance.txt
fi


# Print the finish time of this script
echo $(date)


# Always successful exit so that beanstalk does not stop creating the environment
exit 0

以及在 .ebextensions 中创建一个名为 02_env.config 的文件,如下所示

# .ebextensions/99datadog.config
    container_commands:
        01mkdir:
            command: "mkdir /var/app/tmp"
        02chmod:
            command: "chmod 777 /var/app/tmp"

似乎都不起作用。如何在我的应用程序中创建可写的 tmp 目录?

最佳答案

我最近在 .NET 应用程序中遇到了同样的问题,该应用程序由于无法写入目录而失败,即使在我设置了权限之后也是如此。

我发现在整个 .ebextensions 过程完成后,最后一步是 Web 容器权限更新,它最终覆盖了我的 ebextensions 权限更改。

为了解决这个问题,我将目录移到了 Web 容器之外,并更新了应用程序以改为在那里写入。

在你的情况下,我建议 /tmp

关于javascript - 如何使用 Elastic Beanstalk 创建 "tmp"目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27980478/

相关文章:

javascript - 当我在 Angular 中加载动态组件时,jQuery 不工作

javascript - 固定顶部 div 和底部可滚动 div

javascript - 如何导出在 Node.js 的回调函数中更新的变量

node.js - 已安装的主机卷无法从 Azure Pipelines 中的主机写入

linux - Shopt命令搜索问题

javascript - 如何在 Sails JS 1.0 中设置元描述

javascript - 迭代 javascript 对象

node.js - 如何在mongo shell中的嵌套数组中添加数据

linux - 如何通过在 machineS 中运行它来从机器和机器上的 shell 脚本执行命令

bash - 如何使用 perl -e 将命令行选项传递给 Perl 程序?