PHP program to pull data from S3 bucket to an EC2 instance giving 500 error
by redd9 from LinuxQuestions.org on (#4ZS76)
As the title states, I have a PHP program sitting on an EC2 instance running Amazon Linux 2 which is supposed to pull data (ultimately going to be videos, but just testing with a text file for now) from an S3 bucket and offer it up in a web browser.
I have created the EC2 instance with a role to have full access to S3, and running
Code:aws s3 lsdoes produce a listing of my buckets.
I tried to follow Amazon's example: https://docs.aws.amazon.com/AmazonS3...ngleOpPHP.html
and am using the following code:
Code:<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = 'mybucket';
$keyname = 'mybucketfolder/test.txt';
$s3 = new S3Client([
'version' => 'latest',
'region' => 'us-east-2'
]);
try {
$result = $s3->getObject([
'Bucket' => $bucket,
'Key' => $keyname
]);
header("Content-Type: {$result[text/plain]}");
echo $result['Body'];
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
?>which unfortunately just produces a 500 error. I've double-checked that the region is correct, the autoload.php file is in the correct directory, and the MIME type should be correct as well as it's just a text file.
Creating a .php file with only
Code:<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = 'mybucket';
$keyname = 'mybucketfolder/test.txt';
echo "Hello"
?>does work, so I am wondering if the problem is tied to creating the S3 client somehow.
Any help or ideas would be greatly appreciated.


I have created the EC2 instance with a role to have full access to S3, and running
Code:aws s3 lsdoes produce a listing of my buckets.
I tried to follow Amazon's example: https://docs.aws.amazon.com/AmazonS3...ngleOpPHP.html
and am using the following code:
Code:<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = 'mybucket';
$keyname = 'mybucketfolder/test.txt';
$s3 = new S3Client([
'version' => 'latest',
'region' => 'us-east-2'
]);
try {
$result = $s3->getObject([
'Bucket' => $bucket,
'Key' => $keyname
]);
header("Content-Type: {$result[text/plain]}");
echo $result['Body'];
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
?>which unfortunately just produces a 500 error. I've double-checked that the region is correct, the autoload.php file is in the correct directory, and the MIME type should be correct as well as it's just a text file.
Creating a .php file with only
Code:<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = 'mybucket';
$keyname = 'mybucketfolder/test.txt';
echo "Hello"
?>does work, so I am wondering if the problem is tied to creating the S3 client somehow.
Any help or ideas would be greatly appreciated.