2011-09-12 6 views
6

J'essaie de faire en sorte que mon script montrera test.jpg dans un seau Amazon S3 via PHP. Voici ce que j'ai jusqu'à présent:Comment télécharger un fichier avec php et Amazon S3 sdk?

require_once('library/AWS/sdk.class.php'); 

$s3 = new AmazonS3($key, $secret); 

$objInfo = $s3->get_object_headers('my_bucket', 'test.jpg'); 
$obj = $s3->get_object('my_bucket', 'test.jpg', array('headers' => array('content-disposition' => $objInfo->header['_info']['content_type']))); 

echo $obj->body; 

Ce juste dumps les données du fichier sur la page. Je pense que je dois également envoyer l'en-tête content-disposition, ce que je pensais être fait dans la méthode get_object(), mais ce n'est pas le cas.

Note: J'utilise le SDK disponible ici: http://aws.amazon.com/sdkforphp/

+0

Je ne suis pas au courant de cette bibliothèque S3 PHP (qui est-ce? Pourrait aider si vous avez mentionné que, dans la question, il y en a plusieurs.) Mais le fait que vous fardiez speficiellement 'obj-> body' me fait penser que l'objet que vous récupérez a probablement des en-têtes et des corps séparés, et que vous devez peut-être faire écho à un en-tête Content-Type d'après les informations de '$ obj-> header', ou quelque chose comme ça? –

+0

J'utilise http://aws.amazon.com/sdkforphp/ – doremi

Répondre

12

eu à travailler par echo'ing sur l'en-tête de type de contenu avant echo'ing le corps de l'objet $.

$objInfo = $s3->get_object_headers('my_bucket', 'test.jpg'); 
$obj = $s3->get_object('my_bucket', 'test.jpg'); 

header('Content-type: ' . $objInfo->header['_info']['content_type']); 
echo $obj->body; 
+1

content-disposition est un indice pour le navigateur comment gérer le fichier. inline, attachment, etc ... content-type définit ce qu'est le fichier. (image, document, texte, etc ..) –

+0

- @ Doremi merci pour la résolution de ce problème, je luttais depuis un certain temps avec elle ... –

+0

@MarcB comment je envoyer ce fichier en pièce jointe dans le courrier .. pouvez-vous aide moi plz .. j'utilise PHP SDK de aws –

1

J'ai ajouté l'en-tête Content-Disposition à getAuthenticatedUrl();

// Example 
$timeOut = 3600; // in seconds 
$videoName = "whateveryoulike"; 
$headers = array("response-content-disposition"=>"attachment"); 
$downloadURL = $s3->getAuthenticatedUrl(FBM_S3_BUCKET, $videoName, FBM_S3_LIFETIME + $timeOut, true, true, $headers); 
8

Ces deux méthodes fonctionnent pour moi. La première façon semble plus concise.

$command = $s3->getCommand('GetObject', array(
     'Bucket' => 'bucket_name', 
     'Key' => 'object_name_in_s3' 
     'ResponseContentDisposition' => 'attachment; filename="'.$my_file_name.'"' 
    )); 

    $signedUrl = $command->createPresignedUrl('+15 minutes'); 
    echo $signedUrl; 
    header('Location: '.$signedUrl); 
    die(); 

Ou d'une manière plus verbeuse mais toujours fonctionnelle.

$object = $s3->getObject(array(
    'Bucket' => 'bucket_name', 
    'Key' => 'object_name_in_s3' 
    )); 

    header('Content-Description: File Transfer'); 
    //this assumes content type is set when uploading the file. 
    header('Content-Type: ' . $object->ContentType); 
    header('Content-Disposition: attachment; filename=' . $my_file_name); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate'); 
    header('Pragma: public'); 

    //send file to browser for download. 
    echo $object->body; 
+0

Merci pour le partage. – Brotheryura

+0

Merci! travaillé comme un charme. –

+0

merci @ MatteoStohlman nd @Maximus .. mais comment puis-je envoyer ce fichier en pièce jointe dans le courrier .. pouvez-vous m'aider plz .. –

1

Pour PHP SDK3 changer la dernière ligne de Maximus répondre

$object = $s3->getObject(array(
     'Bucket' => 'bucket_name', 
     'Key' => 'object_name_in_s3' 
    )); 

    header('Content-Description: File Transfer'); 
    //this assumes content type is set when uploading the file. 
    header('Content-Type: ' . $object->ContentType); 
    header('Content-Disposition: attachment; filename=' . $my_file_name); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate'); 
    header('Pragma: public'); 

    //send file to browser for download. 
    echo $object["Body"]; 
Questions connexes