【问题标题】:Google bucket SignedUrls 403Google 存储桶 SignedUrls 403
【发布时间】:2018-07-12 07:18:45
【问题描述】:

我正在做一个简单的 rest API,它执行以下操作:

  • 获取base64编码图片
  • 解码
  • 将其存储在特定的谷歌存储桶中

现在,在 GET 动词上,我的 api 返回一个针对存储桶图像的签名 url。

我编写了一个有效的测试代码:

    initialization stuff
    ...
    BeforeEach(func() {
        mockStringGenerator.On("GenerateUuid").Return("image1")

        // First store
        image, _ = ioutil.ReadFile("test_data/DSCF6458.JPG")
        encodedImage = b64.RawStdEncoding.EncodeToString(image)
        fileName, storeError = storage.Store(ctx, encodedImage, "image/jpeg")

        // Then get
        uri, getError = storage.Get(ctx, fileName)
        getResponse, _ = http.Get(uri)

        // Finally delete
        deleteError = storage.Delete(ctx, fileName)
    })

    // Only 1 test to avoid making too much connexion
    It("should create, get and delete the image", func() {
        // Store
        Expect(storeError).To(BeNil())
        Expect(fileName).To(Equal("image1.jpg"))

        // Get
        Expect(getError).To(BeNil())
        Expect(getResponse.StatusCode).To(Equal(http.StatusOK))
        b, _ := ioutil.ReadAll(getResponse.Body)
        Expect(b).To(Equal(image))

        // Delete
        Expect(deleteError).To(BeNil())
    })

但是当我运行 .exe 并尝试向邮递员发出 ssome 请求时,我在签名的 url 中收到 403 错误:

<?xml version='1.0' encoding='UTF-8'?>
<Error>
    <Code>AccessDenied</Code>
    <Message>Access denied.</Message>
    <Details>Anonymous caller does not have storage.objects.get access to teddycare-images/08d8c508-d97d-48d3-947b-a7f216f622db.jpg.</Details>
</Error>

有什么想法吗?我真的不明白... 救救我,伙计们

[编辑] 在我用来创建 signedUrl 的代码之后:

func (s *GoogleStorage) Get(ctx context.Context, fileName string) (string, error) {
    url, err := storage.SignedURL(s.Config.BucketImagesName, fileName, &storage.SignedURLOptions{
        GoogleAccessID: s.Config.BucketServiceAccountDetails.ClientEmail,
        PrivateKey:     []byte(s.Config.BucketServiceAccountDetails.PrivateKey),
        Method:         http.MethodGet,
        Expires:        time.Now().Add(time.Second * 180),
    })
    if err != nil {
        return "", err
    }
    return url, nil
}

【问题讨论】:

    标签: api go google-cloud-storage http-status-code-403 bucket


    【解决方案1】:

    好的,我醒来后,找到了答案。 原来,当json编组成字符串时,所有特殊字符都被编码了。

    示例:&amp; -> \u0026

    所以我在 UT 中测试的 url 有 &amp;,而 api 返回的 url 有 \u0026,而 google 在这两种情况下似乎没有相同的行为。

    所以解决办法是禁用HTML转义:

    encoder := json.NewEncoder(w)
    encoder.SetEscapeHTML(false)
    return encoder.Encode(response)
    

    【讨论】:

      猜你喜欢
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-27
      • 2020-12-03
      • 2018-06-18
      • 2020-08-12
      • 2017-11-12
      相关资源
      最近更新 更多