0

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.