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:
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.
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 thefoo/bar/baz
"folder", note that this is recursive, it will includefoo/bar/baz/quux/object-name
in the results. To stop at one level you want to read about thedelimiter
parameter too.