To upload the Google Drive images to AWS S3 using Laravel import functionality

I’m trying to upload the Google Drive images to AWS S3 using Laravel import functionality. I have more than 2000 products in it. Each product has 2 mandatory images, in which I’m facing one issue. The first 15 to 18 product images (2 X 15 = 30) are properly getting uploaded,the remaining product images are not uploading. It shows exception error message.I have tried with sleep function also even though its not working.

    public function importProduct(Request $request)
    {
        try {
            $extension = (isset($request->product_excel) && !empty($request->product_excel)) ? strtolower($request->product_excel->getClientOriginalExtension()) : '';
            $validator = app('validator')->make(
                [
                    'product_excel' => $request->product_excel,
                    'extension' => $extension,
                ],
                [
                    'product_excel' => 'required',
                    'extension' => 'required|in:csv,xlsx,xls',
                ],
            );
            if ($validator->fails()) {
                foreach ($validator->errors()->messages() as $key => $value) {
                    $error[] = is_array($value) ? implode(',', $value) : $value;
                }
                $errors = implode(', n ', $error);
                return ResponseBuilder::responseResult(400, $errors);
            }
            $excel_url = '';
            if (!empty($request->file('product_excel'))) {
                $file = $request->file('product_excel');
                $fileName = time() . '-' . $file->getClientOriginalName();
                $request->file('product_excel')->move(public_path() . '/excel/upload/', $fileName);
                $excel_url = ImageManager::upload_product_excel(public_path() . '/excel/upload/' . $fileName, $fileName);
                $objPHPExcel = PhpOfficePhpSpreadsheetIOFactory::load(public_path() . '/excel/upload/' . $fileName);
                $objWorksheet = $objPHPExcel->getSheet(0);
                $highestRow = $objWorksheet->getHighestDataRow();
                $highestColumn = $objWorksheet->getHighestDataColumn();
                $headingsArray = $objWorksheet->rangeToArray('A1:' . $highestColumn . '1', null, true, true, true);
                $headingsArray = $headingsArray[1];

                $r = -1;
                for ($row = 2; $row <= $highestRow; ++$row) {
                    ++$r;
                    $checkDataRow = array();
                    $checkDataRow = $objWorksheet->rangeToArray('B' . $row . ':' . $highestColumn . $row, null, true, true, true);
                    $row_data = array_filter($checkDataRow[$row]);
                    if (!empty($row_data)) {
                        $dataRow = $objWorksheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, null, true, true, true);
                        $namedDataArray[$r]['row'] = $row;
                        foreach ($headingsArray as $columnKey => $columnHeading) {
                            $namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];
                        }
                    }
                }
                $no_of_products = $no_of_errors = $no_of_success = 0;
                $all_errors = $product = array();
                if (isset($namedDataArray) && count($namedDataArray) > 0) {
                    $product_slug_list = Product::getAdminExportProductSlugList();
                    $product_ids = array_keys($product_slug_list);
                    $product_name = '';
                    $p = 0;

                    foreach ($namedDataArray as $row) {
                        $input['product_id'] = isset($row['ProductId']) ? trim($row['ProductId']) : '';
                        $input['product_name'] = isset($row['ProductName']) ? trim($row['ProductName']) : '';

                        if ($p <> 0 && !empty($product) && !empty($input['product_name'])) {
                            if (empty($product['id']) && in_array($product['slug'], $product_slug_list)) {
                                $product['id'] = array_search($product['slug'], $product_slug_list);
                            }
                            $updated_product_id = $this->updateProduct($product);
                            $no_of_success++;
                            $no_of_products++;
                            $product = array();
                        }
                        if (!empty($input['product_name'])) {
                            $product_name = $input['product_name'];
                            $input['description'] = isset($row['Description']) ? trim($row['Description']) : '';
                            $input['mrp_price'] = isset($row['MRP_Price']) ? trim($row['MRP_Price']) : '';
                            $input['image1'] = isset($row['Image1']) ? trim($row['Image1']) : '';
                            $input['image2'] = isset($row['Image2']) ? trim($row['Image2']) : '';
                            for ($i = 1; $i < 3; $i++) {
                                $baseUrl = $this->extractBaseUrl($input['image' . $i]);
                                if ($baseUrl == 'https://drive.google.com') {
                                    $fileId = $this->extractFileId($input['image' . $i]);
                                    $input['image' . $i] = $this->constructImageUrl($fileId);
                                }
                            }
                            $input['status'] = isset($row['ActiveStatus']) ? trim($row['ActiveStatus']) : '';
                            $n_rules = [
                                'product_name' => ['required', 'max:254'],
                                'description' => ['required'],
                                'mrp_price' => ['required', 'numeric'],
                                'image1' => ['required', 'url'],
                                'image2' => ['required', 'url'],
                                'status' => ['required', 'in:Active,In-Active']
                            ];
                            $m_validator = app('validator')->make($input, $n_rules);
                            if ($m_validator->fails()) {
                                $error = array();
                                foreach ($m_validator->errors()->messages() as $key => $value) {
                                    $error[] = is_array($value) ? implode(',', $value) : $value;
                                }
                                $all_errors[$v . '-' . $input['product_name']] = $error;
                                $no_of_errors++;
                                $no_of_products++;
                            } else {
                                $product = [
                                    'id' => $input['product_id'],
                                    'name' => $input['product_name'],
                                    'slug' => strtolower(Str::slug($input['product_name'], '-')),
                                    'details' => $input['description'],
                                    'purchase_price' => BackEndHelper::currency_to_usd($input['mrp_price']),
                                    'image_1' => $input['image1'],
                                    'image_2' => $input['image2'],
                                    'status' => $input['status'] == 'Active' ? '1' : '0',
                                ];
                            }
                        }
                        $p++;
                    }

                    if (!empty($product)) {
                        if (empty($product['id'])) {
                            if (in_array($product['slug'], $product_slug_list)) {
                                $product['id'] = array_search($product['slug'], $product_slug_list);
                            }
                        }
                        $updated_product_id = $this->updateProduct($product);
                        $no_of_success++;
                        $no_of_products++;
                    }
                    $admin_id = auth('admin')->user()->id;
                    $error_data = [
                        'file_name' => $fileName,
                        'errors' => json_encode($all_errors),
                        'created_at' => date('Y-m-d H:i:s'),
                        'created_by' => auth('admin')->user()->id,
                        'file' => $excel_url,
                        'no_of_rows' => count($namedDataArray),
                        'no_of_products' => $no_of_products,
                        'no_of_success' => $no_of_success,
                        'no_of_errors' => $no_of_errors,
                    ];
                    import_product_errors::insert($error_data);
                }
                unlink(public_path() . '/excel/upload/' . $fileName);
                if (!empty($all_errors)) {
                    return ResponseBuilder::responseResult(200, 'Product Imported with Errors');
                }
                return ResponseBuilder::responseResult(200, 'Processed Successfully..!');
            }
            return ResponseBuilder::responseResult(400, 'No Product Found');
        } catch (GuzzleHttpExceptionServerException $e) {
            return ResponseBuilder::responseResult(500, $e->getMessage());
        } catch (GuzzleHttpExceptionRequestException $e) {
            return ResponseBuilder::responseResult(60, $e->getMessage());
        }
    }
    public function updateProduct($product)
    {
        try {
            // DB::beginTransaction();
            $img = array();
            for ($i = 1; $i < 3; $i++) {
                $img['image_' . $i] = $product['image_' . $i];
            }
            unset($product['image_1'], $product['image_2']);
            if (!empty($product['id'])) {
                Product::whereId($product['id'])->update($product);
            } else {
                $product['id'] = Product::insertGetId($product);
            }
            $product_image = array();
            for ($i = 1; $i < 3; $i++) {
                $image = isset($img['image_' . $i]) ? $img['image_' . $i] : '';
                $product_image['image_' . $i] = '';
                if (!empty($image)) {
                    $product_image['image_' . $i] = $this->storeImage($img['image_' . $i], $product['id'], 'image', $i);
                }
            }

            if (!empty($product_image)) {
                Product::whereId($product['id'])->update($product_image);
            }

            // DB::commit();
            return $product['id'];
        } catch (GuzzleHttpExceptionServerException $e) {
            // DB::rollBack();
            return $e->getMessage();
        } catch (GuzzleHttpExceptionRequestException $e) {
            // DB::rollBack();
            return $e->getMessage();
        }
    }
    public function extractBaseUrl($googleDriveUrl)
    {
        $parsedUrl = parse_url($googleDriveUrl);

        if (isset($parsedUrl['scheme']) && isset($parsedUrl['host'])) {
            $baseUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'];
            return $baseUrl;
        }
        return null;
    }
    public function extractFileId($googleDriveUrl)
    {
        preg_match('//file/d/([^/]+)//', $googleDriveUrl, $matches);
        return $matches[1];
    }
    public function storeImage($image, $product_id, $product_type, $i = '0')
    {
        $client = new Client();
        $name = 'product-' . $product_id . '-' . $i . '.jpg';
        $thumb_path = 'images/product/detail-thumb/' . $product_id . '/' . $name;
        $original_path = 'images/product/original/' . $product_id . '/' . $name;
        try {
            $response = $client->get($image, [
                'sink' => public_path('images/tmp/' . $name), // Save the response directly to a file
            ]);
            $ImageContent = @file_get_contents(public_path('images/tmp/' . $name));
            
            Storage::disk('s3')->put($original_path, $ImageContent);
            
            $thumbResizedImage = Image::make($ImageContent)->resize(40, 50)->encode();

            Storage::disk('s3')->put($thumb_path, (string) $thumbResizedImage);
        } catch (RuntimeException $e) {
            return "run time error-" . $i . "-=>" . $e->getMessage();
        } catch (GuzzleHttpExceptionServerException $e) {
            return "server time error-" . $i . "-=>" . $e->getMessage();
        } catch (GuzzleHttpExceptionRequestException $e) {
            return "request time error-" . $i . "-=>" . $e->getMessage();
        } finally {
            // Clean up: delete the temporary file
            unlink(public_path('images/tmp/' . $name));
            return Storage::disk('s3')->url($original_path);
        }
        return '';
    }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật