Moeez448's avatar

Want to create CSV file and upload in s3 bucket AWS.

I want to create a csv file on the run time and store it in the aws s3 bucket but I am unable to do so. Here is my code. Please help me resolve this issue.

​```

          $file = fopen(Storage::disk('s3')->put('/clean_sheet/', $path), "w");       
                    $i = 1;

                    foreach ($cleanSheetss as $key => $cleanSheet) {
                        if ($i == 1) {
                            fputcsv($file, array("Retailer Name", "Location", "Province", 'SKU', "Brand", "product Name", "Category", "Sold", "Purchased", "Average Price", "Average Cost", "Flag", "Comments"));
                        } else {
                            fputcsv($file, $cleanSheet->toArray());
                        }
                        $i++;
                    }
                    fclose($file);

​```

0 likes
5 replies
Sinnbeck's avatar

What is the error?

Also if you want to store it on aws, you should use the storage facade directly. Dont run fopen on it.. It has methods for the things you can do

Sinnbeck's avatar

@Moeez448 Specify what? Write the file to disk (you can store it in /tmp) and then use this to store it on s3

Storage::disk('s3')::putFileAs(
    'csvs', $file, $filename
);

Please or to participate in this conversation.