无法使用图像的直接 url 将图像上传到 S3 存储桶 [英] Can't upload image to S3 bucket using direct url of image

查看:39
本文介绍了无法使用图像的直接 url 将图像上传到 S3 存储桶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,它适用于表单上传(通过 $_FILES)(我省略了那部分代码,因为它无关紧要):

Here is my code, which works for forms upload (via $_FILES) (I'm omitting that part of the code because it is irrelevant):

$file = "http://i.imgur.com/QLQjDpT.jpg";

$s3 = S3Client::factory(array(
    'region' => $region,
    'version' => $version
));  

        try {

            $content_type = "image/" . $ext;

            $to_send = array();

            $to_send["SourceFile"] = $file;

            $to_send["Bucket"] = $bucket;
            $to_send["Key"] = $file_path;
            $to_send["ACL"] = 'public-read';
            $to_send["ContentType"] = $content_type;

            // Upload a file.
            $result = $s3->putObject($to_send);

正如我所说,如果文件是 $_FILES["files"]["tmp_name"] 则此方法有效,但如果 $file 是有效图像 url 且未捕获异常 'Aws\Exception\CanNotCreateChecksumException' 带有消息 'A sha256 校验和无法为提供的上传正文计算,因为它不可查找.为防止出现此错误,您可以 1) 在请求中包含 ContentMD5 或 ContentSHA256 参数,2) 为正文使用可搜索流,或 3) 将不可搜索流包装在 GuzzleHttp\Psr7\CachingStream 对象中.但是您应该小心并记住 CachingStream 使用 PHP 临时流.这意味着流将临时存储在本地磁盘上.'.有谁知道为什么会这样?可能会关闭什么?Tyvm 为您提供帮助!

As I said, this works if file is a $_FILES["files"]["tmp_name"] but fails if $file is a valid image url with Uncaught exception 'Aws\Exception\CouldNotCreateChecksumException' with message 'A sha256 checksum could not be calculated for the provided upload body, because it was not seekable. To prevent this error you can either 1) include the ContentMD5 or ContentSHA256 parameters with your request, 2) use a seekable stream for the body, or 3) wrap the non-seekable stream in a GuzzleHttp\Psr7\CachingStream object. You should be careful though and remember that the CachingStream utilizes PHP temp streams. This means that the stream will be temporarily stored on the local disk.'. Does anyone know why this happens? What might be off? Tyvm for your help!

推荐答案

您必须首先将文件下载到运行 PHP 的服务器.S3 上传仅适用于本地文件 - 这就是 $_FILES["files"]["tmp_name"] 工作的原因 - 它是 PHP 服务器本地的文件.

You have to download the file to the server where PHP is running first. S3 uploads are only for local files - which is why $_FILES["files"]["tmp_name"] works - its a file that's local to the PHP server.

这篇关于无法使用图像的直接 url 将图像上传到 S3 存储桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆