Harinder
Harinder

Reputation: 1257

Getting Private File from AWS S3 with php

I am trying to get private file from AWS S3 But getting error

 $client = S3Client::factory([
        'version' => 'latest',
        'region'  => 'us-west-2',
        'signature'    => 'v4',
        'credentials' => [
            'key'    => '****',
            'secret' => '-****'
        ]
    ]);


  $bucket =  'name';
    $file = 'lks/002geH1P1WFXGHWafCwgTw5mwm58X.pdf';
    $url = "{$bucket}/{$file}";

    $request = $client->get($url);
    $signedUrl = $client->createPresignedUrl($request, '+10 minutes');
    echo  $signedUrl;

I am getting this error

Catchable fatal error: Argument 2 passed to Aws\AwsClient::getCommand() must be of the type array, string given, called in /home/ubuntu/vendor/aws/aws-sdk-php/src/AwsClientTrait.php on line 78 and defined in /home/ubuntu/vendor/aws/aws-sdk-php/src/AwsClient.php on line 202

Upvotes: 1

Views: 1502

Answers (1)

Harinder
Harinder

Reputation: 1257

This works ... Hope it will help others ;)

$cmd = $client->getCommand('GetObject', [
            'Bucket' => $bucket,
            'Key' => $file
        ]);

        $request = $client->createPresignedRequest($cmd, '+20 minutes');
        $presignedUrl = (string)$request->getUri();
        echo $presignedUrl;

Upvotes: 1

Related Questions