2013-02-19 15 views
6

Questo mi sta facendo impazzire: ho lavorato su questo problema per diversi giorni con scarso successo. Ho finalmente colpito un muro di mattoni e ho bisogno di aiuto. Molti degli articoli e dei forum che ho cercato non sono per AWSSDK per PHP 2.Caricamento di un'immagine tramite AWS SDK per PHP 2

Abbiamo utilizzato S3 di Amazon per caricare immagini tramite iOS negli ultimi due anni.

Ora, ho bisogno di implementare il caricamento in un browser.

Ho scaricato e installato con successo AWSSDK per PHP 2 sul nostro server Ubuntu. Posso collegarmi al nostro account AWS S3 e visualizzare il contenuto dei bucket. Ma non riesco a mettere un'immagine in un secchio.

L'eccezione da AWS è:
Aws \ S3 \ Exception \ NotImplementedException: AWS Codice di errore: NotImplemented, Codice di stato: 501, AWS Request ID: CEDC4BBAA83CF70C, AWS Il tipo di errore del server, il messaggio di errore AWS: Un colpo di testa che si fornito implica funzionalità che non è implementata.

Ecco l'URL che ho ottenuto il codice di esempio riportato di seguito da, sotto il titolo chiamato caricamento di un file di Amazon S3: https://github.com/aws/aws-sdk-php#quick-start

E ho aggiornato il mio codice in base a questo: AWS PHP SDK Version 2 S3 putObject Error

Ma ancora non funziona

Ecco il mio codice:

<?php 
require_once("../config.php");  //local config vars 
require_once('AWSSDKforPHP/aws.phar'); 

use Aws\S3\S3Client; 
use Aws\Common\Enum\Region; 
use Aws\Common\Aws; 
use Aws\S3\Enum\CannedAcl; 
use Aws\S3\Exception\S3Exception; 
use Guzzle\Http\EntityBody; 

//get the $s3 object 
$config = array(
    'key' => AMAZON_ACCESS_KEY, 
    'secret' => AMAZON_ACCESS_SECRET, 
    'region' => Region::US_EAST_1  
); 
$s3 = S3Client::factory($config); 


try { 
    $bucketname = 'my_bucket_name';   //my bucket name on s3 
    $filename = 'filename.jpg';    //my image on my server 
    $path = 'http://my.website.com/app/cache/remote';  //the path where the image is located 
    $fullfilename = $path."/".$filename; 

    //this successfully lists the contents of the bucket I am interested in 
    foreach ($s3->getIterator('ListBuckets') as $bucket) { 
     foreach ($s3->getIterator('ListObjects', array('Bucket' => $bucket['Name'])) as $object) { 
      if ($bucket['Name'] == $bucketname) { 
       echo $bucket['Name'] . '/' . $object['Key'] . PHP_EOL; 
      } 
     } 
    } 

    //HERE ME HERE, PLEASE! this is the code that throws the exception 
    $s3->putObject(array(
     'Bucket' => $bucketname, 
     'Key' => $filename, 
     'Body' => EntityBody::factory(fopen($fullfilename, 'r')), 
     'ACL' => CannedAcl::PUBLIC_READ_WRITE, 
     'ContentType' => 'image/jpeg' 
    )); 


} catch (S3Exception $e) { 
    echo $e; 
} 

?> 

Qualcuno può fornire me con un esempio in modo da poter caricare l'immagine JPG nella nostra secchio S3 utilizzando AWSSDK per PHP 2?

Risoluzione: Dalla risposta di ppostma1, ho modificato il mio codice come segue, e ora lavora:

$bucketname = 'my_bucket_name'; //must be all lowercase 
$filename = 'filename.jpg'; //my image on my server 
$path = 'var/www/html/root-website-folder/images/'; //the physical path where the image is located 
$fullfilename = $path.$filename; 

$s3->putObject(array(
     'Bucket' => $bucketname, 
     'Key' => $filename, 
     'Body' => EntityBody::factory(fopen($fullfilename, 'r')), 
     'ACL' => CannedAcl::PUBLIC_READ_WRITE, 
     'ContentType' => 'image/jpeg' 
)); 
+0

stai usando un proxy? – ppostma1

risposta

5

In primo luogo, vi manca un s3 'filename' alias chiave:

'Key' => '/files/imgage/fuzzykitten.jpg' 

Avanti, ho avuto molte meno complicazioni con:

//use Aws\S3\S3Client; 
use Aws\Common\Enum\Region; 
//use Aws\Common\Aws; 
use Aws\S3\Enum\CannedAcl; 
use Aws\S3\Exception\S3Exception; 
use Guzzle\Http\EntityBody; 


$amazon = Aws\S3\S3Client::factory($config) 

con l'essere in grado di trovare t lui file di classe. Ogni volta che tento di includere ./common/aws o ./s3/s3client, inizia a darmi "impossibile trovare Aws \ S3 \ Aws \ S3Client" che mi lascia in wt ???

+0

Grazie, ppostma1. La tua risposta mi ha indirizzato nella giusta direzione. Ho la chiave nell'esempio sopra, ma stavo facendo 2 errori con il mio test. 1) Il nome del secchio DEVE essere lettere minuscole. Il mio secchio di prod è il mio secchio di sviluppo era superiore e inferiore (perché ti permettono di creare un nome di secchio che non puoi usare?). 2) Stavo usando l'URL per accedere al nome del file, ma dal tuo post mi sono reso conto che avevo bisogno di utilizzare il percorso fisico completo sul server del file. – user2089042

+0

ah !, quando non ho visto un percorso presumevo che non ci fosse una chiave. Ora per chiunque colpire questo in su in cerca di un proxy, il codice aggiuntivo è: $ configs = array ( 'chiave' => AWS_KEY, 'segreto' => AWS_SECRET_KEY, ricciolo '.Opzioni => array ( CURLOPT_PROXY => 'proxyHostName', CURLOPT_PROXYPORT => PORT #, CURLOPT_PROXYUSERPWD => 'Username: Password', CURLOPT_HTTPHEADER => array ("Expect:"), CURLOPT_SSL_VERIFYPEER => FALSE ) ); $ amazon = Aws \ S3 \ S3Client :: factory ($ configs); – ppostma1

+0

CURLOPT_HTTPHEADER => array ("Expect:"), è necessario solo per i server "Squid", e se si dispone di quelli potrebbe essere necessario il nome utente del dominio MS che sarebbe quindi "domainhost \ username: Password" – ppostma1

Problemi correlati