[英]Creating folder in bucket google cloud storage using php
I am very new to the google cloud storage.我对谷歌云存储很陌生。
I want to create folder in bucket using php coding.我想使用 php 编码在存储桶中创建文件夹。 I have searched a quite few sites and on 1 i saw it was written:
我搜索了很多网站,在 1 上我看到它是这样写的:
"Creating a folder inside a bucket will create a placeholder object named after the directory, has no data content and the mimetype application/x-directory. Directory placeholder objects created in Google Storage Manager are not supported." “在存储桶内创建文件夹将创建一个以目录命名的占位符对象,没有数据内容和 mimetype application/x-directory。不支持在 Google Storage Manager 中创建的目录占位符对象。”
I could not understand what it is trying to say.我不明白它想说什么。 How can i create folder please help me out.
我如何创建文件夹请帮帮我。 I tried using the following code:
我尝试使用以下代码:
$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');
I am using the API from following link:我正在使用以下链接中的 API:
Google API for PHP用于 PHP 的 Google API
Please help me out creating folder using PHP.请帮助我使用 PHP 创建文件夹。
You probably don't actually need to create a folder.您可能实际上不需要创建文件夹。
Google Storage isn't a tree structure like your operating system's filesystem uses, all Objects are stored in buckets at the top level. Google Storage 不像您的操作系统的文件系统使用的树结构,所有对象都存储在顶级存储桶中。 However you can give an Object a name with slashes in it, so it will kind of look like it is in a folder -
Users/username/docs/2012/09/21/activity.csv
is a perfectly good name for an Object and doesn't need any supporting folders.但是,您可以为对象指定一个带有斜杠的名称,因此它看起来像是在文件夹中 -
Users/username/docs/2012/09/21/activity.csv
是对象的完美名称,并且不需要任何支持文件夹。
Once you've got Objects with this sort of scheme in place, you can list them as if you were viewing the contents of a folder with the delimiter
and prefix
parameters as per these docs .一旦您获得了具有这种方案的对象,您就可以列出它们,就像您正在查看文件夹的内容一样,根据这些文档使用
delimiter
和prefix
参数。
So if you only wanted to create myfoldertrial
so you could upload example.png
into it, there's no need to create the folder, you can just upload straight to myfoldertrial/example.png
.因此,如果您只想创建
myfoldertrial
以便将example.png
上传到其中,则无需创建文件夹,您可以直接上传到myfoldertrial/example.png
。
Sometimes, in a CMS, you need to create a directory first before able to upload file into it, so the mouse click can trigger an event to take the path as the base folder, then do a batch upload.有时,在CMS中,您需要先创建一个目录,然后才能将文件上传到其中,因此单击鼠标可以触发事件以路径为基础文件夹,然后进行批量上传。
It's a file browser, they say.他们说,这是一个文件浏览器。
This code below might help.下面的这段代码可能会有所帮助。
<?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.