![](/img/trans.png)
[英]AWS s3 Uploading files on localhost, but not working in the real URL
[英]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.