使用PHP SDK在amazon S3上上载大小调整后的图像
我有一个脚本,其中调整大小和作物的形象,我想上传到我的亚马逊S3的形象在飞行 问题是,我在尝试运行脚本时收到一条错误消息,因为我猜sourcefile没有被识别为来自磁盘的直接路径($filepath)。你有什么办法来度过这个难关吗 致命错误:未捕获异常“Aws\Common\exception\InvalidArgumentException”,并显示消息“。必须为正文或源文件参数指定非空值。”phar:///var/www/submit/aws.phar/Aws/Common/Client/UploadBodyListener.php:...使用PHP SDK在amazon S3上上载大小调整后的图像,php,file-upload,amazon-web-services,amazon-s3,Php,File Upload,Amazon Web Services,Amazon S3,我有一个脚本,其中调整大小和作物的形象,我想上传到我的亚马逊S3的形象在飞行 问题是,我在尝试运行脚本时收到一条错误消息,因为我猜sourcefile没有被识别为来自磁盘的直接路径($filepath)。你有什么办法来度过这个难关吗 致命错误:未捕获异常“Aws\Common\exception\InvalidArgumentException”,并显示消息“。必须为正文或源文件参数指定非空值。”phar:///var/www/submit/aws.phar/Aws/Common/Client/
您正在将上载的
源文件设置为$filepath
,该文件是从$myImageCrop=imagecreatetruecolor(…)
分配的。因此,它实际上根本不是一条路径——它是一个GD图像资源。你不能直接上传到S3
您需要将该图像写入文件(例如使用imagejpeg()
+文件\u put\u contents()
),或者使用内存中的数据运行上载(同样,从imagejpeg()
或类似文件)。您需要将图像资源转换为包含图像数据的实际字符串。您可以使用此功能实现以下目的:
function image_data($gdimage)
{
ob_start();
imagejpeg($gdimage);
return(ob_get_clean());
}
这意味着我必须在EC2实例上上传图像,然后在S3上上传图像?我尝试了:$filepath=imagejpeg($myImageCrop)代码>如果没有成功,你可以发布完整的代码吗?我面临着同样的错误。对于那些不想保存文件的人来说,这是一个很好的解决方案。使用'Body'=>图像数据($img)
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";
}
function image_data($gdimage)
{
ob_start();
imagejpeg($gdimage);
return(ob_get_clean());
}