Pranav Jituri
Pranav Jituri

Reputation: 823

PHP Script not proceeding ahead with Amazon S3 Upload

I am trying to upload a file to the Amazon S3 using the AWS PHP SDK but whenever I try uploading, the script gives output till the PutObject operation but no output after that operation. I am not even able to dump the result object. The credentials are stored in the .aws in the root folder of my test machine (running Ubuntu). Below is the code -

$s3 = new Aws\S3\S3Client([
        'version' => 'latest',
        'region' => 'us-east-1'
    ]);

echo "<br />".realpath(UPLOAD_DIR . $apiKEY . "/" . $name);
        try {
        $result = $s3->putObject([
            'Bucket' => 'quicklyusercannedspeechbucket',
            'ContentType' => 'text/plain',
            'Key' => $apiKEY . "/" . $name,
            'SourceFile' => realpath(UPLOAD_DIR . $apiKEY . "/" . $name)
        ]);
         var_dump($result);
    }
    catch(\Aws\S3\Exception\S3Exception $e) {
        echo $e->getAwsErrorCode();
    }
    echo "Finished";
    var_dump($result);

When I run the above code, I don't get any output for the $result array. Any idea what might be happening?

Any help is appreciated ^_^

-Pranav

Upvotes: 0

Views: 72

Answers (2)

Zedd Index
Zedd Index

Reputation: 354

Use below code to view if your image is uploaded successfully

try {
    $result = $s3->putObject([
        'Bucket' => 'quicklyusercannedspeechbucket',
        'ContentType' => 'text/plain',
        'Key' => $apiKEY . "/" . $name,
        'SourceFile' => realpath(UPLOAD_DIR . $apiKEY . "/" . $name)
    ]);
    $s3file='http://quicklyusercannedspeechbucket.s3.amazonaws.com/'.$name;
     echo "<img src='$s3file'/>";
     echo 'S3 File URL:'.$s3file;
     var_dump($result);
}
catch(\Aws\S3\Exception\S3Exception $e) {
    echo $e->getAwsErrorCode();
}

You can get step by step detail from here Amazon S3 File Upload Using PHP

Upvotes: 1

Pranav Jituri
Pranav Jituri

Reputation: 823

It seems that I had put the credentials in the wrong folder. As per the NGINX, the default folder for storing the credentials was /var/www rather than the home directory of the user.

Also, I turned on the display_errors in the php.ini which helped me find that the AWS SDK was having problems with the credentials.

Thanks!

Upvotes: 0

Related Questions