简体   繁体   English

如何在不使用官方PHP SDK临时保存文件系统的情况下将base64编码的图像上传到Amazon S3?

[英]How to upload base64 encoded image to Amazon S3 without temporarily saving on file system with official PHP SDK?

I have a "screenshot" of a div using a javascript library named html2canvas . 我使用名为html2canvas的javascript库对div进行了“截屏”。 It returns the "screenshot" as base 64 encoded image which I then pass to my PHP server using AJAX. 它将“截屏”作为base 64编码的图像返回,然后我使用AJAX传递给我的PHP服务器。 The server will then temporarily save the image at the file system, upload the image to Amazon S3 and then immediately delete the file. 然后,服务器将图像临时保存在文件系统中,将图像上传到Amazon S3,然后立即删除文件。 It works, but I would like to see if it is possible to upload the image directly without saving to file system. 它可以工作,但是我想看看是否可以直接上传图像而不保存到文件系统。

I know that there is a way to directly upload image through client side javascript , but I don't want to enable CORS. 我知道有一种方法可以直接通过客户端javascript上传图像,但是我不想启用CORS。 Also, I have found a solution for Node.js , so I guess there got to be a way to do it in PHP as well. 另外,我已经找到了Node.js的解决方案 ,所以我想也必须有一种在PHP中实现的解决方案

I noticed that there are somebody asking similar question , but there are no answer to that question, and the situation is a bit different, as I am using the official PHP SDK with the official service provider for laravel 我注意到有人问类似的问题 ,但没有答案,情况有些不同,因为我将laravel的官方服务提供者官方PHP SDK一起使用

So, is there a way I can directly upload a base64 encoded image to Amazon S3 without temporarily saving on file system? 因此,有没有一种方法可以直接将base64编码的图像上传到Amazon S3,而无需临时保存在文件系统上?

Skip this step if your string is the correct output, but if you need to "save" from a GD image, then you can save the image to string using ob controls: 如果您的字符串是正确的输出,请跳过此步骤,但是如果您需要从GD图像中“保存”,则可以使用ob控件将图像保存到字符串中:

ob_start();
imagepng($image);
$pngData = ob_get_clean();
ob_end_clean();

Then you can simply specify Body to upload a string directly, and not a file. 然后,您只需指定“ Body即可直接上传字符串,而不是文件。

$aParams = [
    'Bucket'     => $bucketName,
    'Key'        => $destName,
    'Body'       => $string,
    'ACL'        => 'public-read'
]);
$result = $s3Client->putObject($aParams);

You can also optionally add image headers etc as you would for uploading the file. 您也可以选择添加图片标题等,就像上传文件一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM