2012-05-19 22 views
5

Sto imparando Amazon S3 usando S3 PHP Class. Ho caricato tutti i miei file sul mio bucket S3, ora voglio creare collegamenti per ogni file disponibile nel mio bucket.Come creare un link per il download per un oggetto bucket Amazon S3?

La funzione seguente potrebbe funzionare per me?

public static function getAuthenticatedURL($bucket, $uri, $lifetime, $hostBucket = false, $https = false) 
{ 

} 

    $s3 = new S3('access-key', 'secret-key'); 
    $s3->getAuthenticatedURL($bucket, $uri, $lifetime, $hostBucket = false, $https = false); 

O un'altra funzione come get_object_url, ma get_object_url() non nella mia classe S3.

Sto utilizzando Amazon S3 PHP class di Undesigned.

+0

possibile duplicato di [Come faccio a scaricare un file con PHP e il sdk Amazon S3?] (http: // stacko verflow.com/questions/7389394/how-do-i-download-a-file-with-php-and-the-amazon-s3-sdk) –

risposta

10

I seguenti modelli sono validi per la costruzione degli URL S3:

http(s)://<bucket>.s3.amazonaws.com/<object> 
http(s)://s3.amazonaws.com/<bucket>/<object> 
+2

Non può essere utilizzato per il download. http://stackoverflow.com/questions/7389394/how-do-i-download-a-file-with-php-and-the-amazon-s3-sdk –

+2

NOTA! Questo non funzionerà per te se hai sicurezza sul tuo secchio –

4

Se si desidera che il pubblico per accedere al secchio, è semplice come

http: // [YourBucketName] .s3.amazonaws.com/[yourfilename]

Finché è possibile impostare manualmente autorizzazioni corrette.

Se sei preoccupato dell'abuso di download, ti consigliamo un URL autenticato (che immagino tu voglia dal codice sammple). In tal caso, ti suggerisco di utilizzare l'SDK di Amazon: http://aws.amazon.com/sdkforphp/ in quanto contiene esempi di ciò che ti serve.

$s3->getObjectUrl($bucket, $filename, '5 minutes'); 

Documenti: http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_getObjectUrl

+1

'get_object_url()' funzione non lavorare con la mia libreria, cosa fare adesso. – Frank

+0

Funzione non funzionante con lib –

+1

Aggiornato al metodo pubblico corrente "getObjectUrl" http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_getObjectUrl – Robbie

0

vedo che le persone hanno già risposto a questo, ma ho voluto aggiungere un po 'di contesto per coloro chi può avere un secchio protetto (richiede l'accesso). Nota, non è necessario generare gli URL se si parla direttamente al bucket S3, quindi è possibile utilizzare 'file_get_contents' ecc. Ma è molto più lento in quanto non è possibile utilizzare richieste di arricciatura multipla (per velocità). Comunque potresti usare pthreads se hai una versione php più recente.

INSTALLAZIONE: Installare il file di classe S3 per Amazon, ci sono modi semplici per aggiungerlo usando il compositore o semplicemente scaricando il file S3.php manualmente.

non assistiti: (vedi altri post su questo argomento, appena fondamentalmente utilizzare l'URL)

http(s)://<bucket>.s3.amazonaws.com/<object> 
http(s)://s3.amazonaws.com/<bucket>/<object> 

HTTPS sicuro (quando si protegge la benna):

https://amazon.com/file/you/wanted.xxx?ID:XXXXX?SIG:YYYYY 

(1) Crea un https: // url e usa lo strumento multi curl per ottenerli tutti nello stesso momento (raccomandato).

Un esempio semplice:

$url = /path/to_the/file_name/file.ext 

//note check amazon to confirm the path which will contain only "_" and no spaces. 

$s3 = new S3($awsAccessKeyID, $awsSecretKey); 
$curls[] = $s3->get_object_url($bucketName, $uri, '1 hour'); 
var_dump($results = multiCurlRequest($curls));  

maggiori informazioni:

http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.S3.S3Client.html#_getObjectUrl http://undesigned.org.za/2007/10/22/amazon-s3-php-class/documentation

FYI:

function multiCurlRequest($curlList = array(),$user = '', $pass = '',$timeout = self::MULTI_REQ_TIMEOUT_SECS, $retTxfr = 1) { 

    if (empty($curlList) || count($curlList) == 0) return false; 

    $master = curl_multi_init(); 
    $node_count = count($curlList); 

    for ($i = 0; $i < $node_count; $i++) { 
     $ch[$i] = curl_init($curlList[$i]); 
     curl_setopt($ch[$i], CURLOPT_TIMEOUT, $timeout); // -- timeout after X seconds 
     curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, $retTxfr); 
     curl_setopt($ch[$i], CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
     curl_setopt($ch[$i], CURLOPT_USERPWD, "{$user}:{$pass}"); 
     curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true); 
     curl_multi_add_handle($master, $ch[$i]); 
    } 

    // -- get all requests at once, finish when done or timeout met -- 
    do { curl_multi_exec($master, $running); } 
    while ($running > 0); 

    $results = array(); 

    // -- get results from requests -- 
    for ($i = 0; $i < $node_count; $i++) { 
     $results[$i] = curl_multi_getcontent($ch[$i]); 
     if ((int) curl_getinfo($ch[$i], CURLINFO_HTTP_CODE) > 399 || empty($results[$i])) { 
      $this->set_request( [ ['label' => '404', 'href' => $results[$i], '404' => '1' ] ]); 
      unset($results[$i]); 
     } 
     curl_multi_remove_handle($master, $ch[$i]); 
     curl_close($ch[$i]); 
    } 

    curl_multi_close($master); 
    if (empty($results)) return false; 
    //$results = array_values($results); // -- removed as we want the original positions 
    return $results; 
} 
Problemi correlati