Search code examples
google-cloud-storagegoogle-api-js-client

How to get all versions of an object in Google cloud storage bucket?


In a web page hosted in Google cloud storage, I will like to show revision history, which require listing all versions of the object.

Sending GET request to the bucket with ?versions parameter return list versions all objects. Is there any way to list all versions of a single object, as in gsutil ls -la, in javascript?


Solution

  • There is not. The closest you can do is to use versions=true and prefix=YOUR_OBJECT_NAME.

    GCS will respond with a listing of objects beginning with all of the versions of your object and continuing on to any other objects that begin with YOUR_OBJECT_NAME. You'll have to check those items to see when the listing runs out of versions of your object and moves on to other objects.

    If it so happens that only one object begins with YOUR_OBJECT_NAME (for example, your object is "foo.txt" and there are no files named, say, "foo.txt.backup", you will get exactly the files you want. You probably don't want to rely on this as a general practice, though.