Php S3图像文件的Post请求

Php S3图像文件的Post请求,php,jquery,image,post,amazon-s3,Php,Jquery,Image,Post,Amazon S3,我有一个php脚本,可以从AmazonS3中提取一个名为test.php 如果我像这样编写html,图像看起来很好 但是,当我尝试使用get请求并设置图像的SRC时,它不会正确显示。我最终想在使用post时传递参数。在检查src时,它充满了元数据,好像没有设置标题(“内容类型:图像”) $.get( "test.php",function( data ) { $('#image1').attr('src', data); }); My test.php脚本: require

我有一个php脚本,可以从AmazonS3中提取一个名为
test.php
如果我像这样编写html
,图像看起来很好

但是,当我尝试使用get请求并设置图像的SRC时,它不会正确显示。我最终想在使用post时传递参数。在检查
src
时,它充满了元数据,好像没有设置标题(“内容类型:图像”)

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

});
My 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'].'">'
require'../vendor/autoload.php';
使用Aws\S3\S3客户端;
$region='us-west-2';
$bucket='mybucket';
$key='mykey';
$secret='mysecret';
$s3Client=新的s3Client([
'版本'=>'最新',
“区域”=>$region,
“凭证”=>[
“key”=>$key,
'secret'=>$secret,
],
]);
$result=$s3Client->getObject(数组(
“Bucket”=>$Bucket,
'Key'=>'monkey1.jpg'
));
标题(“内容类型:图像”);
//通过索引到结果对象来打印结果体。
echo$result['Body'];
//回声“

获取对象URL并将其加载到页面

$result = $s3->get_object_url($bucket, 'monkey1.jpg', '1 minute');
从这里你可以加载到页面的图像

echo '<img src="'.$result.'">
echo'
这对我很有效

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);