使用PHP从Elastic Beanstalk连接到EFS

问题描述 投票:0回答:1

像往常一样,操作越简单,AWS文档就会让我感到困惑。我想用PHP来编写和读取存储在EFS上的文件。我对使用AWS PHP库很有经验,我只需要正确的代码来促进沟通。这是我的设置:

  • 我的默认VPC上有1个EFS文件系统。
  • 1将Elasticbeanstalk环境中的“storage-efs-mountfilesystem.config”添加到最新应用程序代码的.ebextensions目录中并初始化而没有错误(这使我认为它正在连接)。 EB的安全组将添加到EFS安全组。 AWS PHP SDK也与应用程序一起存在
  • 应用程序中的1个PHP文件

现在在文件中我通常以这种方式连接到其他服务:

use Aws\S3\S3Client;
$options = [
    'region'            => 'us-east-1',
    'version'           => '2006-03-01',
    'signature_version' => 'v4',
    'credentials' => [
        'key' => 'key#',
        'secret' => 'secret#'
    ] 
];
$GLOBALS['s3Client'] = new S3Client($options);
$writeFile = $GLOBALS['s3Client']->putObject(array(...

我认为这应该与EFS相同。作为第一个猜测,我试过:

$efsOptions = [
    'region'            => 'us-east-1',
    'version'           => 'latest',
    'credentials' => [
        'key' => 'key#',
        'secret' => 'secret#'
    ] 
];
use Aws\Efs\EfsClient;
$efsClient = new EfsClient($efsOptions);
$result = $efsClient->describeFileSystems(array(
    'FileSystemId' => 'fs-#'
));

但得到一个错误:

Fatal error: Uncaught exception 'Aws\Efs\Exception\EfsException' with message 'Error executing "DescribeFileSystems" on "https://elasticfilesystem.us-east-1.amazonaws.com/2015-02-01/file-systems?FileSystemId=f#"; AWS HTTP error: Client error: `GET https://elasticfilesystem.us-east-1.amazonaws.com/2015-02-01/file-systems?FileSystemId=f#` resulted in a `403 Forbidden`

那么这样做的正确方法是什么? (伪):

use Aws\efs\EfsClient;
$options = [
    'What keys need to be here' => 'paramter',
    'credentials' => [
        'key' => 'key#',
        'secret' => 'sevret#'
    ] 
];
$efsClient = new EfsClient($options);
$dir = "/test/";
$makeDir = $efsClient -> mkdir($dir);
$scanDir = $efsClient -> scandir($dir);
print_r($scanDir);
**/test/

我从不使用控制台或连接到服务器来安装软件包,因此请将此限制为允许我在PHP文件或有限的“一次”控制台配置中执行所有操作的答案。关键是我需要PHP脚本在EFS上创建和读取文件,并与其他EB环境共享。到目前为止我已审核或使用的AWS文档:

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/services-efs.html https://github.com/awsdocs/elastic-beanstalk-samples/blob/master/configuration-files/aws-provided/instance-configuration/storage-efs-mountfilesystem.config https://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.Efs.EfsClient.html

php amazon-web-services elastic-beanstalk mkdir aws-elasticfilesystem
1个回答
1
投票

为什么不将EFS作为共享安装在运行代码的EC2实例上。然后EFS只是PHP应用程序可以写入的常规路径。这就是我们在Elastic Beanstalk Web服务器上执行的操作,用于持久保存用户文件上载等。

在部署代码时,您必须使用AWS EBExtensions机制从EB / EC2实例设置连接。这样做的示例配置代码段是:

# config.yml file:

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/my_config_job.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      # Load the EFS path from the EB environment settings
      # (The variables are set in the "Configuration" section in the AWS
      # Console under the "Environment properties" area. That area takes
      # Name and Value pairs, so in our example below, the value
      # "WHERE_TO_MOUNT_EFS" is the Name of the variable, and it contains
      # a path on the EC2, for example "/efs". That would mount the EFS
      # volume at the path /efs on the filesystem.
      WHERE_TO_MOUNT_EFS=$(/opt/elasticbeanstalk/bin/get-config environment -k WHERE_TO_MOUNT_EFS)

      # This is the actual AWS EFS volume name that has been set up
      EFS_VOLUME_NAME=$(/opt/elasticbeanstalk/bin/get-config environment -k EFS_VOLUME_NAME)

      # Now create the path for the mount on the filesystem (again, in 
      # our example "/efs" as specified in the WHERE_TO_MOUNT_EFS variable)
      mkdir ${WHERE_TO_MOUNT_EFS}

      # Now actually mount the EFS to the "/efs" folder created above
      mount -t nfs4 -o nfsvers=4.1 ${EFS_VOLUME_NAME}-$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone).mydomain.com:/ ${WHERE_TO_MOUNT_EFS}

当然,这只是一个样本。

“curl”命令用于查询AWS在特殊IP 169.254.169.154上提供的信息。您的域名和路径会有所不同。

此外,这是一个在Linux上运行的Bash脚本。如果您在EB上使用Windows,则必须调整此过程。

最后,在上面的mount之后,在我们的脚本中,我们实际上继续创建一个符号链接,从我们网站的子文件夹到附加的EFS文件夹。我们还使用Bash命令管理权限,为“webapp”用户分配所需的权限。当然,这些步骤是可选的。

现在PHP只会将该文件夹视为文件系统上的路径,但实际上它位于您的EFS共享上。当重建EB环境时,会自动重新运行此脚本并重新连接EFS,因此数据似乎对EC2上的应用程序保持持久性。

我希望这有帮助

© www.soinside.com 2019 - 2024. All rights reserved.