drvlf9739 2016-11-21 02:53
浏览 39

获取远程映像并上载到AWS S3

I'm trying to fetch a remote image, resize it, and upload it to AWS S3, and I'm getting an error that I don't know how to resolve.

Here's my code:

//Fetch and resize remote image
$max_width = 800;
$remote_img = file_get_contents("https://commons.wikimedia.org/wiki/Stovall_House#/media/File:Tampa_Stovall_House_gazebo01.jpg");
$im = imagecreatefromstring($remote_img);
$remote_img_width = imagesx($im);
$remote_img_height = imagesy($im);
if($remote_img_width<=$max_width){
    $saved_img_width = $remote_img_width;
    $saved_img_height = $remote_img_height;
    } else {
    $saved_img_width = $max_width;
    $ratio = $max_width/$remote_img_width;
    $saved_img_height = $remote_img_height * $ratio;
    }
$saved_image = imagecreatetruecolor($saved_img_width, $saved_img_height);
imagecopyresized($saved_image , $im, 0, 0, 0, 0, $saved_img_width, $saved_img_height, $remote_img_width, $remote_img_height);

//Upload to AWS S3
require 'aws/aws-autoloader.php';
use Aws\S3\S3Client;
$s3 = new Aws\S3\S3Client([
    'version'     => 'latest',
    'region'      => 'us-east-1',
    'credentials' => [
        'key'    => 'ABCD1234',
        'secret' => 'ABCD1234'
        ]
    ]);
$result = $s3->putObject(array(
        'Bucket' => 'aws-website-virulous-bebmb',
        'Key'    => 'ABCD1234',
        'Body'   => $saved_image,
        'ACL'    => 'public-read'
    ));

// Get the URL the object can be downloaded from and display
$image_url = $result['ObjectURL'];
echo "The image can be seen at <a href='" . $image_url . "' target='_new'>" . $image_url . "</a>";

//Clean up
imagedestroy($saved_image); 
imagedestroy($im);

And here's the error I'm getting:

Fatal error: Uncaught InvalidArgumentException: Found 1 error while validating the input provided for the PutObject operation: [Body] must be an fopen resource, a GuzzleHttp\Stream\StreamInterface object, or something that can be cast to a string.

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dsc6517 2016-11-21 05:13
    关注

    Well it would be more beneficial to suggest to you that you take your error message, and perform a search on Google, as there are Lots of HITS and lots of explanations.

    One of the suggestions in the error message

    [Body] must be an fopen resource

    Which Hints that you need to have saved your final image and open it using fopen passing the handle to body instead of $saved_image.

    There's a few references you can hunt down regarding this on Google and in the AWS API.

    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致