无法从 php 访问 localstack s3
localstack s3 can't accessable from php
我正在尝试在 docker container.I 为 s3.I 创建的演示存储桶中使用 localstack 可以查看我从终端上传的文件,但我无法访问 s3从代码。我在哪里犯了错误?我在下面分享我的代码。谢谢。我的 yml 文件如下所示:
version: "3.7"
services:
php:
image: php:7.4-fpm
volumes:
- ~/image.minyay.com/src:/var/www/html
- ~/image.minyay.com/docker/php.ini:/usr/local/etc/php/php.ini
web:
image: nginx:1.17
ports:
- 80:80
volumes:
- ~/image.minyay.com/src:/var/www/html
- ~/image.minyay.com/docker/site.conf:/etc/nginx/conf.d/site.conf
depends_on:
- php
localstack:
image: localstack/localstack:0.11.0
container_name: localstack
ports:
- "4563-4599:4563-4599"
- '8055:8080'
environment:
- SERVICES=s3
- DOCKER_HOST=unix:///var/run/docker.sock
- DATA_DIR=/tmp/localstack/data
volumes:
- /tmp/localstack:/tmp/localstack
- /var/run/docker.sock:/var/run/docker.sock
- ./aws:/docker-entrypoint-initaws.d
我的 php 代码如下所示:
public function get_big_folder_in_s3($file_path){
try {
$pages = $this->s3->getPaginator('ListObjects', [
'Bucket' => $this->bucket,
'Prefix' => $file_path, //DESTINATION
]);
foreach ($pages as $page) {
foreach ($page['Contents'] as $object) {
// echo $object['Key'] . PHP_EOL;
$result[] = $object['Key'];
}
}
return $result;
} catch (S3Exception $e) {
return false;
}
}
我的 s3 客户端创建:
$this->s3 = S3Client::factory([
'credentials' => false,
'version' => 'latest',
'region' => 'eu-central-1',
'endpoint' => 'http://localhost:4572',
'force_path_style' => true,
]);
你没有分享你的客户端初始化,但 localstack 通常需要传递一个端点,因此我想在 PHP 我猜它应该看起来类似于:
$s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-west-2',
'endpoint' => 'http://localstack:4566',
]);
我正在尝试在 docker container.I 为 s3.I 创建的演示存储桶中使用 localstack 可以查看我从终端上传的文件,但我无法访问 s3从代码。我在哪里犯了错误?我在下面分享我的代码。谢谢。我的 yml 文件如下所示:
version: "3.7"
services:
php:
image: php:7.4-fpm
volumes:
- ~/image.minyay.com/src:/var/www/html
- ~/image.minyay.com/docker/php.ini:/usr/local/etc/php/php.ini
web:
image: nginx:1.17
ports:
- 80:80
volumes:
- ~/image.minyay.com/src:/var/www/html
- ~/image.minyay.com/docker/site.conf:/etc/nginx/conf.d/site.conf
depends_on:
- php
localstack:
image: localstack/localstack:0.11.0
container_name: localstack
ports:
- "4563-4599:4563-4599"
- '8055:8080'
environment:
- SERVICES=s3
- DOCKER_HOST=unix:///var/run/docker.sock
- DATA_DIR=/tmp/localstack/data
volumes:
- /tmp/localstack:/tmp/localstack
- /var/run/docker.sock:/var/run/docker.sock
- ./aws:/docker-entrypoint-initaws.d
我的 php 代码如下所示:
public function get_big_folder_in_s3($file_path){
try {
$pages = $this->s3->getPaginator('ListObjects', [
'Bucket' => $this->bucket,
'Prefix' => $file_path, //DESTINATION
]);
foreach ($pages as $page) {
foreach ($page['Contents'] as $object) {
// echo $object['Key'] . PHP_EOL;
$result[] = $object['Key'];
}
}
return $result;
} catch (S3Exception $e) {
return false;
}
}
我的 s3 客户端创建:
$this->s3 = S3Client::factory([
'credentials' => false,
'version' => 'latest',
'region' => 'eu-central-1',
'endpoint' => 'http://localhost:4572',
'force_path_style' => true,
]);
你没有分享你的客户端初始化,但 localstack 通常需要传递一个端点,因此我想在 PHP 我猜它应该看起来类似于:
$s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-west-2',
'endpoint' => 'http://localstack:4566',
]);