【问题标题】:$s3->getObject returns private class (can't access properties)$s3->getObject 返回私有类(不能访问属性)
【发布时间】:2018-10-26 04:58:47
【问题描述】:

我正在尝试编写一个从我的 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

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

【问题讨论】:

  • 由于回复来自 Guzzle,我假设您可以使用 $response->getBody()->getContents();(string) $response->getBody();
  • 不抱歉这没有帮助...调用未定义的方法 Aws\Result::getBody()
  • 我能找到的与您的问题相关的唯一另一件事是this。同样,由于我从未使用过 AWS,我只能假设您可以在答案上执行 get()。类似$result->get('key_name')
  • 天啊!!!我想通了...:/您必须将其称为数组 $response['ContentType']
  • 好吧,给你。可能也想将其发布为答案。其他人也可能会遇到这个问题。

标签: php codeigniter amazon-s3 aws-sdk aws-php-sdk


【解决方案1】:

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

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

$response['ContentType']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 2017-08-27
    • 2016-12-14
    • 2017-02-23
    • 2017-08-25
    相关资源
    最近更新 更多