2014-05-23 153 views
0

在寫入GCS文件之前,我想測試它是否存在。但是,我收到file.Stat返回的錯誤,該錯誤返回,我在appengine/fileappengine中看不到任何導出的錯誤,我可以對其進行測試。從App Engine確定GCS中不存在文件的最佳方法是什麼?如何判斷是否存在具有此名稱的文件?

有可能我這樣做的方式完全是錯誤的,還有一些其他方法可以確保我不覆蓋或追加到現有文件。如果有的話,我也很想聽到這個消息。

我的再現代碼:

package main 

import (
    "net/http" 
    "fmt" 

    "appengine" 
    "appengine/file" 
) 

func init() { 
    http.HandleFunc("/test", reproduceHandler) 
} 

func reproduceHandler(w http.ResponseWriter, r *http.Request) { 
    c := appengine.NewContext(r) 
    // You'll need your own bucket name here 
    _, err := file.Stat(c, "/gs/my-bucket-name/my-file-name") 
    fmt.Fprintln(w, err) 
} 

這說明如下當我訪問 「/測試」:

API error 100 (file: EXISTENCE_ERROR) 
+0

文檔也不多說了。如果文件存在或不存在,Stat應該返回。 – fabrizioM

+0

Stat返回一個'os.FileInfo'和一個'error'。在這種情況下,'os.FileInfo'是'','error'就像我的問題一樣。訣竅是從Stat可能返回的任何其他錯誤中消除該錯誤。 –

+0

查看源代碼:https://code.google.com/p/appengine-go/source/browse/appengine/file/file.go#26如果文件不存在,它看起來總是會失敗,因爲實施的開放限制。如果文件存在,那麼它將能夠返回一個stat對象。 – fabrizioM

回答

1

看看AppEngine上是如何定義的文件中的錯誤,在這裏:

https://code.google.com/p/appengine-go/source/browse/appengine_internal/files/file_service.pb.go

你應該能夠辨別錯誤t YPE基於枚舉頂部的文件中:

FileServiceErrors_OK         FileServiceErrors_ErrorCode = 0 
FileServiceErrors_API_TEMPORARILY_UNAVAILABLE  FileServiceErrors_ErrorCode = 1 
FileServiceErrors_REQUEST_TOO_LARGE     FileServiceErrors_ErrorCode = 3 
FileServiceErrors_RESPONSE_TOO_LARGE     FileServiceErrors_ErrorCode = 4 
FileServiceErrors_INVALID_FILE_NAME     FileServiceErrors_ErrorCode = 5 
FileServiceErrors_OPERATION_NOT_SUPPORTED   FileServiceErrors_ErrorCode = 6 
FileServiceErrors_IO_ERROR       FileServiceErrors_ErrorCode = 7 
FileServiceErrors_PERMISSION_DENIED     FileServiceErrors_ErrorCode = 8 
FileServiceErrors_WRONG_CONTENT_TYPE     FileServiceErrors_ErrorCode = 9 
FileServiceErrors_FILE_NOT_OPENED     FileServiceErrors_ErrorCode = 10 
FileServiceErrors_WRONG_OPEN_MODE     FileServiceErrors_ErrorCode = 11 
FileServiceErrors_EXCLUSIVE_LOCK_REQUIRED   FileServiceErrors_ErrorCode = 12 
FileServiceErrors_FILE_TEMPORARILY_UNAVAILABLE  FileServiceErrors_ErrorCode = 13 
FileServiceErrors_EXISTENCE_ERROR     FileServiceErrors_ErrorCode = 100 
FileServiceErrors_FINALIZATION_ERROR     FileServiceErrors_ErrorCode = 101 
FileServiceErrors_UNSUPPORTED_CONTENT_TYPE   FileServiceErrors_ErrorCode = 102 
FileServiceErrors_READ_ONLY       FileServiceErrors_ErrorCode = 103 
FileServiceErrors_EXCLUSIVE_LOCK_FAILED    FileServiceErrors_ErrorCode = 104 
FileServiceErrors_EXISTENCE_ERROR_METADATA_NOT_FOUND FileServiceErrors_ErrorCode = 105 
FileServiceErrors_EXISTENCE_ERROR_METADATA_FOUND  FileServiceErrors_ErrorCode = 106 
FileServiceErrors_EXISTENCE_ERROR_SHARDING_MISMATCH FileServiceErrors_ErrorCode = 107 
FileServiceErrors_FINALIZATION_IN_PROGRESS   FileServiceErrors_ErrorCode = 108 
FileServiceErrors_EXISTENCE_ERROR_OBJECT_NOT_FOUND FileServiceErrors_ErrorCode = 109 
FileServiceErrors_EXISTENCE_ERROR_BUCKET_NOT_FOUND FileServiceErrors_ErrorCode = 110 
FileServiceErrors_SEQUENCE_KEY_OUT_OF_ORDER   FileServiceErrors_ErrorCode = 300 
FileServiceErrors_OUT_OF_BOUNDS      FileServiceErrors_ErrorCode = 500 
FileServiceErrors_GLOBS_NOT_SUPPORTED    FileServiceErrors_ErrorCode = 600 
FileServiceErrors_FILE_NAME_NOT_SPECIFIED   FileServiceErrors_ErrorCode = 701 
FileServiceErrors_FILE_NAME_SPECIFIED    FileServiceErrors_ErrorCode = 702 
FileServiceErrors_FILE_ALREADY_EXISTS    FileServiceErrors_ErrorCode = 703 
FileServiceErrors_UNSUPPORTED_FILE_SYSTEM   FileServiceErrors_ErrorCode = 704 
FileServiceErrors_INVALID_PARAMETER     FileServiceErrors_ErrorCode = 705 
FileServiceErrors_SHUFFLER_INTERNAL_ERROR   FileServiceErrors_ErrorCode = 800 
FileServiceErrors_SHUFFLE_REQUEST_TOO_LARGE   FileServiceErrors_ErrorCode = 801 
FileServiceErrors_DUPLICATE_SHUFFLE_NAME    FileServiceErrors_ErrorCode = 802 
FileServiceErrors_SHUFFLE_NOT_AVAILABLE    FileServiceErrors_ErrorCode = 803 
FileServiceErrors_SHUFFLER_TEMPORARILY_UNAVAILABLE FileServiceErrors_ErrorCode = 900 
FileServiceErrors_MAX_ERROR_CODE      FileServiceErrors_ErrorCode = 9999 

你得到的錯誤是最有可能的FileServiceErrors_ErrorCode型(或指針指向型),所以檢查和比較對您想辨別的情況下通過使用一個類型斷言:

_, err := file.Stat(c, "/gs/my-bucket-name/my-file-name") 

if apiErr, ok := err.(*appengine_internal.APIError); ok { 
    if apiErr.Code == int32(files.FileServiceErrors_EXISTENCE_ERROR) && apiErr.Service == "file" { 
     // file does not exist 
    } 
} 

不要忘記

import "appengine_internal" 
import "appengine_internal/files" 
+0

完美!感謝編輯添加細節。在編輯之前,我不太確定如何做到這一點,但現在它非常清晰。 –

+0

實際上,我仍在四處尋找,但這告訴我'files.FileServiceErrors_ErrorCode'沒有實現錯誤(缺少Error方法)。 –

+1

原來,錯誤的類型是'* appengine_internal.APIError',它的'Code'屬性被設置爲(在本例中)'fi​​les.FileServiceErrors_ErrorCode'。我已經編輯了您的答案,以在樣本解決方案中考慮到這一點。 –

相關問題