【问题标题】:upload to google cloud store using json api from javascript使用来自 javascript 的 json api 上传到谷歌云商店
【发布时间】:2017-05-21 20:06:16
【问题描述】:

我正在尝试使用gapi 将图像上传到谷歌云存储。我拥有的当前代码是

<script src="https://apis.google.com/js/api.js"></script>
<script type="text/javascript">
var imgData = null;

function getImage() {
    navigator.camera.getPicture(onSuccess, onFailure, {
        destinationType: navigator.camera.DestinationType.FILE_URI,
        sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
    });

    function onSuccess(imageURI) {
        imgData = encodeImageUri(imageURI);
        var contentLen = imgData.length;
        gapi.load('client', start);
    }

    function onFailure(message) {
        alert("Get image failed: " + message);
    }
}

function start() {
    // 2. Initialize the JavaScript client library.
    console.log('firing google storage api');
    gapi.client.init({
        'apiKey': 'XXX-XX'
    }).then(function() {
        // 3. Initialize and make the API request.
        console.log('api initialized');
        var request = gapi.client.request({
            'path': 'https://www.googleapis.com/upload/storage/v1/b/visionapibucket/o?uploadType=media&name=myObject',
            'method': 'POST',
            'headers': {
                'Content-Type': 'image/jpeg'
            },
            'body': imgData
        });

        try {
            //Execute the insert object request
            console.log('executing call');
            request.execute(function(resp) {
                alert(resp);
            });

        } catch (e) {
            console.log('An error has occurred: ' + e.message);
        }
    }).then(function(response) {
        console.log(response.result);
    }, function(reason) {
        console.log('Error: ' + reason.result.error.message);
    });
};
</script>

我可以看到代码正在命中 控制台中的语句是api initialized

但我没有看到gapi.client.request调用,甚至没有打印任何错误等

我不确定这里出了什么问题。请指教

【问题讨论】:

  • 我对此google api 了解不多,但我可以告诉你一件事,gapi.client.request参数path,method,params ,header,body 记录在here,请在更正后发送您的输入。
  • 只是路径是必需的,其他都是可选的
  • content type不是参数,应该发送为header {'Content-type' : 'image/jpeg'},请在更正后发送您的输入
  • 没有帮助。在问题中更新了我的代码
  • 你能不能把gapi.client.request移动到try,看看这个语句是否抛出异常。另外,这个网站 (googleapis.com) 在你身边吗?

标签: javascript google-cloud-storage json-api google-api-js-client client-library


【解决方案1】:

您不能仅使用 API 密钥将文件上传到 Google 存储。您必须有一个 oauth 令牌。

如果请求需要授权(例如对个人私人数据的请求),则应用程序必须在请求中提供 OAuth 2.0 令牌。应用程序也可以提供 API 密钥,但不是必须的。

如果请求不需要授权(例如对公共数据的请求),则应用程序必须提供 API 密钥或 OAuth 2.0 令牌,或两者都提供 — 任何对您来说最方便的选项。

https://cloud.google.com/storage/docs/json_api/v1/how-tos/authorizing

在你的情况下,你想上传,所以你必须有一个 oauth 令牌。

您有几种解决方法:

  • 您可以将文件上传到您的服务器,并使用您的服务器凭据通过服务帐户上传。
    https://cloud.google.com/compute/docs/access/service-accounts

  • 您可以在您的服务器上创建一个令牌,并将其发送给您的客户端。然后客户端无需永久访问令牌即可将一个文件上传到您的 Google 存储帐户

  • 您可以上传到您用户的 Google 存储帐户。为此,他们必须登录您的应用程序。 https://developers.google.com/identity/protocols/OAuth2

  • 最后一种方法是,您可以将文件上传到您的服务器,然后通过执行gutil mv 命令将其复制到云端。在这种情况下,您只需使用gcloud auth 登录一次您的 Google 云帐户即可 gustil mv, gcloud mv command

有关gcloud 实用程序的更多信息,并下载它:https://cloud.google.com/sdk/

【讨论】:

  • 我同意你上面所说的,但我的问题是,即使通话也不会发生。我应该看到身份验证错误或权限错误。不是吗?
  • @Aminadav,我可以得到你提到的You can create a token on your server, and send it to your client. Then the client can upload one file to your Google Storage account without a permanent access token 的点击吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 2014-08-03
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
相关资源
最近更新 更多