Search code examples
getgoogle-cloud-storagesubdirectorybucket

Google Cloud Storage JSON REST API - Get object held in a sub-directory


I'm looking to access an object I have stored in Google Cloud Storage - this is straightforward enough if the object is stored at the highest level of the bucket using the following call as per the documentation:

https://storage.googleapis.com/storage/v1/b/bucketname/o/objectname?alt=media

However, if the file is held within a sub-directory on the bucket I'm unable to access it. There doesn't seem to be any guidance in the docs on this. I've tried the following calls but neither have been successful:

https://storage.googleapis.com/storage/v1/b/bucketname/o/foldername/objectname?alt=media https://storage.googleapis.com/storage/v1/b/bucketname/foldername/o/objectname?alt=media

I don't know if the directory is supposed to be submitted as part of the main URL or as a query parameter or somewhere else. Please note that I've submitted the Access Token through the header, too.

I'd also like to be able to store and list objects within a sub-directory so any help with that would be greatly appreciated too.

Could anyone please advise?


Solution

  • You need to URL encode the / in the object name:

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

    therefore it should be (assuming no other /):

    https://storage.googleapis.com/storage/v1/b/bucketname/o/foldername%2Fobjectname?alt=media