2013-06-20 22 views
2

我有這樣的代碼:ForbiddenError當試圖從GAE python的文件寫入GCS應用

def save_to_gcs(self, img, img_obj): 
    ''' 
    Image data, Image metadata object -> Blob Key 
    Given an image and image metadata, stores it in a GCS bucket 
    ''' 
    bucket = '/foo' 
    filename = bucket + '/' + str(img_obj['filename']) 

    self.tmp_filenames_to_clean_up = [] 

    logging.info('Creating file %s\n' % img_obj['filename']) 

    write_retry_params = gcs.RetryParams(backoff_factor=1.1) 
    gcs_file = gcs.open(filename, 
         'w', 
         content_type=img_obj['mimetype'], 
         retry_params=write_retry_params) 
    gcs_file.write(img) 
    gcs_file.close() 
    self.tmp_filenames_to_clean_up.append(filename) 

    return blobstore.create_gs_key('/gs/' + filename) 

但它失敗,此錯誤:

Expect status [201] from Google Storage. But got status 403. Response headers: {'content-length': '145', 'via': 'HTTP/1.1 GWA', 'x-google-cache-control': 'remote-fetch', 'expires': 'Fri, 01 Jan 1990 00:00:00 GMT', 'server': 'HTTP Upload Server Built on Jun 7 2013 11:30:13 (1370629813)', 'pragma': 'no-cache', 'cache-control': 'no-cache, no-store, must-revalidate', 'date': 'Thu, 20 Jun 2013 23:13:55 GMT', 'content-type': 'application/xml; charset=UTF-8'} 
Traceback (most recent call last): 
    File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__ 
    rv = self.handle_exception(request, response, e) 
    File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__ 
    rv = self.router.dispatch(request, response) 
    File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher 
    return route.handler_adapter(request, response) 
    File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1102, in __call__ 
    return handler.dispatch() 
    File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 572, in dispatch 
    return self.handle_exception(e, self.app.debug) 
    File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch 
    return method(*args, **kwargs) 
    File "/base/data/home/apps/s~foo/5.368231578716365248/main.py", line 409, in post 
    blob_key = self.save_to_gcs(img, img_obj) # Save the image to a GCS bucket. returns a blob_key 
    File "/base/data/home/apps/s~foo/5.368231578716365248/main.py", line 448, in save_to_gcs 
    retry_params=write_retry_params) 
    File "/base/data/home/apps/s~foo/5.368231578716365248/external/cloudstorage/cloudstorage_api.py", line 69, in open 
    return storage_api.StreamingBuffer(api, filename, content_type, options) 
    File "/base/data/home/apps/s~foo/5.368231578716365248/external/cloudstorage/storage_api.py", line 527, in __init__ 
    errors.check_status(status, [201], headers) 
    File "/base/data/home/apps/s~foo/5.368231578716365248/external/cloudstorage/errors.py", line 99, in check_status 
    raise ForbiddenError(msg) 
ForbiddenError: Expect status [201] from Google Storage. But got status 403. Response headers: {'content-length': '145', 'via': 'HTTP/1.1 GWA', 'x-google-cache-control': 'remote-fetch', 'expires': 'Fri, 01 Jan 1990 00:00:00 GMT', 'server': 'HTTP Upload Server Built on Jun 7 2013 11:30:13 (1370629813)', 'pragma': 'no-cache', 'cache-control': 'no-cache, no-store, must-revalidate', 'date': 'Thu, 20 Jun 2013 23:13:55 GMT', 'content-type': 'application/xml; charset=UTF-8'} 

與破譯的錯誤和未來的任何幫助與解決方案將不勝感激。

謝謝

回答

6

同樣的事情發生在我身上,它讓我感到困惑。按照先決條件部分中的this page的步驟完成工作。一對夫婦,雖然指出:

  • 對於2號,請確保你去APIs Console和服務下打開GCS
  • 對於5號,轉到Cloud Console,選擇項目,單擊設置扳手,然後點擊團隊。在這裏添加你的gserviceaccount.com的東西。
  • 對於數字5,我認爲你必須用gsutil編輯ACL文件。按照提供的替代指示。

這應該爲你工作,因爲它爲我做了。

+0

謝謝。實際上,我放棄了使用* deprecated *文件API將文件寫入blobstore。我現在沒有時間來測試您的答案,但無論如何都會將其標記爲正確,因爲它*很有用。 –

9

該文檔令人困惑的是授予訪問您的應用程序引擎應用程序(這很可能是您的問題)。下面是我在最新Google Cloud Console什麼工作:

  1. 在谷歌雲端控制檯,點擊您的項目,然後選擇「的API &身份驗證」。打開「Google雲端存儲」和「Google雲端存儲JSON API」。
  2. 單擊「概覽」屏幕,然後單擊左側菜單中的「雲存儲」。
  3. 單擊存儲桶旁邊的複選框,然後單擊「存儲桶權限」按鈕。
  4. 添加新的「用戶」權限並指定您的應用引擎服務帳戶名稱,格式爲[email protected]。這可以在AppEngine控制檯的應用程序設置中找到。找到這個帳戶名稱的更好的說明是here
  5. 保存更改。
+0

一切都很棒 – Tjorriemorrie

+0

今天這個工作,謝謝你! – Aerodyno

+0

我無法在任何地方找到關於添加新的「用戶」權限或格式[email protected]的說明。我發現的一切都是默認的東西。非常感謝。 –

1

...除上述之外,還請到「API的」(在「API和驗證」項目下的「雲端控制檯」,並確保「谷歌雲存儲」已開啓。 和在「結算」,確保已開啓。

1

這並沒有從我的工作。 谷歌說,我們要做的下面提到的方法。

授予權限,以你的水桶或對象。 要啓用應用程序來創建一個存儲桶中的新對象,您需要執行以下操作:

登錄到App Engine管理控制檯。 單擊您要爲雲存儲存儲桶授權的應用程序。 單擊左側「管理」部分下的「應用程序設置」。 複製服務帳戶名稱下的值。這是您的應用程序的服務帳戶名稱,格式爲[email protected]。如果您使用的是App Engine Premier帳戶,則應用程序的服務帳戶名稱格式爲[email protected]。 使用以下方法之一授予訪問權限: 嚮應用程序授予應用程序訪問權限的最簡單方法是使用Google Cloud Console將應用程序的服務帳戶名稱作爲團隊成員添加到包含存儲區的項目。您可以在Google雲端控制檯左側的權限下執行此操作。如果需要寫入存儲區,該應用程序應具有編輯權限。有關雲存儲中權限的信息,請參閱作用域和權限。如果需要,可以向項目組添加更多應用程序 注意:在某些情況下,您可能無法將該服務帳戶添加爲團隊成員。如果您無法添加服務帳戶,請使用替代方法(存儲桶ACL),如下所述。