Laravel getID3 无法从 S3 中提取文件

Laravel getID3 unable to pull file from S3

我终于开始在我的项目中测试外部文件存储系统,但我在尝试分析其中一些文件时遇到了一个奇怪的错误。

我想要实现的目标:获取某个 s3 目录中所有文件的列表(已完成)并使用 php 包通过它们的 ID3 标签分析它们:

https://packagist.org/packages/james-heinrich/getid3

$files = Storage::disk('s3')->files('going/down/to/the/bargin/basement/because/the/bargin/basement/is/cool'); //Get Files
$file = Storage::disk('s3')->url($files[0]); // First things first... let's grab the first one.
$getid3 = new getID3; // NEW OBJECT! 
return $getid3->analyze($file); // analyze the file!

然而,当我把它扔进 Tinker 时,它会用以下方式回击我:

"GETID3_VERSION" => "1.9.14-201703261440",
 "error" => [
   "Could not open "https://a.us-east-2.amazonaws.com/library/pending/admin/01%20-%20Cathedrals.mp3" (!is_readable; !is_file; !file_exists)",
 ],

这似乎表明该文件不可读?这是我第一次使用 AWS S3,所以可能有些地方我没有正确配置。

GetId3 doesn't have support for remote files.

您需要将文件从 S3 拉到本地存储,然后将文件的本地路径传递给 getID3analyze 方法。

# $file[0] is path to file in bucket.
$firstFilePath = $file[0];

Storage::put(
    storage_path($firstFilePath),
    Storage::get($firstFilePath)
);

$getid3->analyze(storage_path($firstFilePath));

问题是您将 URL 传递给 analyze 方法。这是提到here

To analyze remote files over HTTP or FTP you need to copy the file locally first before running getID3()

理想情况下,您将 URL 中的文件保存在本地,然后传递给 getID3->analyze()

// save your file from URL ($file)
// I assume $filePath is the local path to the file
$getID3 = new getID3;
return $getID3->analyze($filePath); // $filePath should be local file path and not a remote URL

在本地保存 s3 文件

$contents = $exists = Storage::disk('s3')->get('file.jpg');
$tmpfname = tempnam("/tmp", "FOO");
file_put_contents($tmpfname, $contents);
$getID3 = new getID3;
// now use $tmpfname for getID3
$getID3->analyze($tmpfname);
// you can delete temporary file when done