2016-10-04 3 views
3

아마존 S3에서 이미지 파일을 가져 오기위한 PHP 스크립트가 있습니다. <img src="test.php"> 이미지를 잘 나타내려면 test.php 이라고 입력하면됩니다.S3 이미지 파일에 대한 게시물 요청

그러나 요청을 가져 와서 이미지의 SRC를 설정하려고하면 올바르게 표시되지 않습니다. 결국이 게시물을 사용하여 params를 전달하고 싶습니다. src을 검사 할 때 헤더 ("Content-type : image")가 설정되지 않은 것처럼 메타 데이터로 채워집니다.

$.get("test.php",function(data) { 
    $('#image1').attr('src', data);  

}); 

내 test.php 스크립트 :

require '../vendor/autoload.php'; 
use Aws\S3\S3Client; 

$region = 'us-west-2'; 
$bucket = 'mybucket'; 
$key = 'mykey'; 
$secret = 'mysecret'; 


$s3Client = new S3Client([ 
    'version'  => 'latest', 
    'region'  => $region, 
    'credentials' => [ 
     'key' => $key, 
     'secret' => $secret, 
    ], 
]); 

$result = $s3Client->getObject(array(
    'Bucket' => $bucket, 
    'Key' => 'monkey1.jpg' 

)); 
header("Content-type: image"); 
// Print the body of the result by indexing into the result object. 
echo $result['Body']; 
//echo '<img src="'.$result['Body'].'">' 

답변

1

이것은 나를 위해 일했습니다.

PHP 스크립트

$s3Client = new S3Client([ 
    'version'  => 'latest', 
    'region'  => $region, 
    'credentials' => [ 
     'key' => $key, 
     'secret' => $secret, 
    ], 
]); 

$s3Client->registerStreamWrapper(); 
if (file_exists('s3://'.$bucket.'/'.$id.'/'.$filepath)) { 
    //echo 'It exists!'; 
    $result = $s3Client->getObject(array(
    'Bucket' => $bucket, 
    //'Key' => 'monkey1.jpg' 
    'Key' => $id.'/'.$filepath 
    )); 
    header("Content-type: image/*"); 
    // Print the body of the result by indexing into the result object. 
    echo base64_encode($result['Body']); 
} 

JS

$('#image').attr('src', 'data:image/*;base64,'+data); 
1

당신이 페이지에 이미지를로드 할 수있는 객체의 URL 시간 수 및 페이지이에서

$result = $s3->get_object_url($bucket, 'monkey1.jpg', '1 minute'); 

에로드!

echo '<img src="'.$result.'">