2015-07-20 63 views
0

我無法使用適用於PHP的AWS開發工具包創建Presigned Url。我的代碼 -使用適用於PHP的AWS開發工具包調用未定義的方法createPresignedUrl()

function connect() 
    { 
    // Instantiate the S3 class and point it at the desired host 
    date_default_timezone_set('GMT'); 
    return S3Client::factory(array(
    'region' => 'us-west-2', 
    'version' => 'latest', 
    'credentials' => [ 
      'key' => $key, 
      'secret' => $secret 
     ] 

    )); 

function getSignedS3URLForObject($fileName) 
     { 
      // GET CURRENT DATE 
      $milliseconds = round(microtime(true) * 1000); 
      $expiration = $milliseconds + (1000 * 60 * 60 * 24 * 30 * 2); 
      $s3 = self::connect(); 
      $command = $s3->getCommand('GetObject', array(
       'Bucket'  => self::$customerBucket, 
       'Key'   => $fileName, 
       'ContentType' => 'image/jpeg', 
       'Body'  => '', 
       'ContentMD5' => false 
      )); 
      $signedUrl = $command->createPresignedUrl($expiration); 
      echo urldecode($signedUrl); 
      return $signedUrl; 
     } 

它給了我未來無差錯

Fatal error: Call to undefined method Aws\Command::createPresignedUrl() in /Users/waverley_lv/WaverleySoftware/workspace/fox.php.auto/sites/default/behat-tests/util/S3Utility.php on line 103

+0

在你調用'$ s3 = self :: connect();'''''''''''''之後'執行'var_dump($ s3)你可能沒有找回你期待的對象。 – nickb

+0

您使用的是什麼版本的AWS SDK? –

+0

我正在使用AWS SDK S3 - 發佈v2.1.39 –

回答

1

使用s3.0.0 V3 - 我做了如下得到這個工作。

$command = $s3->getCommand('GetObject', array(
      'Bucket'  => $this->customerBucket, 
      'Key'   => $fileName, 
      'ContentType' => 'image/png', 
      'ResponseContentDisposition' => 'attachment; filename="'.$fileName.'"' 
     )); 
$signedUrl = $s3->createPresignedRequest($command, "+1 week"); 
+0

謝謝,它幫助我) –

相關問題