プロフィール画像において、AWS SDK for phpで、AWS S3へ画像をアップロードしたいです。
<現状>
echo $result['ObjectURL'] . "\n";
でAWSのURLがブラウザに表示され、$keyname = 'upload-nct/profile/${filename}';
で仮で書いているfilenmeのテキストファイルがS3に格納されています。S3への接続はできましたが、下記のその先がわかりません。
<実現したいこと>
HTMLのid="profilePhoto"でアップロードした画像をS3に保存したいです。
html
1<div class="profile-pic"> 2 <img data-ele="profile_pic" src="%show_main_user_image%" alt="Change Profile Picture" style="width: 100%"/ > 3</div> 4<form id="frmProfilePic" enctype="multipart/form-data"action="s3upload.php" method="post"> 5<a data-ele="cropProfPic" class="profile-link" href="javascript:void(0)";>プロフィール写真の変更 6<input type="file" id="profilePhoto" name="key" value="upload-nct/profile/${filename}" /> 7<input type="hidden" name="crop_action" value="edit_profile" /> 8<input type="hidden" name="height" id="height" value="500" /> 9<input type="hidden" name="width" id="width" value="500" /> 10 11
php
1 2<!-- AWS S3への画像アップロード --> 3<?php 4 5// Include the AWS SDK using the Composer autoloader. 6require '../../vendor/autoload.php'; 7 8use Aws\S3\S3Client; 9use Aws\S3\Exception\S3Exception; 10 11 12$bucket = '********[仮で置いています]; 13$keyname = 'upload-nct/profile/${filename}'; 14 15// Instantiate the client. 16$s3 = S3Client::factory([ 17'version' => '2006-03-01', 18'region' => 'ap-northeast-1' 19]); 20 21try { 22 // Upload data. 23 $result = $s3->putObject(array( 24'Bucket' => $bucket, 25'Key' => $keyname, 26'id' => $profilePhoto, 27'Body' => 'Hello, world!', 28 'ACL' => 'public-read' 29 )); 30 // Print the URL to the object. 31 echo $result['ObjectURL'] . "\n"; 32} catch (S3Exception $e) { 33 echo $e->getMessage() . "\n"; 34} 35?>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。