从 S3 读取 CSV 时出现“流不支持查找”

问题描述 投票:0回答:2

使用LeagueCSV“^9.6”

在我的本地服务器上读取 CSV 文件时 leaguecsv 效果很好。 我已将 CSV 文件移至 S3 进行生产,现在在进行 getHeader() 调用时遇到“查找”错误。

"{message: "流不支持查找", 异常: "League\Csv\Exception",…}"

出现搜索错误后,我尝试了在 Github 上看到的以下更改,如下所示,但没有任何帮助:

        $s3Client = \Aws\S3\S3Client::factory(array(
            'version' => 'latest',
            'region' => env('AWS_DEFAULT_REGION'),
            'credentials' => array(
                'key'    => env('AWS_ACCESS_KEY_ID'),
                'secret' => env('AWS_SECRET_ACCESS_KEY'),
            ),
        ));
        
        $s3Client->registerStreamWrapper();

        $context = stream_context_create(array(
            's3' => array(
                'seekable' => true
            )
        ));

我还从createFromPath(当文件位于本地服务器上时工作)更改为createFromStream for S3

        //load the CSV document from a file path
        //$csv = Reader::createFromPath($FileNameOnEC2, 'r');  <<--this worked fine when the file was on the local server
        $stream = fopen($FileNameOnS3, 'r', false, $context);
        $csv = \League\Csv\Reader::createFromStream($stream);

        $csv->setHeaderOffset(0);

        $header = $csv->getHeader(); //returns the CSV header record // <<-- calling this causes the error 

        $records = $csv->getRecords(); 
        $content = $csv->getContent();
        
        $stmt = (new Statement());

        $records = $stmt->process($csv);

有人看到这个问题吗?

php amazon-s3 thephpleague
2个回答
1
投票

我也遇到了由 Flysystem-aws-s3-v3 v1.0.28 中的 BC 引起的相同问题https://github.com/thephpleague/flysystem-aws-s3-v3/commit/c73ebc5b78076e971ec64fdab8a5a956d118b160

请参阅https://github.com/thephpleague/flysystem-aws-s3-v3/issues/218。 我目前已将版本修复为 v1.0.27。


0
投票

就我而言,它只是缺少 PHP 的curl 扩展

sudo apt-get install php-curl

© www.soinside.com 2019 - 2024. All rights reserved.