使用s3文件使用Guzzle PHP进行多部分/表单数据上传



我试图采取一个文件托管在Amazon S3上,并使用Content-Type: multipart/form-data post请求将其上传到另一个服务器。我可以使用Curl --form来实现这一点,但我不确定如何让S3文件像本地文件一样运行,以便我可以这样做。

curl -F "file=@localfile;filename=nameinpost" url.com

我已经设置了guzzle,这样我就可以像这样使用流包装器

$this->guzzleClient->registerStreamWrapper(); $data = file_get_contents('s3://buck/test-file.jpg');

如果我能让这个流工作,那将是伟大的,还是只能使用本地文件?做这类事情的最佳方式是什么?

使用Guzzle的PHP AWS SDK为S3提供了一个专门的流包装器。

我想到了这样的东西。

//...configure an s3client
$this->s3client->registerStreamWrapper();
$filename = sys_get_temp_dir() .'/'. $unique_name;
$handle = fopen($filename, 'w+');
$bytes_written = fwrite( $handle, file_get_contents('s3://bucket/test-file.jpg'));
//...configure a Guzzle client
//Guzzle 3.0
$request = $this->client->post('image')->addPostFile('images', $filename);
$response = $request->send();
//Guzzle 4.0
$request = $this->client->createRequest('POST', 'images');
$request->getBody()->addFile(new PostFile('image', $handle));
$response = $this->client->send($request);
//remove the file
unlink($filename);
Guzzle 4.0也有一些新的特性,允许直接使用流,而不必创建这个临时文件。然而,我不能得到这个工作与服务,期待一个基于表单的文件上传以下http://www.faqs.org/rfcs/rfc1867.html。如果可以使用流,那就更好了!

更多细节见"更好的POST文件支持"这里http://mtdowling.com/blog/2014/03/15/guzzle-4-rc,从版本3到4的过渡确实给我带来了一些困惑。确保你知道你的客户端是什么版本,并且你正在阅读该版本的文档:)

最新更新