0

I am trying to create directory in amazon aws s3 for that I am trying following code ( I am using v3 php sdk)

$bucketName = 'somebucketName';
$key = 'folderName';

$params = [
    'Bucket' => $bucketName,
    'Key' => $key . '/'
  ];
$s3->putObject($params);

$s3 is instance of $s3 = new Aws\S3\S3Client class, I am getting bucket and object successfully with my current configuration.

it was working fine before but now I am getting error

Fatal error: Uncaught exception 'Aws\S3\Exception\S3Exception' with message 'Error executing "PutObject" on "https://s3-us-west-2.amazonaws.com/sdfsdf/demoer/";

AWS HTTP error: Client error: 411 MissingContentLength (client): You must provide the Content-Length HTTP header.

Richerd fuld
  • 364
  • 1
  • 10
  • 23

2 Answers2

2

This error is due to you are not passing any image or object for put. pass a object too. I also faced similar kind of problem https://stackoverflow.com/questions/32117596/aws-s3-uploaded-images-are-getting-corrupted Check the below code.

  try {
    $result = $s3->putObject(array(
       'Bucket' => $bucketName,
       'Key' =>  $key . '/',
       'SourceFile' => $filepath, // file path which is putting on AWS S3, Path should be absolute path like $filepath = "/var/www/html/for_testing_aws/assets/img/avtar.png";
       'ContentType' => mime_content_type($filepath),
   ));
} catch (S3Exception $e) {
   echo $e->getMessage() . "\n";
}

for more information AWS putObject

Community
  • 1
  • 1
urfusion
  • 5,528
  • 5
  • 50
  • 87
2

The problem is simple, Amazon S3 does not actually have directories.

The reality is that the slashes in object paths create the appearance of directories (and the AWS Console interface allows you to interact as if the objects are inside directories).

So, to create a directory, you must upload an object, in Git there are no directories, so users often create a file called .gitkeep to 'hold' a directory which shouldn't have any files in it committed. You could do something similar if you really don't want to push an actual file.

ThomasRedstone
  • 379
  • 1
  • 4
  • 13