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

4.2k Views Asked by At

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?

3

There are 3 best solutions below

2
On BEST ANSWER

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.

0
On

If any one else stumbles on this, it is now possible:

using -a as suggested in the question

gsutil ls -a gs://BUCKET_NAME

https://cloud.google.com/storage/docs/using-versioned-objects#list

0
On

Brondon's answer work with XML, but not with gapi client.

/**
 * Get versions meta data of the object.
 * @return {goog.async.Deferred} return history of the object.
 */
mbi.data.Object.prototype.history = function() {
  var df = new goog.async.Deferred();
  var use_gapi = true;
  var name = this.getName();
  if (use_gapi) {
    // GAPI does not return result for versions request.
    var params = {
      'bucket': this.getBucketName(),
      'versions': true,
      'prefix': name
    };
    // console.log(params);
    var req = gapi.client.rpcRequest('storage.buckets.get',
        mbi.app.base.GAPI_STORAGE_VERSION, params);
    req.execute(function(json, row) {
      if (json) {
        df.callback(json);
      } else {
        df.errback();
        throw new Error(row);
      }
    });
  } else {
    var xm = mbi.data.Object.getXhr();
    var uri = new goog.Uri(this.getUrl());
    uri.setParameterValue('versions', 'true');
    uri.setParameterValue('max-keys', '25');
    uri.setParameterValue('prefix', name);
    var url = uri.setPath('').setFragment('').toString();
    xm.send(url, url, 'GET', null, {}, 1, function(e) {
      var xhr = /** @type {goog.net.XhrIo} */ (e.target);
      if (xhr.isSuccess()) {
        var xml = xhr.getResponseXml();
        // console.log(xml);
        var json = mbi.utils.xml.xml2json(xml);
        var items = json['ListBucketResult']['Version'];
        var versions = goog.isArray(items) ? items : items ? [items] : [];
        versions = versions.filter(function(x) {
          return x['Key'] == name;
        });
        df.callback(versions);
      } else {
        df.errback(xhr.getStatus() + ' ' + xhr.getResponseText());
      }
    });
  }
  return df;
};

GAPI return as follow without version meta:

[
 {
  "id": "gapiRpc",
  "result": {
   "kind": "storage#bucket",
   "id": "mbiwiki-test",
   "name": "mbiwiki-test",
   "timeCreated": "2013-08-20T01:18:46.957Z",
   "metageneration": "9",
   "owner": {
    "entity": "group-00b4903a97262a358b97b95b39df60893ece79605b60280ad389c889abf70645",
    "entityId": "00b4903a97262a358b97b95b39df60893ece79605b60280ad389c889abf70645"
   },
   "location": "US",
   "website": {
    "mainPageSuffix": "index.html",
    "notFoundPage": "error404.html"
   },
   "versioning": {
    "enabled": true
   },
   "cors": [
    {
     "origin": [
      "http://static.mechanobio.info",
      "http://mbinfo-backend.appspot.com",
      "https://mbinfo-backend.appspot.com",
      "http://localhost",
      "chrome-extension://pbcpfkkhmlbicomenogobbagaaenlnpd",
      "chrome-extension://mhigmmbegkpdlhjaphlffclbgkgelnbe",
      "chrome-extension://jhmklemcneaienackijjhdikoicmoepp"
     ],
     "method": [
      "GET",
      "HEAD",
      "POST",
      "PUT",
      "DELETE",
      "PATCH"
     ],
     "responseHeader": [
      "content-type",
      "Authorization",
      "Cache-Control",
      "x-goog-meta-reviewer"
     ]
    }
   ],
   "storageClass": "STANDARD",
   "etag": "CAk="
  }
 }
]