2015-04-04 22 views
0

google-api-php-client库有一个名为File.php的文件,在src/Google/Cache目录中。该文件从线149返回一个错误:Google - App Engine - API PHP - 警告:mkdir():本地文件系统是只读的,mkdir失败 - 允许本地文件系统制作目录

if (! mkdir($storageDir, 0755, true)) { 

的错误是:

警告命令mkdir():本地文件系统是只读的,MKDIR未能在C:\用户\ NONAME \文件\ google-cache-file.php on line 149

我正在通过从本地计算机上的Google App Engine启动器运行代码来测试代码。

显然,我的本地文件系统不允许PHP创建目录。我怎样才能让PHP创建目录?我使用Windows 7

在PHP文件,它规定:

注: 模式在Windows上被忽略。

Client.php文件时,Google_Client类有一个方法isAppEngine()。作为一个测试,我运行该方法(本地从我的电脑),它什么都没有返回。所以,。 。 。在我的计算机上测试App Engine项目时,google-api-php-client没有检测到这是App Engine的。如果它在App Engine上运行时,将创建$config对象时,它会检查代码在App Engine上运行,并

//自动使用内存缓存,如果我们在AppEngine上。

if ($this->isAppEngine()) { 
    // Automatically use Memcache if we're in AppEngine. 
    $config->setCacheClass('Google_Cache_Memcache'); 
} 

所以,我不知道是否该代码试图在我的电脑上创建一个新目录的原因,是为了用它来做缓存,因为它没有检测到代码运行在App Engine上。 (事实并非如此)

我想我可以将开发项目部署到App Engine,并使用echo语句测试OAuth2功能。每次我想测试时,我都需要不断部署。如果OAuth正在工作,显然我不需要改变任何东西。但是,如果我无法授权OAuth2,我将如何在本地机器上测试新版本?

我将以下代码部署到App Engine,并从服务器运行代码。它没有向浏览器显示错误。然后我刷新窗口,并从IF检查中得到一个味精,表明服务标记已被设置。所以,它看起来像是授权我的应用程序作为服务。所以,我假设因为我从App Engine运行代码,它使用Mem Cache而不是创建Cache目录,并将数据保存到文件中。

<?php 
session_start(); 

//The php file loaded below outputs information to the user in the browser 
include_once "templates/base.php"; 
//The autoload.php file loads the entire API library I think. It avoids needing to call specific files. 
require_once realpath(dirname(__FILE__) . '/google-api-php-client/src/Google/autoload.php'); 

//The following three lines are the credentials 
$client_id = 'my ID here.apps.googleusercontent.com'; //Client ID 
$service_account_name = 'My name [email protected]'; //Email Address 
$key_file_location = 'Test Project-file-name.p12'; //key.p12 

//Display a page header to the user in their browser 
echo pageHeader("Service Account Access"); 

//Check if the credentials are present and assigned to their variable names 
if (!strlen($client_id) 
    || !strlen($service_account_name) 
    || !strlen($key_file_location)) { 
    echo missingServiceAccountDetailsWarning(); 
} 

//The Google_Client Class is in file Client.php 
//Create a Google_Client object and use it to acquire an access token 
$client = new Google_Client(); 
/* 
foreach($client as $key => $value) { 
    echo $key." - ".'<br>'; 
} 
*/ 

//The object $client was just created in the above line of code. Call the function 'setApplicationName' that is inside of 
//the $client object. 
$client->setApplicationName("Client_Drive_Examples"); 

$service = new Google_Service_Drive($client); 

/************************************************ 
    If we have an access token, we can carry on. 
    Otherwise, we'll get one with the help of an 
    assertion credential. In other examples the list 
    of scopes was managed by the Client, but here 
    we have to list them manually. We also supply 
    the service account 
************************************************/ 
if (isset($_SESSION['service_token'])) { 
    echo 'There is a service token'; 

    $client->setAccessToken($_SESSION['service_token']); 
} 

$key = file_get_contents($key_file_location); 

//echo 'the $key: ' . $key; 
//echo '$service_account_name: ' . $service_account_name; 

/* 
https://www.googleapis.com/auth/drive - View and manage the files in your Google Drive 
https://www.googleapis.com/auth/drive.file - View and manage files that you have created with this app 
https://spreadsheets.google.com/feeds/ - Manage your spreadsheets 
*/ 
$scopes = array('https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file'); 

$cred = new Google_Auth_AssertionCredentials(
    $service_account_name, 
    $scopes, 
    $key 
); 

$client->setAssertionCredentials($cred); 

if ($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion($cred); 
} 


$_SESSION['service_token'] = $client->getAccessToken(); 

echo 'end of code'; 
?> 
+1

As far据我所知,GAE不会让你写入文件系统,所以即使你设法使它在本地运行,它会在部署后阻止你 – Patrice 2015-04-04 16:49:28

+0

谢谢。我想我唯一的选择是修改API代码。我搜索了所有PHP API文件,'mkdir'只用了一次,所以希望我可以编写一个自定义解决方案。 – 2015-04-04 17:09:25

+0

您是否使用PHP在GAE中实现了OAuth2? – 2015-04-04 17:12:28

回答

2

App Engine不支持动态创建目录。

+1

请注意,只有在沙盒环境中运行的GAE实例才适用,如果您确实需要访问您可以使用的文件系统[托管虚拟机](https://cloud.google.com/appengine/docs/managed -vms /)将您的GAE应用程序托管在GCE VM中,因此可以摆脱沙箱限制。 – jirungaray 2015-04-05 15:19:08

+1

确实如此,它挂在App Engine的定义上。托管虚拟机超出测试版时,我的回答可能会有所不同。 – 2015-04-05 16:15:16

1

它有点类似于tho。您只需将目录路径添加到文件名,并且它有一个新的长文件名,但它可以完成相同的操作,并保持向后兼容每个文件夹类型的较早的max文件,假设您将代码分支到环境中

相关问题