如何在Google云存储桶中获取对象的所有版本?

时间:2013-09-11 06:51:01

标签: google-cloud-storage google-api-js-client

在Google云端存储中托管的网页中,我想显示修订历史记录,这需要列出该对象的所有版本。

向具有?versions参数返回列表版本的所有对象的存储区发送GET请求。有没有办法列出单个对象的所有版本,如gsutil ls -la中的javascript?

2 个答案:

答案 0 :(得分:1)

没有。您最接近的方法是使用versions=trueprefix=YOUR_OBJECT_NAME

GCS将回复一个以对象的所有版本开头的对象列表,并继续处理以YOUR_OBJECT_NAME开头的任何其他对象。您必须检查这些项目,以查看列表何时用完对象的版本并转移到其他对象。

如果碰巧只有一个对象以YOUR_OBJECT_NAME开头(例如,你的对象是“foo.txt”并且没有名为的文件,比如说“foo.txt.backup”,那么你将获得完整的文件但是,你可能不希望依赖它作为一般做法。

答案 1 :(得分:0)

Brondon的回答是使用XML,但没有使用gapi客户端。

/**
 * 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返回如下,没有版本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="
  }
 }
]