[英]appengine std PHP stream wrapper from storage
我正在編寫一個 php web 頁面,用戶可以在其中上傳 excel 文件。
我需要獲取此文件並通過 SpreadsheetReader PHP 庫讀取它
在 appengine 中,不可能在文件系統上寫入,所以我通過 streamwrapper 將此文件發送到 Cloud Storage 存儲桶(運行良好)。
但我不知道如何從 object 之類的存儲文件中讀取並加載到 SpreadsheetReader...
首先,按照我的部分代碼,存儲中的 stream 包裝器不起作用:
$client = new StorageClient(['projectId' => $projectId,
]);
$client->registerStreamWrapper();
$contents = file_get_contents($location);
返回此錯誤:“PHP 致命錯誤:未捕獲的 InvalidArgumentException:json_encode 錯誤:格式錯誤的 UTF-8 字符,可能編碼錯誤”
2)如何創建一個類似 object 的文件來做到這一點:
$reader = new SpreadsheetReader($file);
$sheets = $reader->Sheets();
謝謝你
$client = new StorageClient(['projectId' => $projectId,
]);
$client->registerStreamWrapper();
$contents = file_get_contents($location);
返回此錯誤:“PHP 致命錯誤:未捕獲的 InvalidArgumentException:json_encode 錯誤:格式錯誤的 UTF-8 字符,可能編碼錯誤”
您應該在 JSON 的 StorageClient 中提供 keyFile。
見https://github.com/googleapis/google-cloud-php-storage/blob/master/src/StorageClient.php#L78
我了解到您正在通過 streamwrapper 將文件寫入 Cloud Storage 存儲分區。
然后您嘗試使用以下命令讀取文件:
$contents = file_get_contents($location);
根據官方文檔鏈接
但是,如果一個應用程序需要讀取文件,並且這些文件是 static,您可以選擇使用 PHP 文件系統功能如 file_get_contents 讀取隨您的應用程序上傳的 static 文件。
例如:
$fileContents = file_get_contents($filePath);
其中指定的路徑必須是相對於訪問它們的腳本的路徑。
將應用程序部署到 App Engine 時,您必須上傳應用程序子目錄中的一個或多個文件,並且必須配置 app.yaml 文件,以便您的應用程序可以訪問這些文件。 有關完整的詳細信息,請參閱 PHP 5 使用 app.yaml 的應用程序配置。
我認為您想從 GCS 而不是從 Application Subdirectory鏈接讀取文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.