簡體   English   中英

AWS S3 - 訪問和使用 JSON 文件

[英]AWS S3 - accessing and working with JSON files

我正在嘗試從 AWS S3 讀取 json 文件。

我可以訪問該文件並打印出 json 值,但它不會讓我對它做任何事情。

它給我一個錯誤說:“可恢復的致命錯誤:stdClass 類的對象無法轉換為字符串”,即使它的類型設置為字符串。

我所擁有的如下:

<?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輸入是一個字符串,但該函數的輸出是一個對象。

然后將該輸出傳遞給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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM