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

后端 未结 6 1942
不思量自难忘°
不思量自难忘° 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:31

    Both of these methods work for me. The first way seems more concise.

        $command = $s3->getCommand('GetObject', array(
           'Bucket' => 'bucket_name',
           'Key'    => 'object_name_in_s3'  
           'ResponseContentDisposition' => 'attachment; filename="'.$my_file_name.'"'
        ));
    
        $signedUrl = $command->createPresignedUrl('+15 minutes');
        echo $signedUrl;
        header('Location: '.$signedUrl);
        die();
    

    Or a more wordy but still functional way.

        $object = $s3->getObject(array(
        'Bucket' => 'bucket_name',
        'Key'    => 'object_name_in_s3'   
        ));
    
        header('Content-Description: File Transfer');
        //this assumes content type is set when uploading the file.
        header('Content-Type: ' . $object->ContentType);
        header('Content-Disposition: attachment; filename=' . $my_file_name);
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
    
        //send file to browser for download. 
        echo $object->body;
    

提交回复
热议问题