AWS S3 - 访问和使用 JSON 个文件

AWS S3 - accessing and working with JSON files

我正在尝试从 AWS S3 读取 json 文件。

我可以访问该文件并打印出 json 值,但它不允许我对它执行任何操作。

它给我一个错误提示:'Recoverable fatal error: Object of class stdClass could not be converted to string' 即使它的类型设置为字符串。

我的情况如下:

<?php
require "../vendor/autoload.php";
use Aws\S3\S3Client;

$aws_credentials = [
    'region' => 'eu-west-1',
    'version' => 'latest',
    'credentials' => [
        'key' => 'xxxxxxxxx',
        'secret' => 'xxxxxxxxxxxxxxxx'
    ]
];

$aws_client = new S3Client($aws_credentials);

$bucket = 'xxxxxxx';
$file_name = 'data.json';

$result = $aws_client->getObject(array(
    'Bucket' => $bucket,
    'Key'    => $file_name
));

$json = (string)$result['Body'];

echo $json; // this outputs the json I want to work with
echo '<br />';
echo gettype($json); // this outputs 'string'
echo '<br />';
echo json_decode($json); // this outputs 'Recoverable fatal error:  Object of class stdClass could not be converted to string'

?>

json_decode输入是一个字符串,但是那个函数的输出是一个对象。

然后您将 output 传递给 echo,但 echo 需要将其转换为字符串,但不知道如何操作。

如果我们将输出分配给变量,这可能会更清楚:

echo gettype($json); // this outputs 'string'
echo '<br />';
$object = json_decode($json);
echo gettype($object); // this will output 'object'
var_dump($object); // this will show you what's in the object
echo $object; //  this is an error, because you can't echo an object