Reputation: 380
First time messing with AWS, I already set up my EC2 server and RDS. I want to use S3 to store images uploaded from a page on my EC2 server, save the image path in a database and then show the image with a img tage at a later time.
I can do this easily with a regular VPS but not sure how to access S3 storage from my PHP code sitting in my EC2 server.
Any advice?
Upvotes: 1
Views: 2071
Reputation: 1127
When you use the AWS SDK to create a new S3 object it should return the full URL to access this image.
Save the URL that is returned when you successfully create new S3object to your database like you are now, and then pull that URL to from database using your existing PHP code to display it in HTML page, which is what I assume you are currently doing in some form or another already..
Example PHP code to upload a file, set permissions to public, and return the URL of the newly uploaded file:
use Aws\S3\S3Client;
$bucket = '*** Your Bucket Name ***';
$keyname = '*** Your Object Key ***';
// $filepath should be absolute path to a file on disk
$filepath = '*** Your File Path ***';
// Instantiate the client.
$s3 = S3Client::factory();
// Upload a file.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
echo $result['ObjectURL'];
Upvotes: 3