【问题标题】:Upload resized image on amazon S3 with PHP SDK使用 PHP SDK 在亚马逊 S3 上上传调整大小的图像
【发布时间】:2014-05-19 00:12:05
【问题描述】:

我有一个脚本可以调整图像大小和裁剪图像,我想将图像上传到我的亚马逊 S3 上。

问题是当我尝试运行我的脚本时收到一条错误消息,因为我猜源文件未被识别为来自磁盘的直接路径 ($filepath)。你有什么办法度过这个难关吗?

致命错误:在 phar:///var 中未捕获异常“Aws\Common\Exception\InvalidArgumentException”和消息“您必须为 Body 或 SourceFile 参数指定非空值。” /www/submit/aws.phar/Aws/Common/Client/UploadBodyListener.php:...

$myResizedImage = imagecreatetruecolor($width,$height);
        imagecopyresampled($myResizedImage,$myImage,0,0,0,0, $width, $height, $origineWidth, $origineHeight);

$myImageCrop = imagecreatetruecolor(612,612);
        imagecopy( $myImageCrop, $myResizedImage, 0,0, $posX, $posY, 612, 612);

//Save image on Amazon S3

     require 'aws.phar';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

$bucket = 'yol';
$keyname = 'image_resized';                    
$filepath = $myImageCrop;

// Instantiate the client.
$s3 = S3Client::factory(array(
    'key'    => 'private-key',
    'secret' => 'secrete-key',
    'region' => 'eu-west-1'

    ));

try {
    // Upload data.
    $result = $s3->putObject(array(
        'Bucket' => $bucket,
        'Key'    => $keyname,
        'SourceFile'   => $filepath,
        'ACL'    => 'public-read',
        'ContentType' => 'image/jpeg'
    ));

    // Print the URL to the object.
    echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

【问题讨论】:

  • 你能发布完整的代码吗?我也遇到了同样的错误。

标签: php file-upload amazon-web-services amazon-s3


【解决方案1】:

您需要将图像资源转换为包含图像数据的实际字符串。您可以使用此功能来实现:

function image_data($gdimage)
{
    ob_start();
    imagejpeg($gdimage);
    return(ob_get_clean());
}

【讨论】:

  • 对于那些不想保存文件的人来说是一个很好的解决方案。而不是SourceFile 使用'Body'=>image_data($img)
【解决方案2】:

您将上传的SourceFile 设置为$filepath,它是从$myImageCrop = imagecreatetruecolor(...) 分配的。因此,它实际上根本不是一条路径——它是一个 GD 图像资源。您不能直接将这些上传到 S3。

您需要将该图像写入文件(例如使用imagejpeg() + file_put_contents()),或者使用内存中的数据运行上传(同样,来自imagejpeg() 或类似文件)。

【讨论】:

  • 这意味着我必须将我的图像上传到我的 EC2 实例上,然后再上传到我的 S3 上?
  • 我试过了:$filepath = imagejpeg($myImageCrop); 没有成功
【解决方案3】:
     require 'aws.phar';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

$bucket = 'kkkk';
$keyname = 'test';
// $filepath should be absolute path to a file on disk  

$newFielName = tempnam(null,null); // take a llok at the tempnam and adjust parameters if needed
imagejpeg($myImageCrop, $newFielName, 100); // use $newFielName in putObjectFile()

$filepath = $newFielName;

// Instantiate the client.
$s3 = S3Client::factory(array(
    'key'    => 'jjj',
    'secret' => 'kkk',
    'region' => 'eu-west-1'

    ));

try {
    // Upload data.
    $result = $s3->putObject(array(
        'Bucket' => $bucket,
        'Key'    => $keyname,
        'SourceFile'   => $filepath,
        'ACL'    => 'public-read',
        'ContentType' => 'image/jpeg'
    ));

    // Print the URL to the object.
    echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-24
    • 2012-07-07
    • 2016-01-11
    • 2017-08-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    相关资源
    最近更新 更多