如何使用Amazon aws3将图像保存在子文件夹中? Laravel

时间:2018-12-11 11:12:36

标签: laravel amazon-s3

我正在使用aws存储图像,并且控制器中的代码如下所示:

Storage::disk('3')->put($file->getClientOriginalName(), fopen($file, 'r+'), 'public');

图像正在保存在我的本地存储中。 现在,我希望能够创建一个子文件夹来使图像井井有条。

就我而言,它正在注册一家公司。因此,我希望将图像存储在包含适当业务ID的子文件夹中。我尝试过:

Storage::disk('3')->put($file->getClientOriginalName(), fopen($file, 'r+'), 'public/' . $business->id.

有关控制器的更多信息如下:

$input              = $request->all();

$files              = isset($input['file']) ? $input['file'] : array ();
$business_names     = json_decode($input['business_names'], true);
$business_details   = json_decode($input['business_details']);
$share_amount       = json_decode($input['share_amount'], true);
$entity             = json_decode($input['entity'], true);
$directors          = json_decode($input['directors'], true);
$shareholders       = json_decode($input['shareholders'], true);
$appointments       = json_decode($input['appointments'], true);

$input['user_id'] = Auth::user()->id;

Log::info(Auth::user());
Log::info($request->user());
/* Create Business Record */
$business = new Business;
$business->business_names = json_encode($business_names);
$business->share_amount = $share_amount ?: 0;
$business->entity = $entity ?: '';
$business->business_physical_address = json_encode($business_details->physical_address);
$business->has_business_postal_address = $business_details->has_postal_address;
$business->business_postal_address = json_encode($business_details->postal_address);
$business->user_id = $input['user_id'];
$business->save();

/* Create a new folder in storage/app/files named after the business ID */
Storage::makeDirectory('files/' . $business->id);

/* Upload Files */
// TODO: file storing?
foreach($files as $file) {
    if ($file) {
        Storage::disk('3')->put($file->getClientOriginalName(), fopen($file, 'r+'), 'public/' . $business->id);
        // $file->storeAs('files/' . $business->id, $file->getClientOriginalName());
    }
} 

当我现在尝试保存公司时,出现以下错误:

  

C:\ xampp \ htdocs \ startingabandbaby \ vendor \ league \ flysystem \ src \ Adapter \ Local.php(356):   Illuminate \ Foundation \ Bootstrap \ HandleExceptions-> handleError(8,   '未定义的索引...','C:\ xampp \ htdocs ...',356,数组)

由于我以前能够存储图像,所以我认为这与连接业务ID有关。

每次创建新业务并将所有文件添加到同一文件夹中时,如何创建带有业务ID的子文件夹?

2 个答案:

答案 0 :(得分:1)

根据我们的讨论,您可以使用以下2种解决方案:

1)put()

$path = Storage::disk('s3')->put(
                    '/files/'. $business->id, //path you want to upload image to S3
                    file_get_contents($request->file('file')), //fileContent
                   'public' //visibility

2)putFileAs():要使用putFileAs()实现相同的功能,我需要将其编写如下。第一个参数需要目录名称,我在通过文件名模仿s3中的目录名称时将其留空。

$path = Storage::disk('s3')->putFileAs(
                '', // 1st parameter expects directory name, I left it blank as I'm mimicking the directory name through the filename
                '/files/'. $business->id,
                $request->file('file'), //3rd parameter file resource
                ['visibility' => 'public'] //options
            );

希望这对您有帮助!

答案 1 :(得分:0)

经过研究,我得出以下结论:

$directory = 'public/' . $business->id;
Storage::disk()->makeDirectory($directory);

foreach($files as $file) {
    if ($file) {
        Storage::disk()->put($directory . '/' .$file->getClientOriginalName(), fopen($file, 'r+'));                   
    }
}