【发布时间】:2014-07-16 21:02:45
【问题描述】:
我决定使用 AWS SDK 的包装器,这样我就可以方便地从我的 S3 存储桶中流式传输数据,但情况如下:
使用此代码写入我的存储桶有效
$client = \Aws\Common\Aws::factory(array(
'key' => 'my key',
'secret' => 'my secret',))
->get('S3');
$client->registerStreamWrapper();
$fh = fopen('s3://mybucket/test.txt','w');
fwrite($fh, 'Test');
fclose($fh);
此操作后文件存在,所以一切正常,但之后我无法读取它,因为fopen('s3://mybucket/test.txt', 'r') 失败并出现以下警告:
Warning: fopen(s3://mybucket/test.txt): failed to open stream: "Aws\S3\StreamWrapper::stream_open" call failed
...还有这个错误:
Fatal error: Uncaught exception 'Guzzle\Common\Exception\RuntimeException' with message 'Error creating resource. [type] 2 [message] fopen(https://mybucket.s3.amazonaws.com/test.txt): failed to open stream: Invalid argument [file] phar://path/to/aws.phar/Guzzle/Stream/PhpStreamRequestFactory.php [line] 217' in phar://path/to/aws.phar/Guzzle/Stream/PhpStreamRequestFactory.php on line 271
因此,我可以捕获该异常或改用getObject,但我仍然无法读取文件流,这违背了目的。也许这不是什么大事,但我对 Guzzle 和 AWS SDK 的内部工作原理并不熟悉。
任何关于我在这里做错的帮助或指南将不胜感激,我在谷歌上搜索了一段时间,但我找不到有用的信息来解决它,所以这很可能是由于我的无能。
提前致谢。
干杯。
【问题讨论】:
-
您的意思是从读取的 fopen() 中省略
'r'参数吗? -
@MarcB 忘了写在问题里(但它在代码里),感谢观察!
标签: php amazon-web-services amazon-s3 guzzle aws-php-sdk