[英]Creating folder in bucket google cloud storage using php
我对谷歌云存储很陌生。
我想使用 php 编码在存储桶中创建文件夹。 我搜索了很多网站,在 1 上我看到它是这样写的:
“在存储桶内创建文件夹将创建一个以目录命名的占位符对象,没有数据内容和 mimetype application/x-directory。不支持在 Google Storage Manager 中创建的目录占位符对象。”
我不明白它想说什么。 我如何创建文件夹请帮帮我。 我尝试使用以下代码:
$req = new Google_HttpRequest("http://commondatastorage.googleapis.com/bucket/myfoldertrial");
$req->setRequestHeaders(array(
'x-goog-project-id' => 21212,
'x-goog-acl' => 'public-read',
'Content-Type' => 'application/x-directory'
));
$req->setRequestMethod('PUT');
$req->setPostBody('myfoldertrial');
我正在使用以下链接中的 API:
请帮助我使用 PHP 创建文件夹。
您可能实际上不需要创建文件夹。
Google Storage 不像您的操作系统的文件系统使用的树结构,所有对象都存储在顶级存储桶中。 但是,您可以为对象指定一个带有斜杠的名称,因此它看起来像是在文件夹中 - Users/username/docs/2012/09/21/activity.csv
是对象的完美名称,并且不需要任何支持文件夹。
一旦您获得了具有这种方案的对象,您就可以列出它们,就像您正在查看文件夹的内容一样,根据这些文档使用delimiter
和prefix
参数。
因此,如果您只想创建myfoldertrial
以便将example.png
上传到其中,则无需创建文件夹,您可以直接上传到myfoldertrial/example.png
。
有时,在CMS中,您需要先创建一个目录,然后才能将文件上传到其中,因此单击鼠标可以触发事件以路径为基础文件夹,然后进行批量上传。
他们说,这是一个文件浏览器。
下面的这段代码可能会有所帮助。
<?php
$privateKeyFile = '{{full/path/to/*.p12}}';
$newDirectory = '{{path/of/new/directory/}}'; // remember to end it with a slash.
/**
* Authentication
*/
$client = new Google_Client();
$client->setApplicationName('Create a new folder');
$client->setClientId($clientId);
$scopes = array('https://www.googleapis.com/auth/devstorage.full_control');
$client->setScopes($scopes);
$service = new Google_Service_Storage($client);
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
if (!file_exists($privateKeyFile)) {
die('missing the location of primary key file, given: ' . $privateKeyFile);
}
$key = file_get_contents($privateKeyFile);
$cred = new Google_Auth_AssertionCredentials(
$clientEmailAddress
, $scopes
, $key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
/**
* Creating Folder
*/
try {
/* create empty file that acts as folder */
$postBody = new Google_Service_Storage_StorageObject();
$postBody->setName($newDirectory);
$postBody->setSize(0);
$created = $service->objects->insert($bucketName, $postBody, array(
'name' => $newDirectory,
'uploadType' => 'media',
'projection' => 'full',
'data' => '',
));
} catch (Exception $ex) {
echo $ex->getMessage() . "\n<pre>";
print_r($ex->getTraceAsString());
echo '</pre>';
die();
}
echo 'EOF';
您可以通过在上传时在文件路径中提供它来简单地创建文件夹,即您的网址应为https://storage.googleapis.com/ //?GoogleAccessId=id@developer.gserviceaccount.com&Expires=1410875751&Signature=SIGNATURE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.