How do I download a file with php and the Amazon S3 sdk?

后端 未结 6 1944
不思量自难忘°
不思量自难忘° 2020-12-15 21:01

I\'m trying to make it so that my script will show test.jpg in an Amazon S3 bucket through php. Here\'s what I have so far:

require_once(\'library/AWS/sdk.cl         


        
6条回答
  •  春和景丽
    2020-12-15 21:25

    If you're still looking for a relevant answer in 2019+, With AWS SDK for PHP 3.x and specifically '2006-03-01' with composer, the following worked for me

    
    ...
    
    /**
     * Download a file
     * 
     * @param   string  $object_key
     * @param   string  $file_name
     * @return  void
     */
    function download($object_key, $file_name = '') {
      if ( empty($file_name) ) {
        $file_name = basename($file_path);
      }
      $cmd = $s3->getCommand('GetObject', [
        'Bucket'                        => '',
        'Key'                           => $object_key,
        'ResponseContentDisposition'    => "attachment; filename=\"{$file_name}\"",
      ]);
      $signed_url = $s3->createPresignedRequest($cmd, '+15 minutes') // \GuzzleHttp\Psr7\Request
                    ->getUri() // \GuzzleHttp\Psr7\Uri
                    ->__toString();
      header("Location: {$signed_url}");
    }
    download('', '');
    
    
    

    NOTE: This is a solution for those who would want to avoid the issues that may arise from proxying the download through their servers by using a direct download link from AWS.

    提交回复
    热议问题