Laravel 5.0:CSV使用fopen()创建并使用存储在AWS S3存储桶上上传

时间:2020-11-09 05:51:56

标签: php mysql amazon-s3 laravel-5

我正在做两件事:

  1. 使用fopen()创建CSV文件,然后从mysql插入数据并关闭文件。
  2. 现在,需要将创建的文件直接上传到AWS S3存储桶。

现在,我能够创建CSV文件,但是无法将文件上传到AWS S3存储桶中。

Laravel PHP代码:

public function download() {

        $headers = array(
            "Content-type" => "text/csv",
            "Content-Disposition" => "attachment; filename=auditsystem.csv",
            "Pragma" => "no-cache",
            "Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
            "Expires" => "0"
        );
        $columns = array(
            'S.no',
            'State Name',
            'State Code'
        );
        $audit_params = DB::select(DB::raw("select * from table_state_codes"));
        $callback = function() use ($audit_params, $columns) {
         $file_name="auditsystem.csv";
            $file = fopen('php://output', 'w+');
            fputcsv($file, $columns);
            foreach ($audit_params as $rowOrders) {
                fputcsv($file, [
                    $rowOrders->id,
                    $rowOrders->state_name,
                    $rowOrders->state_code,
                ]);
            }
            fclose($file);
            Storage::disk('s3')->put('uploads/' . $file_name, file_get_contents($file), 'public');

        };

        return response()->stream($callback, 200, $headers);
 }

我遇到此错误,Error Uploading on AWS S3 bucket

我无法将文件上传到AWS S3存储桶。另外,文件格式保持为w +,这样我既可以写又可以读。

请让我知道我要去哪里了,请耐心等待您的意见。

谢谢。

0 个答案:

没有答案
相关问题