tpyo/amazon-s3-php-class

How To Get Public URL Of Object After Uploading A File Object?

EXEIdeas opened this issue · 0 comments

I am using this for my simple little PHP Form to upload files from the form to the AWS bucket via HTMP-PHP. Here is the simple code for this concept.

<?php
require_once('s3.php'));

if (isset($_POST["submit"])) {
	// Define Constants
	define('AWS_S3_KEY', 'my_aws_key_here');
	define('AWS_S3_SECRET', 'my_aws_secret_key_here');
	define('AWS_S3_REGION', 'us-east-2');
	define('AWS_S3_BUCKET', 'my_bucket_name');
	define('AWS_S3_PATH', 'my_bucket_path');
	define('AWS_S3_URL', 'http://s3.'.AWS_S3_REGION.'.amazonaws.com/'.AWS_S3_BUCKET.'/');

	// Get File From The HTML Form
	$tmpfile = $_FILES['file']['tmp_name'];
	$file = $_FILES['file']['name'];
	
	// Upload To AWS
	if (defined('AWS_S3_URL')) {
		S3::setAuth(AWS_S3_KEY, AWS_S3_SECRET);
		S3::setRegion(AWS_S3_REGION);
		S3::setSignatureVersion('v4');
		$result = S3::putObject(S3::inputFile($tmpfile), AWS_S3_BUCKET, $path.$file, S3::ACL_PUBLIC_READ);
		print_r(S3::getObjectInfo(AWS_S3_BUCKET, AWS_S3_PATH.$file));echo "<br/>";
		echo "<br/>";echo "<br/>";
	} else {
		// Persist to local-disk
		move_uploaded_file($tmpfile, AWS_S3_PATH);
	}
	
	echo "\nContents for bucket: ".AWS_S3_BUCKET."\n";echo "<br/>";
	print_r(S3::getBucket(AWS_S3_BUCKET));
}
?>


<form method="post" action="" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" name="submit" value="Upload" />
</form>

After this, I got this in return where I want the URL...

Array ( [date] => 1678131187 [time] => 1674733540 [hash] => ae0b6a07deea0f5687339cdgdsdfdsfa1111eeae8 [type] => application/octet-stream [size] => 109048673 )

But when I call the whole bucket then I am able to see the URL also...

Contents for bucket: my_bucket_name
Array ( [my_first_file.jpg] => Array ( [name] => my_first_file.jpg [time] => 1674544620 [size] => 0 [hash] => d41d8cd98f00b2sdsfds04e9800998ecf8427e ) [my_folder/my_first_file.mp4] => Array ( [name] => my_folder/my_first_file.mp4 [time] => 1674733540 [size] => 109048673 [hash] => ae0b6a07deea0f5687339cdgdsdfdsfa1111eeae8) )

Tell me how can I get my uploaded single file URL in response via print_r(S3::getObjectInfo(AWS_S3_BUCKET, AWS_S3_PATH.$file));