4

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

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

4

2 回答 2

2

那没有。您可以做的最接近的是使用versions=trueand prefix=YOUR_OBJECT_NAME

GCS 将以对象的所有版本开头并继续以 YOUR_OBJECT_NAME 开头的任何其他对象的列表作为响应。您必须检查这些项目以查看列表何时用完您的对象版本并移至其他对象。

如果碰巧只有一个对象以 YOUR_OBJECT_NAME 开头(例如,您的对象是“foo.txt”并且没有名为“foo.txt.backup”的文件,您将得到您想要的文件。您不过,可能不想将此作为一般做法。

于 2013-09-11T07:43:19.740 回答
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 返回如下,没有版本元:

[
 {
  "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="
  }
 }
]
于 2013-09-13T02:11:10.037 回答