和往常一样,操作越简单,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
发布于 2018-09-07 15:41:01
为什么不直接在运行代码的EC2实例上挂载EFS作为共享。然后,EFS只是PHP应用程序可以写入的常规路径。这就是我们在弹性豆柄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脚本。如果你在你的电子束上使用Windows,你将不得不适应这个过程。
最后,在上面的挂载之后,我们在脚本中继续创建一个符号链接,从我们网站的一个子文件夹到附加的EFS文件夹。我们还使用Bash命令管理权限,这些命令为"webapp“用户分配它所需的权限。当然,这些步骤是可选的。
现在,PHP只会将该文件夹视为文件系统上的路径,但实际上它位于您的EFS共享上。当重新构建EB环境时,这个脚本会自动重新运行并重新附加EFS,因此数据似乎会持久化到EC2上的应用程序。
我希望这能帮到你
https://stackoverflow.com/questions/52213468
复制相似问题