Krishna Ballabh Gupta
Krishna Ballabh Gupta

Reputation: 45

In AWS S3 Bucket Uploading images as 0 Bytes

I am trying to upload images in AWS s3 bucket but all times its uploading 0 bytes in the bucket.

I really don't know why it's happening like this.

Anyone is here experienced the same issue

Here is the snapshot of image and code https://www.awesomescreenshot.com/image/3369121/5ac921c28f569f45d1b51ac2ce7b6da1

use Aws\S3\S3Client;



public function aws_upload(){

            require 'vendor/autoload.php';
            if(isset($_FILES["left_front"]) && $_FILES["left_front"]["error"] == 0){
                        $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
                        $filename = $_FILES["left_front"]["name"];
                        $filetype = $_FILES["left_front"]["type"];
                        $filesize = $_FILES["left_front"]["size"];
            }

            // Instantiate an Amazon S3 client.

            $s3 = new S3Client([
                'version' => 'latest',
                'region'  => AWS_REGION,
                'credentials' => [
                    'key'    => ACCESS_KEY_ID,
                    'secret' => SECRET_ACCESS_KEY
                ]
            ]);
            $bucketName = 'demokrishna';
            $key = basename($filename);
            try {
                $result = $s3->putObject([
                    'Bucket' => $bucketName,
                    'Key'    => $key,
                    'acl'   => 'public-read'
                  //  'ACL'    => 'public-read',
                ]);
                echo $result->get('ObjectURL');

            } catch (Aws\S3\Exception\S3Exception $e) {
                echo "There was an error uploading the file.\n";
                echo $e->getMessage();
            }
        }

Upvotes: 2

Views: 7531

Answers (2)

Krishna Ballabh Gupta
Krishna Ballabh Gupta

Reputation: 45

$key_bucket = basename($filename);
    $result = $s3->putObject([
                                'Bucket' => $bucketName, // bucketname
                                'Key'    => $key_bucket, 
                                'StorageClass' => 'REDUCED_REDUNDANCY',
                                'SourceFile' => $value['tmp_name'] //temporary path

                            ]);

It works for me

Upvotes: 0

jarmod
jarmod

Reputation: 78553

You have supplied bucket, key, and ACL but you have not supplied the file contents. You can do this using either a Body or a SourceFile.

Example with Body:

$result = $s3Client->putObject([
    'Bucket'     => $bucket,
    'Key'        => $key,
    'Body'       => 'Hello world!',
    'ACL'        => 'public-read'
]);

Example with SourceFile:

$file_Path = 'folder/filename.jpg';

$result = $s3Client->putObject([
    'Bucket'     => $bucket,
    'Key'        => $key,
    'SourceFile' => $file_Path,
    'ACL'        => 'public-read'
]);

Also, note that it's 'ACL', not 'acl'. Please read the putObject documentation.

Upvotes: 4

Related Questions