これへの貢献を投稿したいだけで、Alex Neth はこのリファレンスで正しいですが、Amazon 独自の AWS PHP SDK2 を使用して、リンクが十分な情報ではないと感じています。以下に、この方法でデータを呼び出すための基本的な (テストされていない) メソッドの概要を示します。S3 ファクトリー メソッドまたは AWS Service Builder を使用して S3 クライアントを作成できます。
<?php
// S3 Factory Method
/*use Aws\S3\S3Client;
$s3= S3Client::factory(array(
'key' => '<aws access key>',
'secret' => '<aws secret key>'
));*/
// 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');
?>
その後、PHP の header("Location: $url"); を使用できます。強制ダウンロードで訪問者を MP3 ファイルにリダイレクトするために、これによりブラウザでの再生が防止されます。注意してください、私は ResponseContentType を頻繁に使用しますが、AWS で ResponseContentDisposition を使用したことはありません (ドキュメントに従って動作するはずです)。 )。
このサンプルを関数に変換するのは簡単です。$bucket、$key、$force_download などを渡すこともできます。
<?php
use Aws\Common\Aws;
function gen_url($bucket,$key,$force_download=false){
// OR AWS Service Builder (personal method to do this)
// 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');
$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"));
?>
警告、わからない場合は、現在 (2014 年 4 月 7 日) ここにある AWS PHP SDK 2が必要ですhttp://aws.amazon.com/sdkforphp/このコードはほとんどが擬似コードであり、追加の調整が必要になる場合がありますこれをメモリから参照しているので、実際に機能させることができます。