与往常一样,操作越简单,AWS文档就越让我感到困惑。我想使用PHP来读写存储在EFS上的文件。我在使用AWS 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
答案 0 :(得分: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,则必须调整此过程。
最后,在上述安装之后,我们实际上在脚本中继续创建从网站的子文件夹到附加的EFS文件夹的符号链接。我们还使用Bash命令管理权限,这些命令为“ webapp”用户分配所需的权限。这些步骤当然是可选的。
现在,PHP只会将该文件夹视为文件系统上的路径,但实际上它在您的EFS共享上。重建EB环境后,该脚本会自动重新运行并重新连接EFS,因此数据似乎对EC2上的应用程序是持久的。
我希望这对您有帮助