简体   繁体   English

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

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

So, I've been trying to get this to work for the past couple hours, but I can't figure it out. 因此,在过去的几个小时中,我一直在努力使它正常工作,但我无法弄清楚。 The goal is to pull the converted mp4 file from gfycat and upload that file to the Amazon S3 bucket. 目标是从gfycat提取转换后的mp4文件,并将该文件上传到Amazon S3存储桶。

gfycat is returning a JSON object properly, and $result->mp4Url is returning a correct url to the mp4 file. gfycat正在正确返回JSON对象,而$ result-> mp4Url正在将正确的url返回到mp4文件。 I keep getting errors such as "object expected, string given". 我不断收到诸如“期望对象,给定字符串”之类的错误。 Any ideas? 有任何想法吗? Thanks. 谢谢。

// 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($response) yields: var_dump($ response)产生:

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)

Using json_decode() on it also yields similar results. 在上面使用json_decode()也会产生类似的结果。

You are mixing up the 'SourceFile' parameter (which accepts a file path) with the 'Body' parameter (which accepts raw data). 您正在将'SourceFile'参数(接受文件路径)与'Body'参数(接受原始数据)混合在一起。 See Uploading Objects in the AWS SDK for PHP User Guide for more examples. 有关更多示例,请参阅AWS SDK for PHP用户指南中的上传对象

Here are 2 options that should work: 这里有两个应该起作用的选项:

Option 1 (Using SourceFile) 选项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,
));

Option 2 (Using Body) 选项2(机身)

// 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,
));

Option 1 is better though, because the SDK will use a file handle to mp4 file instead of loading the entire thing into memory (like file_get_contents does). 不过选项1更好,因为SDK将使用文件句柄来处理mp4文件,而不是将整个内容加载到内存中(就像file_get_contents一样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM