mardi 9 mars 2021

File upload not overwritte in AWS S3

I want to upload an excel file but the AWS does not save my file. and I do not know what the problem is. I try to dump all the data and the process says it uploaded successfully.

Below is my code and files involve for laravel:

filefolderlibrary.php

public static function upload($name, $path, $file)
{
    $path          = preg_replace('{/$}', '', $path);
    $original_path = $path;

    //add tenant folder as parent folder
    $path = 'prod/' . config('b3.tenant_path') . '/' . $path;

    //check folder to separete public and private bucket
    $private_folder = ['student', 'finance', 'images', 'user', 'oa_material', 'active-student'];

    if (array_intersect(explode('/', $path), $private_folder)) {
        $s3_access = 's3_private';
    } else {
        $s3_access = 's3_public';
    }

    if (pathinfo($name, PATHINFO_EXTENSION) == "") {
        $extension = $file->getClientOriginalExtension(); // getting file extension
        $fileName  = $name . '.' . $extension;
    } else {
        $fileName = $name;
    }

    $full_path = $path . '/' . $fileName;

    if (is_string($file)) {
        //$upload_path = Storage::disk(config('b3.disk'))->put($full_path, $file);
        $upload_path = Storage::disk($s3_access)->put($full_path, $file);
    } else {
        // $upload_path = Storage::disk($s3_access)->putFileAs($path, $file, $fileName);
        $upload_path = Storage::disk($s3_access)->put($path, $file);
    }

    $data['name'] = $fileName;
    $data['path'] = $original_path . '/' . $fileName;

    return $data;
}

filesystems.php

'disks' => [
 's3_public' => [
            'driver' => 's3',
            'key' => env('AWS_KEY'),
            'secret' => env('AWS_SECRET'),
            'region' => env('AWS_REGION'),
            'bucket' => env('AWS_BUCKET_PUB'),
            'url'   => env('AWS_URL'),
            'visibility' => 'public'
        ],

        's3_private' => [
            'driver' => 's3',
            'key' => env('AWS_KEY'),
            'secret' => env('AWS_SECRET'),
            'region' => env('AWS_REGION'),
            'bucket' => env('AWS_BUCKET_PRI'),
            'url'   => env('AWS_URL'),
            'visibility' => 'private'
        ],
],

ImportLeadController.php

public function store()
{
    $data = [];

    $file_name = 'temp-import-prospect-file-' . auth()->user()->id;
    $file_info = FileFolderLibrary::upload($file_name, 'marketing/import_prospect', Input::file('file'));

    $excel = Excel::load(Input::file('file'), function ($reader) {
        $reader->formatDates(true, GeneralSetting::where('field', 'system_date_format')->select(['value'])->first()->value);
        return $reader;
    });

    $result = $excel->get();

    $result = $this->convertData($result);
    
    $data              = [];
    $data['prospects'] = $result;
    $data['file_name'] = $file_info['name'];

    return $data;
}

.env

AWS_BUCKET_PUB=vialing-image-public
AWS_BUCKET_PRI=vialing-image-private
AWS_REGION=ap-southeast-1
AWS_URL=https://cdn1.vialing.com/

Can anyone know how to overcome the problem? Thank you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire