简体   繁体   English

使用 GCP PHP 客户端库创建文件夹

[英]Creating folders using GCP PHP Client Libraries

I started learning Google Cloud Storage PHP Client Library through documentation given by Google.我通过 Google 提供的文档开始学习 Google Cloud Storage PHP 客户端库。 But following questions are uncleared.但以下问题尚不清楚。

  1. How to create a folder in a bucket?如何在存储桶中创建文件夹?
  2. How to create a folder in a folder?如何在文件夹中创建文件夹?
  3. How to list all the files in a folder?如何列出文件夹中的所有文件?

I have learnt that folders are nothing but objects with "/" at the end of their name.我了解到文件夹只不过是名称末尾带有“/”的对象。 Also we can upload an object using $bucket->upload() method to upload an object.我们也可以使用$bucket->upload()方法上传 object 来上传 object。 This method needs a path of file so it is creating a trouble to create a folder此方法需要文件路径,因此创建文件夹很麻烦

Folders do not actually exist in Cloud Storage Buckets.云存储桶中实际上并不存在文件夹。 The namespace is flat.命名空间是扁平的。 The impression of folders is emulated by the Google Cloud Console and other tools.文件夹的印象由 Google Cloud Console 和其他工具模拟。 The files /MyFolder/test.txt and /YourFolder/test.txt are at the same level (same directory) in the bucket (at the root level) - I am ignoring object name hashing.文件/MyFolder/test.txt/YourFolder/test.txt位于存储桶(根级别)中的同一级别(同一目录) - 我忽略了 object 名称哈希。 Tools use the / character as a separator (delimitor) to emulate folders.工具使用/字符作为分隔符(定界符)来模拟文件夹。

How Subdirectories Work 子目录如何工作

When working with the Google Cloud Storage REST API, directories are listed by using a delimeter=/ and specifying a prefix for the directory name, example:使用 Google Cloud Storage REST API 时,通过使用delimeter=/并为目录名称指定前缀来列出目录,例如:

https://www.googleapis.com/storage/v1/b/MYBUCKETNAME/o?prefix=MyDirectory/?delimiter=/

This URL informs the REST API that you want to list the object names that start with MyDirectory/ breaking on object names that continue after the next delimiter, example: This URL informs the REST API that you want to list the object names that start with MyDirectory/ breaking on object names that continue after the next delimiter, example:

MyDirectory/file1.txt
MyDirectory/file2.txt
MyDirectory/something/file3.txt

Note that nowhere in the namespace is the object name (directory) MyDirectory or MyDirectory/something stored.请注意,命名空间中没有 object 名称(目录) MyDirectoryMyDirectory/something存储的内容。 That is just derived from the object names using prefixes and delimiters.这只是从使用前缀和分隔符的 object 名称派生而来。

The above URL will list the first two objects.上面的 URL 会列出前两个对象。 The third will be skipped.第三个将被跳过。

The API returns JSON data, and there are two types of data returned: API返回JSON数据,返回的数据有两种:

  1. items , which return the object names that start with prefix and do not contain a delimiter after the prefix. items ,返回以前缀开头且prefix后不包含delimiter的 object 名称。
  2. prefixes , which returns the object names truncated by the delimiter - which provides the equivalent of directory names. prefixes ,它返回被分隔符截断的 object 名称 - 它提供了目录名称的等价物。

In other words, the illusion of directories and their contents is just a fancy parsing trick performed by the Google Cloud Storage API.换句话说,目录及其内容的错觉只是谷歌云存储 API 执行的一个花哨的解析技巧。

Answer to question1 : Creating empty folders is possible only via cloud console.问题 1 的答案:只能通过云控制台创建空文件夹。 You cannot create empty folder in a GCP bucket via SDK functions.您不能通过 SDK 函数在 GCP 存储桶中创建空文件夹。 Suppose if you wanted to create, /test folder in a bucket, upload a file named /test/file.txt .假设如果您想在存储桶中创建/test文件夹,请上传一个名为/test/file.txt的文件。 Make sure that the files you upload later in that /test folder to be other than file.txt确保您稍后在该/test文件夹中上传的文件不是file.txt

Answer to question2 : Almost identical to first question except paths对问题 2的回答:除了路径之外几乎与第一个问题相同

Answer to question3 : Listing items in a particular directory can be done using extra parameters like delimiters, prefixes provided by GCP SDK.对问题 3 的回答:可以使用额外的参数(如 GCP SDK 提供的分隔符、前缀)来列出特定目录中的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM