Home>

What's happening now
As the title suggests, the following error is displayed.

string (798) "Error executing" PutObject "on" https://s3.Region.amazonaws.com/%2A%2A%2A%20 Bucket name% 20% 2A% 2A% 2A/s3/sample.png ";AWS HTTP error: Client error:PUT https://s3.Region.amazonaws.com/%2A%2A%2A%20 Bucket name% 20% 2A% 2A% 2A/s3/sample.pngresulted in a400 Bad Requestresponse: InvalidBucketNameThe specified bucket is not valid.<(Truncated ...) InvalidBucketName (client): The specified bucket is not valid. --InvalidBucketNameThe specified bucket is not valid.* Bucket name *518DE16D49E9F219fY + flLyc7w + Tg4X + XnwzmEoZg7NFppfEUPikYbGm01vj5DSQRu6zHtWway // MnHy6dUgtqmYB2I = "NULL"

code

<? php
require_once "./../vendor/autoload.php";
use Aws \ S3 \ S3Client;
use Aws \ Exception \ AwsException;
use Aws \ S3 \ Exception \ S3Exception;
$bucket ='--- bucket name ---';
$key ='--- mykey ---';
$secret ='--- mysecret ---';
$s3 = new S3Client ([[
  'version' =>'latest',
  'cledentials' =>[
    'key' =>$key,
    'secret' =>$secret
  ],,
  'region' =>'region',
]);
$file = $_FILES ['img'] ['tmp_name'];
if (! is_uploaded_file ($file)) {
  return;
}
try {
  $result = $s3->putObject ([[
    'Bucket' =>$bucket,
    'Key' =>'s3/sample.png',
    'Body' =>' private-read',
    'ContentType' =>mime_content_type ($file)
  ]);
} catch (S3Exception $e) {
  var_dump ($e->getMessage ());
}
var_dump ($result);
<! DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
<title>test</title>
</head>
<body>
<form action = "s3.php" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "img">
<button type = "submit">Upload</button>
</form>
</body>
</html>


I want to resolve the error and upload the image to S3.

What I tried
Check the bucket name → No problem

environment
macOS Mojave 10.14.6
Visual Studio Code 1.48.2
PHP 7.1.33

Postscript
Separately, after setting the environment variable, specify the value with the getenv function, and read it again, the following error occurred.

PHP Fatal error: Uncaught Aws \ Exception \ CredentialsException: Error retrieving credentials from the instance profile metadata service. (CURL error 28: Connection timed out after 1000 milliseconds (see https://curl.haxx.se/libcurl/c/libcurl- errors.html)) in /app/vendor/aws/aws-sdk-php/src/Credentials/InstanceProfileProvider.php:240

We haven't solved it yet. I will continue to investigate.

Postscript
Modified code

<? php
require_once "./../vendor/autoload.php";
use Aws \ S3 \ S3Client;
use Aws \ Exception \ AwsException;
use Aws \ S3 \ Exception \ S3Exception;
$bucket = getenv ('S3_BUCKET_KEY');
$key = getenv ('S3_ACCESS_KEY');
$secret = getenv ('S3_SECRET_KEY');
$s3 = new S3Client ([[
  'version' =>'latest',
  'region' =>getenv ('S3_REGION'),
  'cledentials' =>[
    'key' =>$key,
    'secret' =>$secret
  ]
]);
if (! is_uploaded_file ($_FILES ['img'] ['tmp_name'])) {
  return;
}
$file = fopen ($_FILES ['img'] ['tmp_name'],'rb');
try {
  $result = $s3->putObject ([[
    'ACL' =>' public-read',
    'Bucket' =>$bucket,
    'Key' =>'sample.jpg',
    'Body' =>$file,
    'ContentType' =>mime_content_type ($_FILES ['img'] ['tmp_name'])
  ]);
} catch (S3Exception $e) {
  var_dump ($e->getMessage ());
}
var_dump ($result);
  • Answer # 1

    Is $bucket ='--- BUCKET_NAME ---' ;. Is the before and after --- required?

    ---If you have, that is the cause. Enter only the bucket name you actually created.
    The same is true for keys.
    It is not good to write the key directly to the file in the first place, so please use environment variables.

    … I'll check it just in case, not the name of the bucket I created--- BUCKET_NAME ---You didn't put the string, right?