php - 上传base64图片到amazon s3

标签 php image amazon-s3 upload base64

我在尝试将图像上传到 AWS S3 时遇到了一些问题。它似乎正确上传了文件,但是,每当我尝试下载或预览时,它都无法打开。目前,这是我正在使用的上传代码:

<?php
require_once 'classes/amazon.php';
require_once 'includes/aws/aws-autoloader.php';
use Aws\S3\S3Client;

$putdata = file_get_contents("php://input");
$request = json_decode($putdata);

$image_parts = explode(";base64,", $request->image);
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = $image_parts[1];

$dateTime = new DateTime();
$fileName = $dateTime->getTimestamp() . "." . $image_type;  

$s3Client = S3Client::factory(array(
    'region' => 'eu-west-1',
    'version' => '2006-03-01',
    'credentials' => array(
        'key'    => Amazon::getAccessKey(),
        'secret' => Amazon::getSecretKey(),
    )
));

try {
    $result = $s3Client->putObject(array(
        'Bucket'          => Amazon::getBucket(),
        'Key'             => 'banners/' . $fileName,
        'Body'            => $image_base64,
        'ContentType'     => 'image/' . $image_type,
        'ACL'             => 'public-read'
    ));
    echo $result['ObjectURL'] . "\n";
} catch(S3Exception $e) {
    echo $e->getMessage() . "\n";
}
?>

因此,当我在上传图像文件后检查控制台时,它具有预期的大小、权限和 header ,但正如我所说,每当我尝试打开文件时,它都会失败。

这可能是什么问题?提前致谢。

最佳答案

这里的问题是您似乎正在上传图像的 base64 编码版本,而不是图像的原始字节。获取 $image_base64 并首先解码为原始字节 http://php.net/manual/en/function.base64-decode.php .我敢肯定,如果您尝试在文本编辑器中打开这些“图像”,您会看到 base64 十六进制数据。

关于php - 上传base64图片到amazon s3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42738578/

相关文章:

amazon-web-services - Base64解码编码,得到不同的数据

PHP 酒吧定位器 邮政编码 MYSQL

php - 如何制作一个 if 语句来检查一个变量是否在 mysql 数据库中

javascript - 浏览器渲染包含 img 的 5600 div 网格时速度缓慢

javascript - 设置加载时间过长时交换图像的条件

javascript - AWS4 签署 S3 PUT 请求

javascript - 使用 rel ="follow"设置所有外部链接时,如何手动将 rel ="nofollow"添加到特定域?

php - 如何将 MySQL DateTime 转换为 PHP Y-m-d?

html - 图像缩放后如何修复父元素大小?

scala - Spark Scala S3 存储 : permission denied