简体   繁体   English

使用javascript的json api上传到谷歌云商店

[英]upload to google cloud store using json api from javascript

I am trying to use gapi to upload an image to google cloud storage .The current code I have is 我正在尝试使用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>

I can see that the code is hitting the statement in console is : api initialized 我可以看到代码在控制台中的语句api initialized

but I don't see the gapi.client.request being called or even printing any error etc . 但我没有看到gapi.client.request调用甚至打印任何错误等

I am not sure what is wrong here. 我不确定这里有什么问题。 Please advise 请指教

You cannot upload files to Google storage by using just API key. 您只能使用API​​密钥将文件上传到Google存储空间。 You must have an oauth token. 你必须有一个oauth令牌。

If the request requires authorization (such as a request for an individual's private data), then the application must provide an OAuth 2.0 token with the request. 如果请求需要授权(例如对个人私有数据的请求),则应用程序必须为请求提供OAuth 2.0令牌。 The application may also provide the API key, but it doesn't have to. 应用程序也可以提供API密钥,但不一定要提供。

If the request doesn't require authorization (such as a request for public data), then the application must provide either the API key or an OAuth 2.0 token, or both—whatever option is most convenient for you. 如果请求不需要授权(例如请求公共数据),那么应用程序必须提供API密钥或OAuth 2.0令牌,或两者兼而有之 - 无论哪种选项最方便。

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

In your case, you want to upload so you must have an oauth token. 在您的情况下,您要上传,因此您必须拥有oauth令牌。

You have several workaround: 你有几个解决方法:

  • You can upload the file to your server, and upload it using you server credentials by using service account. 您可以将文件上载到服务器,然后使用服务帐户使用服务器凭据上载文件。
    https://cloud.google.com/compute/docs/access/service-accounts https://cloud.google.com/compute/docs/access/service-accounts

  • 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 然后,客户端可以将一个文件上传到您的Google存储空间帐户而无需永久访问令牌

  • You can upload to the Google Storage account of your users. 您可以上传到用户的Google存储空间帐户。 For do so, they must login to your app. 为此,他们必须登录您的应用程序。 https://developers.google.com/identity/protocols/OAuth2 https://developers.google.com/identity/protocols/OAuth2

  • The last method is, you can upload the files to your server, and copy it to the cloud by executing gutil mv command. 最后一种方法是,您可以将文件上传到服务器,然后通过执行gutil mv命令将其复制到云端。 In this case all you have to do is to login once to your Google cloud account by using gcloud auth gustil mv , gcloud mv command 在这种情况下,您所要做的就是使用gcloud auth gustil mvgcloud mv命令登录您的Google云帐户一次

More info about gcloud utility, and to download it: https://cloud.google.com/sdk/ 有关gcloud实用程序的更多信息,以及下载它: httpsgcloud

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JavaScript从客户端浏览器上传到Google云端存储 - Upload from Client Browser to Google Cloud Storage Using JavaScript 将文件从Javascript上传到Google Cloud Endpoint - Upload file from Javascript to Google Cloud Endpoint 在Javascript中使用JSON从Google Search Ajax API获取结果 - Using JSON in Javascript to obtain results from Google Search Ajax API 使用javascript将请求批量上传到Google云端存储 - Batch upload requests to Google Cloud Storage using javascript Google Cloud Storage - 如何直接从浏览器上传内容 (JavaScript) - Google Cloud Storage - How to Upload content directly from browser (JavaScript) 如何使用 Google Cloud Storage JSON API 获取可恢复上传的上传进度信息? - How to get upload progress informations for a resumable-upload with Google Cloud Storage JSON API? 使用Javascript在Google Maps API中使用JSON标记 - Using JSON markers in Google Maps API with Javascript 使用本机 JavaScript 向 Google Cloud 文本转语音 API 进行身份验证 - Authenticate to Google Cloud text-to-speech API using native JavaScript Google Cloud Platform Storage JSON API上传会中断音频文件。 怎么修? - Google Cloud Platform Storage JSON API upload breaks audio files..?! How to fix? 通过javascript将文件上传到Google云存储 - Upload file to google cloud storage by javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM