首页 > 解决方案 > $s3->getObject 返回私有类(不能访问属性)

问题描述

我正在尝试编写一个从我的 S3 存储桶下载内容的函数。我遇到的主要问题是,$s3->getObject返回具有私有属性的类。

"aws/aws-sdk-php": "^3.54"通过作曲家使用,这是我的方法。

我的主控制器

public function download($idDocument = null) {
    $document = $this->Documents->get(['where' => ['id_document' => $idDocument]]);
    $document = $document[0];
    // Download file from S3
    $this->load->library('s3');
    $response = $this->s3->downloadFromS3($document->path);

    var_dump($response);
    die();
}

这是我在上层控制器中调用的 S3 库

public function authorize() {
    $s3 = new Aws\S3\S3Client([
        'version' => 'latest',
        'region' => 'eu-central-1',
        'credentials' => [
            'key' => $this->config->config['s3_access_key'],
            'secret' => $this->config->config['s3_secret_key']
        ]
    ]);

    return $s3;
}

public function downloadFromS3($uniqueIdTypeAndName) {
     $s3 = $this->authorize();
     $object = $s3->getObject([
         'Bucket' => $this->config->config['s3_bucket_name'],
         'Key' => $uniqueIdTypeAndName
     ]);

     return $object;
}

如果我var_dump($response);使用我的库函数,这就是响应

在此处输入图像描述

所以当我尝试调用 $response->ContentType 我得到Message: Undefined property: Aws\Result::$ContentType

我该怎么做才能让我的课程公开并且可以访问这些属性?如果您需要任何其他信息,请告诉我,我会提供。谢谢

标签: phpcodeigniteramazon-s3aws-sdkaws-php-sdk

解决方案


我想通了。您必须像访问数组一样访问此对象属性。

就我而言,如果我尝试访问 $response 的内容类型,我需要调用

$response['ContentType']

推荐阅读