File Uploads with S3 (v2023)

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
rvanore
User
Posts: 44

File Uploads with S3 (v2023)

Post by rvanore »

Hello,

We are struggling to reliably upload multiple files via our Add/Edit pages. We have the multiple upload option enabled.

Is there a way we can access the files directly so we can upload to S3 similar to the below code?

// upload the stream to S3
	$stream = fopen("s3://hycare-uploads/training/{$tcID}/{$title}.pdf", 'w');
	fwrite($stream, $outputStream);
	fclose($stream);

we are using PHPMaker 2023, PHP 8.3.8, and php-fpm.

Any advice would be appreciated. Thank you.


rvanore
User
Posts: 44

Post by rvanore »

We were using file path encryption but our file names were encrypted and our users did not want that. Upon turning off encryption in the advanced settings we are now getting the following error.

/var/www/html/vendor/aws/aws-sdk-php/src/EndpointV2/Ruleset/RulesetParameter.php(125): Input parameter Key is the wrong type. Must be a String.

Our Global Code is as follows:

putenv("AWS_ACCESS_KEY_ID=####################"); // UAT access key ID - UAT Specific
putenv("AWS_SECRET_ACCESS_KEY=##################"); // UAT secret access key

$s3client = new \Aws\S3\S3Client([
"version" => "latest",
"region" => "us-east-1",
"acl" => "public-read"

// "http" => ["verify" => FALSE] // Disable certificate verification (this is insecure!)
]);

$s3client->registerStreamWrapper();

Is there a way to have the path encrypted but not the filename?


arbei
User
Posts: 9719

Post by arbei »

What is the non-encrypted file path? Is it pointing to your file in your bucket correctly?


rvanore
User
Posts: 44

Post by rvanore »

We were able to work around the problem by modifying src/phpfn.php's CleanPath() function to return before executing its code. We had discovered by adding our own Log statements that the error we were experiencing was that sometimes the temp folder would get cleared out by this function before the user submitted the form, so the file would be missing from the temp folder and the upload would fail. We are now manually clearing out the temp folder every 24 hours via a Lambda function.

Regarding the file encryption, we can't view the unencrypted path due to the error with the aws-sdk. We were able to prevent the error from throwing by changing DbField.php's hrefPath() function to cast the result of Random() to a string, but the path that resulted from that was not using the application's API, which we still want to use since the files in S3 are only accessible using the credentials within the application.

This issue only happens with PDFs - other files download with their original filename. However, if the file is a PDF, it opens in a new tab and the filename changes to the encrypted name like in the API links. We just want it so that when someone clicks a PDF file href in the application, the file that opens has the correct original filename.


Post Reply