如何正确授权对Google Cloud Storage API的请求? [英] How to properly authorize request to Google Cloud Storage API?

查看:392
本文介绍了如何正确授权对Google Cloud Storage API的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google Cloud Storage JSON API通过http调用从存储桶中检索文件.

I am trying to use the Google Cloud Storage JSON API to retrieve files from a bucket using http calls.

我正在GCE中与存储桶位于同一项目中的Container中卷曲,并且服务帐户具有对该存储桶的读取访问权限

I am curling from a Container in GCE within the same project as the storage bucket, and the service account has read access to the bucket

以下是请求的模式:

https://storage.googleapis.com/{bucket}/{object}

根据API控制台,由于服务帐户提供了应用程序默认凭据",因此我不需要任何特别的操作.但是,我一直这样:

According to the API console, I don't need anything particular as the service account provides Application Default Credentials. However, I keep having this:

Anonymous caller does not have storage.objects.get

我还尝试为该项目创建一个API密钥,并将其附加到url(https://storage.googleapis.com/{bucket}/{object}?key={key}),但仍然出现相同的401错误.

I also tried to create an API key for the project and appended it to the url (https://storage.googleapis.com/{bucket}/{object}?key={key})but I still got the same 401 error.

如何授权查询此API的请求?

How can I authorize requests to query this API?

推荐答案

您使用的URL不正确.这些API使用以https://www.googleapis.com/storage/v1/b开头的URL.

The URL that you are using is not correct. The APIs use a URL that starts with https://www.googleapis.com/storage/v1/b.

不建议使用API​​密钥.相反,您应该使用Bearer: token.我将展示两种方法.

Using API keys is not recommended. Instead you should use a Bearer: token. I will show both methods.

要获取gcloud默认配置的访问令牌:

To get an access token for the gcloud default configuration:

gcloud auth print-access-token

然后在curl请求中使用令牌.用gcloud命令中的令牌替换TOKEN.

Then use the token in your curl request. Replace TOKEN with the token from the gcloud command.

要列出存储桶:

curl -s -H "Authorization: Bearer TOKEN" https://www.googleapis.com/storage/v1/b

curl https://www.googleapis.com/storage/v1/b?key=APIKEY

列出对象:

curl -s -H "Authorization: Bearer TOKEN" https://www.googleapis.com/storage/v1/b/examplebucket/o

curl https://www.googleapis.com/storage/v1/b/examplebucket/o?key=APIKEY

API参考:列表桶

这篇关于如何正确授权对Google Cloud Storage API的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆