通过API-KEY使用Rest端点将图像上传到Google Cloud Storage

时间:2018-08-30 12:58:53

标签: rest google-cloud-platform google-cloud-storage storage

我是Google Cloud的新手。我想将图像上传到我的存储桶。 我是从GCP控制台和GCP Shell成功完成的。但是,我无法通过使用API​​-KEY的REST Endpoint来做到这一点。 这是我的网址。

https://www.googleapis.com/upload/storage/v1/b/[MY_BUCKET]/o?uploadType=media&name=myhero&key=[MY_API-KEY]

我收到以下消息

{
    "error": {
        "errors": [
            {
                "domain": "global",
                "reason": "required",
                "message": "Anonymous caller does not have storage.objects.create access to [MY_BUCKET]/myhero.",
                "locationType": "header",
                "location": "Authorization"
            }
        ],
        "code": 401,
        "message": "Anonymous caller does not have storage.objects.create access to [MY_BUCKET]/myhero."
    }
}

这是因为,即使API-KEY允许该密钥的用户将对象上载到存储桶,存储桶目前也不允许以任何方式允许用户使用API​​-KEY。

我该怎么办 1)如果我想使用API​​键方式。 2)如果我想使用服务帐户方式。生成授权令牌的最佳方法是什么?我到现在为止

{"alg":"RS256","typ":"JWT"}.
{
"iss":"761326798069-r5mljlln1rd4lrbhg75efgigp36m78j5@developer.gserviceaccount.com",
"scope":"https://www.googleapis.com/auth/prediction",
"aud":"https://www.googleapis.com/oauth2/v4/token",
"exp":1328554385,
"iat":1328550785
}.

但是我无法生成{Base64url编码的签名},因为我无法找出私钥和公钥...

1 个答案:

答案 0 :(得分:0)

由于没有正确进行身份验证,您将获得“匿名呼叫者”。您可以使用承载令牌代替API密钥进行身份验证。

您可以运行以下脚本:

BUCKET=<BUCKET_NAME>
OBJECT=<OBJECT>
TOKEN=$(gcloud auth print-access-token)
 curl "https://www.googleapis.com/upload/storage/v1/b/$BUCKET/o?uploadType=media&name=$OBJECT" -H"Authorization: Bearer $TOKEN" -H'Content-Type: image/jpg'  --data-binary @$OBJECT