Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
EXEIdeas opened this issue Mar 6, 2023 · 0 comments
Open

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

EXEIdeas opened this issue Mar 6, 2023 · 0 comments

Comments

@EXEIdeas
Copy link

EXEIdeas commented Mar 6, 2023

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));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant