如何判断具有此名称的文件是否存在?

时间:2014-05-23 00:02:29

标签: google-app-engine go google-cloud-storage

在写入GCS文件之前,我想测试它是否存在。但是,我从file.Stat返回了false中的os.IsNotExist错误,我在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)
}

当我访问“/ test”时显示以下内容:

API error 100 (file: EXISTENCE_ERROR)

1 个答案:

答案 0 :(得分:1)

看看appengine如何定义文件错误,请点击此处:

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

您应该能够根据文件中的枚举顶部识别错误类型:

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"