Force-Download with php on Amazon S3

后端 未结 12 1890
予麋鹿
予麋鹿 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:09

     '',
                    'secret' => '',
            );
    
            // Create a service builder using a configuration file
            $aws = Aws::factory($config);
    
            // Get the client from the builder by namespace
            $s3 = $aws->get('S3');
            $params = array(
                'Bucket'                        => $bucket,
                'Key'                           => $key,
                'ResponseContentType'           => 'application/octet-stream',
                'ResponseContentDisposition'    => 'attachment; filename="'.$key,
            );
    
            if($force_download){
                $params['ResponseContentType'] = 'application/octet-stream';
                $params['ResponseContentDisposition'] = 'attachment; filename="'.basename($key).'"';
            }
    
            $command = $s3->getCommand('GetObject',$params);
            return $command->createPresignedUrl('+1 days');
        }
    
        $bucket = '';
        $filename = '';
    
    
        $url = gen_url($bucket,$filename,true);
    
        echo "\n".$url."\n\n";
    

    The code above works, you just need to install the S3 composer dependency and link in the autoload file, put your key/secret into config and then supply the bucket/filename.

提交回复
热议问题