douxianji6181 2013-02-27 09:23
浏览 171
已采纳

将二进制数据传递给S3 PutObject

I'm attempting to upload many images to an S3 bucket. With speed in mind, I'm looking to reduce the number of disk reads and writes by keeping the data in memory. To this end, I have come up with the following scheme:

//fetch binary image data from remote URL
$contents = file_get_contents("http://somesite.com/image.jpg");
//trim the image as per: http://stackoverflow.com/a/15104071/568884
$out = shell_exec('echo ' . base64_encode($contents) . " | base64 -d | convert - -fuzz 10% -trim jpeg:-");
//create a temporary resource to pass to S3's inputResource() method.
$resource = fopen('php://temp', 'r+');
//write the binary data into the empty resource.
fwrite($resource, $out);
//pass the resource and length of binary data into inputResource()
$ir = $this->s3->inputResource($resource, strlen($out));
//finally transfer the resource from machine to S3.
$this->s3->putObject($ir, $bucket, $s3_path, S3::ACL_PUBLIC_READ);

The error is: S3::putObject(): [RequestTimeout] Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed and data is not written to S3.

If I replace the assignment of $out to simply an empty string: $out = ""; Then, the library successfully writes 0 byte files to S3 as expected.

I'm using CodeIgniter S3 library... which is just a wrapper around AWS S3 API afaik.

  • 写回答

2条回答 默认 最新

  • duanniu3385 2013-03-10 12:21
    关注

    You are passing the file-handle $resource to the library however, you wrote into it first so that the file-pointer is at the end of the file.

    The library probably can not deal with that edge-case (it's source-code suggests that).

    You could try to rewind($resource) the file after writing to it but before passing it into the S3 library.

    And if you want to speed this up a little bit you can make PHP use memory for smaller files if you want by switching php://temp to php://memory. See the php:// wrapper docs for details and options.

    The S3 library btw. is not an official one. If you enable notices and warnings you will likely see some problems reported because it still contains PHP 4 code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog