Using PHP to load in Amazon S3

I spent the last few hours after tutorials on uploading files to Amazon S3 using php. I downloaded the latest version of the Donovan Schönknecht S3 class to my server (like S3.php), and I am trying to use the following code to test download capabilities. I know this code will work because I have seen many examples in action.

<?php require('S3.php'); $s3 = new S3('KEY', 'SECRET KEY'); //insert into s3 $new_name = time() . '.txt'; S3::putObject( 'upload-me.txt', 'bucketName', $new_name, S3::ACL_PUBLIC_READ, array(), array(), S3::STORAGE_CLASS_RRS ); ?> 

When I try to load this page, a 500 server error occurs. In addition, every other authoritative textbook of this kind gave me the same 500 error.

I have confirmed that my key and secret key are valid by connecting to S3 using Cyberduck.

Does anyone know what I can do wrong?

Thanks,

Sean

+6
source share
7 answers

As it turned out, I was missing the cURL extension for PHP, and this caused a problem, because the S3 class I used required the use of cURL. Now everything works.

+8
source

You should also consider using the official AWS SDK for PHP . Examples of using S3 with the SDK can be found in the S3 user guide.

+8
source

You can download the latest version of the Amazon PHP SDK by running the following composer command

 composer require aws/aws-sdk-php 

Additional configuration for uploading a file on Amazon s3

 // Include the SDK using the Composer autoloader require 'vendor/autoload.php'; use Aws\S3\S3Client; use Aws\S3\Exception\S3Exception; // Set Amazon s3 credentials $client = S3Client::factory( array( 'key' => "your-key", 'secret' => "your secret key" ) ); try { $client->putObject(array( 'Bucket'=>'your-bucket-name', 'Key' => 'your-filepath-in-bucket', 'SourceFile' => 'source-filename-with-path', 'StorageClass' => 'REDUCED_REDUNDANCY' )); } catch (S3Exception $e) { // Catch an S3 specific exception. echo $e->getMessage(); } 

Get step-by-step details from here. Download Amazon S3 File Using PHP

+4
source

I did not find an updated Script with the latest Amazons sdk. I did it myself. it runs as a command line interpreter php script. try:

https://github.com/arizawan/aiss3clientphp

+1
source

I am not familiar with the S3 API, but I used it as a repository with https://github.com/KnpLabs/Gaufrette . Gaufrette is a library that provides a pretty good level of abstraction over S3 and other file services / systems.

0
source

Use this to upload images using a form and it works. Great for me you can try using it with code

 $name = $_FILES['photo']['name']; $size = $_FILES['photo']['size']; $tmp = $_FILES['photo']['tmp_name']; //////Upload Process // Bucket Name $bucket = 'bucket-name'; require_once('S3.php'); //AWS access info $awsAccessKey = 'awsAccessKey'; $awsSecretKey = 'awsSecretKey'; //instantiate the class $s3 = new S3($awsAccessKey, $awsSecretKey); $s3->putBucket($bucket, S3::ACL_PUBLIC_READ); //Rename image name. $actual_image_name = time(); //Upload to S3 if($s3->putObjectFile($tmp, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ) ) { $image='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name; }else{ echo 'error uploading to S3 Amazon'; } 
0
source

Here is sample code for uploading images to Amazon S3.

 // Bucket Name $bucket="BucketName"; if (!class_exists('S3'))require_once('S3.php'); //AWS access info if (!defined('awsAccessKey')) define('awsAccessKey', 'ACCESS_KEY'); if (!defined('awsSecretKey')) define('awsSecretKey', 'ACCESS_Secret_KEY'); $s3 = new S3(awsAccessKey, awsSecretKey); $s3->putBucket($bucket, S3::ACL_PUBLIC_READ); if($s3->putObjectFile($tmp, $bucket , $image_name_actual,S3::ACL_PUBLIC_READ) ) { $message = "S3 Upload Successful."; $s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name; echo "<img src='$s3file'/>"; echo 'S3 File URL:'.$s3file; } else{ $message = "S3 Upload Fail."; } } 
0
source

Source: https://habr.com/ru/post/943788/


All Articles