Help needed for Amazon S3 SDK

I always use echo getcwd() to find the search source path then manually try the relative path. Or if file_exists(…)

1 Like

Interesting. Why would you think that? I have made many posts (perhaps on other sites) to leave out the action attribute completely.

You got me thinking though, in the case of a public method, I would say to put the public designation even though that is the default. I would say add type hints even though they are not required. I guess it just depends on what we are talking about as to whether I would say do something or not. Programmers preference really. What does hold true in all cases though, is just be consistent whatever it is you do.

Hi There,

If anyone has administrator/moderator rights here can he change the topic to “help needed for amazon S3 SDK”? I have a few more issues. Otherwise, the topic is very misleading, or advise me If I should start a new thread.

1 Like

This is the Live location, and it is throwing an error →

Fatal error : Uncaught InvalidArgumentException: Missing required client configuration options: region: (string) A “region” configuration value is required for the “s3” service (e.g., “us-west-2”). A list of available public regions and endpoints can be found at http://docs.aws.amazon.com/general/latest/gr/rande.html. in

For that particular folder/bucket in the amazon dashboard, this is the region given →

this is my configuartion.php:

<?php 
return [
  's3' => [
    'version' => 'latest',
    'region'  => 'us-east-1',
    'key'     =>  'WEFKdCo+yTEHsP47KXgJaoCuvBDsda4kHg3+N2y5', 
    'secret'  =>  'XXXXXXXXXXXXXX',
    'bucket'  =>  'appopedia'
  ]
];

In which direction should I move, in order to troubleshoot the issue.

Are you using Laravel? If so, clear the config cache.

$ artisan config:clear

1 Like

No sir. A normal practice code →

Additioanally,

Code on upload.php is →

<?php
declare(strict_types=1);
error_reporting(E_ALL);
ini_set('display_errors', 'true');
?>
<?php require '../app/init.php'; ?>
<?php 
if (isset($_FILES['file'])) {
  $file = $_FILES['file'];

  $name = $file['name'];
  $tmp_name = $file['tmp_name'];

  $ext = explode('.',$name);
  $ext = strtolower(end($ext));
  // echo $ext . "<br />";

  // Temporary Data
  $key = md5(uniqid());
  $temp_file_name = "{$key}.{$ext}";
  // $temp_file_name = rand().".${ext}";
  // echo $temp_file_name;
  $temp_file_path = "uploads/{$temp_file_name}";
  move_uploaded_file($tmp_name, $temp_file_path);
  // var_dump($ext);

  try {
    $s3->putObject([
    'Bucket' => $config['s3']['bucket'],
    'Key' => "uploads/$name",
    'Body' => fopen($temp_file_path, 'rb'),
    'ACL'  => 'public-read'
  ]);    
  } catch (S3Exception $e) {
    die('There was some Kind of uploading Issue'.$e->getMessage());
  }  

  unlink($temp_file_path);
}
?>

<?php require '../includes/header.php'; ?>
<h1>Upload </h1>
<form method="post" enctype="multipart/form-data" class="form-inline">
  <div class="form-group">
    <input type="file" name="file">    
  </div>
  <div class="form-group">
  <input type="submit" value="Upload" class="btn btn-primary">    
  </div>
</form>



<?php require '../includes/footer.php'; ?>

Off Topic:

Done.

For future reference, please note it is much better to use the flag system for such requests, to ensure they are not missed. smile

2 Likes

I got it, thanks.

2 Likes

Need the code on init.php as well, because it’s line 7 in that file that’s throwing the error (read your stack trace.)

Last time you showed us init.php, it did not include a region in its invoker.

Sidebar: It may help you to look at the source code of your error page, because PHP’s error dumps use regular file notations (\n’s) instead of HTML entities (<br>s) for its breaking. In Source Code view, the stack dump looks like:

<b>Fatal error</b>:  Uncaught InvalidArgumentException: Missing required client configuration options: 

region: (string)

  A &quot;region&quot; configuration value is required for the &quot;s3&quot; service
  (e.g., &quot;us-west-2&quot;). A list of available public regions and endpoints can be
  found at http://docs.aws.amazon.com/general/latest/gr/rande.html. in /home1/toolculator/public_html/app.trafficopedia.com/amazons3/vendor/aws/aws-sdk-php/src/ClientResolver.php:406
Stack trace:
#0 /home1/toolculator/public_html/app.trafficopedia.com/amazons3/vendor/aws/aws-sdk-php/src/ClientResolver.php(302): Aws\ClientResolver-&gt;throwRequired(Array)
#1 /home1/toolculator/public_html/app.trafficopedia.com/amazons3/vendor/aws/aws-sdk-php/src/AwsClient.php(199): Aws\ClientResolver-&gt;resolve(Array, Object(Aws\HandlerList))
#2 /home1/toolculator/public_html/app.trafficopedia.com/amazons3/vendor/aws/aws-sdk-php/src/S3/S3Client.php(346): Aws\AwsClient-&gt;__construct(Array)
#3 /home1/toolculator/public_html/app.trafficopedia.com/amazons3/app/init.php(7): Aws\S3\S3Client-&gt;__co in <b>/home1/toolculator/public_html/app.trafficopedia.com/amazons3/vendor/aws/aws-sdk-php/src/ClientResolver.php</b> on line <b>406</b><br />

1 Like

Hi there @m_hutley,

this is the init.php code →

<?php 
use Aws\S3\S3Client;
require '../vendor/autoload.php';
$config = require 'configuration.php';
$s3 = new S3Client([
  'version' => $config['s3']['version'],
  'secret' => $config['s3']['secret'],
]);

The error message points out what your code confirms. You’re not specifying a region in this code.

1 Like

I have corrected that mistake, but now got a new mistake

**Fatal error** : Uncaught exception 'Aws\S3\Exception\S3Exception' with message 'Error executing "PutObject" on "https://appopedia.s3.amazonaws.com/uploads/Ava%20Gardner%2010.jpg"; AWS HTTP error: Client error: PUT https://appopedia.s3.amazonaws.com/uploads/Ava%20Gardner%2010.jpg` resulted in a 403 Forbidden response: <?xml version="1.0" encoding="UTF-8"?> AccessDeniedAccess Denied4A5240 (truncated…) AccessDenied (client): Access Denied - <?xml version="1.0" encoding="UTF-8"?> AccessDeniedAccess Denied4A524073762E5D2FDj0GUqLHYI4hHWcN6PKBskc30S5LwEIvT3Owzgv5akTf9r1qBOOO6PTPGuTSR0kKeQRh9PbYdVQ=’ GuzzleHttp\Exception\ClientException: Client error: PUT https://appopedia.s3.amazonaws.com/uploads/Ava%20Gardner%2010.jpg resulted in a 403 Forbidden response: <?xml version="1.0" encoding="UTF-8"?> AccessDeniedAccess Denied4A5240`

I believe this is caused by some access issues not defined in/among these three →

  • IAM policies
  • S3 Bucket policies
  • S3 ACL

Which one should I focus on to get rid of the access issue?

I also go this flowchart →

When we are accesing bcuket as an object do we have to deal with IAM?

This has fixed the issue adding amazons3 full acess to the user in permissions/policy→
bucket

This means we were dealing with IAM users and its policy/permission issues.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.