Force-Download with php on Amazon S3

后端 未结 12 1935
予麋鹿
予麋鹿 2020-12-13 07:40

I am trying to use http://code.google.com/p/amazon-s3-php-class/ to force-dowload files from AWS S3. I have an mp3 that I want people to \"play\" or \"download.\" By default

12条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 08:15

    Just wanting to post a contribution to this, Alex Neth is correct on this reference, but i do not feel a link is sufficient information, using amazon's own AWS PHP SDK2. Below I've outlined a basic (untested) method for calling data this way, you can use either S3 Factory Method or AWS Service Builder to make the S3 Client.

     '',
        'secret' => ''
    ));*/
    
    // OR AWS Service Builder
    use Aws\Common\Aws;
    
    // Create a service builder using a configuration file
    $aws = Aws::factory('/path/to/my_config.json');
    
    // Get the client from the builder by namespace
    $s3 = $aws->get('S3');
    
    // Now lets create our request object.
    $command = $s3->getCommand('GetObject',array(
        'Bucket'                        => 'your-bucket-name',
        'Key'                           => 'keyname',
        'ResponseContentType'           => 'application/octet-stream',
        'ResponseContentDisposition'    => 'attachment; filename="filename.mp3',
    ));
    $url = $command->createPresignedUrl('+1 days');
    ?>
    

    You can then use PHP's header("Location: $url"); in order to redirect the visitor to the MP3 file with a force download, this should prevent it from playing in the browser, Please note, i use ResponseContentType quite frequently but I've never used ResponseContentDisposition with AWS (it should work according to the docs).

    Converting this sample into a function should be easy, you could even pass in $bucket, $key, $force_download as such

    get('S3');
        $params = array(
            'Bucket'                        => $bucket,
            'Key'                           => 'keyname',
            'ResponseContentType'           => 'application/octet-stream',
            'ResponseContentDisposition'    => 'attachment; filename="filename.mp3',
        );
    
        if($force_download){
            $params['ResponseContentType'] = 'application/octet-stream';
            $params['ResponseContentDisposition'] = 'attachment; filename="'.basename($key).'"';
        }
    
        $command = $s3->getCommand('GetObject',$params);
        return $command->createPresignedUrl('+1 days');
    }
    
    // Location redirection to an MP3 force downlaod
    header("Location: ".gen_url("recordings","my-file.mp3",true));
    // Location redirection to a MP3 that lets the browser decide what to do.
    header("Location: ".gen_url("recordings","my-file.mp3"));
    
    ?>
    

    WARNING, if you haven't figured it out, this requires the AWS PHP SDK 2 currently (April 7th 2014) found here http://aws.amazon.com/sdkforphp/ This code is mostly pseudo code and may require some additional tweaking to actually make work as i'm referencing this from memory.

提交回复
热议问题