0
votes

I'm trying to figure out how to: a) Store / insert an object on Google Cloud Storage within a sub-directory b) List a given sub-directory's contents

I managed to resolve how to get an object here: Google Cloud Storage JSON REST API - Get object held in a sub-directory

However, the same logic doesn't seem to apply to these other types of call.

For store, this works:

https://www.googleapis.com/upload/storage/v1/b/bucket-name/o?uploadType=media&name=foldername%2objectname

but it then stores the file name on GCS as foldername_filename, which doesn't change functionality but isn't really ideal.

For listing objects in a bucket, not sure where the syntax for a nested directory should go in here: storage.googleapis.com/storage/v1/b/bucketname/o.

Any insight much appreciated.

1

1 Answers

0
votes

The first thing to know is that GCS does not have directories or folders. It just has objects that share a fixed prefix. There are features that both gsutil and the UI used to create the illusion that folders do exist.

https://cloud.google.com/storage/docs/gsutil/addlhelp/HowSubdirectoriesWork

With that out of the way: to create an object with a prefix you need to URL encode the object name, as I recall %2F is the encoding for /:

https://cloud.google.com/storage/docs/request-endpoints#encoding

Finally to list only the objects with a common prefix you would use the prefix parameter:

https://cloud.google.com/storage/docs/json_api/v1/objects/list#parameters

Using prefix=foo/bar/baz (after encoding) would list all the objects in the foo/bar/baz "folder", note that this is recursive, it will include foo/bar/baz/quux/object-name in the results. To stop at one level you want to read about the delimiter parameter too.