如何将url内容存储在变量中并上传到Amazon S3


How to store url contents in variable and upload to Amazon S3?

所以,我一直试图让这个工作在过去的几个小时,但我不能弄清楚。目标是从gfycat提取转换后的mp4文件,并将该文件上传到Amazon S3存储桶。

gfycat返回一个JSON对象正确,和$result->mp4Url返回一个正确的url到mp4文件。我不断得到错误,如"对象预期,字符串给定"。什么好主意吗?谢谢。

// convert the gif to video format using gfycat
$response = file_get_contents("http://upload.gfycat.com/transcode/" . $key . 
     "?fetchUrl=" . urlencode($upload->getUrl('g')));
$result = json_decode($response);
$result_mp4 = file_get_contents($result->mp4Url);
// save the converted file to AWS S3 /i
$s3->putObject(array(
     'Bucket'     => getenv('S3_BUCKET'),
     'Key'        => 'i/' . $upload->id64 . '.mp4',
     'SourceFile' => $result_mp4,
));

var_dump(响应)美元收益率:

string '{
    "gfyId":"vigorousspeedyinexpectatumpleco",
    "gfyName":"VigorousSpeedyInexpectatumpleco",
    "gfyNumber":"884853904",
    "userName":"anonymous",
    "width":250,
    "height":250,
    "frameRate":11,
    "numFrames":67,
    "mp4Url":"http:'/'/zippy.gfycat.com'/VigorousSpeedyInexpectatumpleco.mp4",
    "webmUrl":"http:'/'/zippy.gfycat.com'/VigorousSpeedyInexpectatumpleco.webm",
    "gifUrl":"http:'/'/fat.gfycat.com'/VigorousSpeedyInexpectatumpleco.gif",
    "gifSize":1364050,
    "mp4Size":240833,
    "webmSize":220389,
    "createDate":"1388777040",
    "views":"205",
    "title":'... (length=851)

对其使用json_decode()也会产生类似的结果。

您将'SourceFile'参数(接受文件路径)与'Body'参数(接受原始数据)混淆了。更多示例请参见《AWS SDK for PHP用户指南》中的上传对象。

这里有两个选项应该可以工作:

选项1(使用SourceFile)

// convert the gif to video format using gfycat
$response = file_get_contents("http://upload.gfycat.com/transcode/" . $key . 
     "?fetchUrl=" . urlencode($upload->getUrl('g')));
$result = json_decode($response);
// save the converted file to AWS S3 /i
$s3->putObject(array(
    'Bucket'     => getenv('S3_BUCKET'),
    'Key'        => 'i/' . $upload->id64 . '.mp4',
    'SourceFile' => $result->mp4Url,
));

选项2(使用Body)

// convert the gif to video format using gfycat
$response = file_get_contents("http://upload.gfycat.com/transcode/" . $key . 
     "?fetchUrl=" . urlencode($upload->getUrl('g')));
$result = json_decode($response);
$result_mp4 = file_get_contents($result->mp4Url);
// save the converted file to AWS S3 /i
$s3->putObject(array(
    'Bucket'  => getenv('S3_BUCKET'),
    'Key'    => 'i/' . $upload->id64 . '.mp4',
    'Body'   => $result_mp4,
));

选项1更好,因为SDK将使用mp4文件句柄,而不是将整个文件加载到内存中(如file_get_contents)。