0

I'm trying to wirte a function which is downloading content from my S3 bucket. The main problem I have is that, $s3->getObject returns class with private properties.

I'm using "aws/aws-sdk-php": "^3.54" via composer and this are my methods.

My main controller

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();
}

This is my S3 library, which I'm calling in upper controller

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;
}

And this is the response if I var_dump($response); of my library function

enter image description here

So when I try to call $response->ContentType i get Message: Undefined property: Aws\Result::$ContentType

What can I do so my class will be public and the properties will be accessible? If you need any additional informations, please let me know and I will provide. Thank you

Valor_
  • 3,461
  • 9
  • 60
  • 109
  • Since the response is a from Guzzle, I assume you can do `$response->getBody()->getContents();` or `(string) $response->getBody();` – Andrei May 16 '18 at 08:30
  • no sorry this doens't help... Call to undefined method Aws\Result::getBody() – Valor_ May 16 '18 at 08:41
  • The only other thing I could find related to your question was [this](https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.Result.html). Again, I can only assume since I've never used AWS, is that you can do [`get()`](https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.Result.html#_get) on the answer. Something like `$result->get('key_name')` – Andrei May 16 '18 at 08:44
  • OMG!!! I figure it out... :/ you have to call it as array $response['ContentType'] – Valor_ May 16 '18 at 08:45
  • Well, there you go. May wanna post it as an answer too. Other people may come across this question also. – Andrei May 16 '18 at 08:46
  • Will do! Thx for help anyway! – Valor_ May 16 '18 at 08:46

1 Answers1

1

I figure it out. You have to access this object properties as you would access the array.

In my case if I try to access content type of $response I need to call

$response['ContentType']
Valor_
  • 3,461
  • 9
  • 60
  • 109