【问题标题】:NotFoundError: Expect status [200] from Google Storage. But got status 404 while reading a fileNotFoundError:期望来自 Google 存储的状态 [200]。但是在读取文件时得到状态 404
【发布时间】:2018-01-13 13:47:23
【问题描述】:

我在谷歌存储中创建了一个存储桶并上传了一个示例文本文件并授予所有者权限。

使用google docs 中的代码,我正在尝试读取上传的文本文件(test.txt)并创建一个示例文本文件。我无法同时执行读取/创建操作。

连接到云存储:

bucket_name = os.environ.get(
        'BUCKET_NAME', 'bucket-name-12345.appspot.com')

错误 - “NotFoundError:期望来自 Google 存储的状态 [200]。但得到状态 404。
路径:'/bucket-name-12345.appspot.com/test'

代码:

def get_cloudstorage(self,site,start_date):
    bucket_name = os.environ.get(
        'BUCKET_NAME', 'bucket-name-12345.appspot.com')
    log.info(" in the cloud storage get method")
    bucket = '/' + bucket_name
    filename = bucket + '/sample'
    filename1 = bucket + '/test.txt'
    self.tmp_filenames_to_clean_up = []
    self.create_file(filename)
    self.read_file(filename1)
    
 # Writing a file to Cloud Storage
def create_file(self, filename):
    """Create a file."""
    # self.response.write('Creating file {}\n'.format(filename))
    # The retry_params specified in the open call will override the default
    # retry params for this particular file handle.
    log.info(" in the cloud storage create_file  method")
    write_retry_params = cloudstorage.RetryParams(backoff_factor=1.1)
    with cloudstorage.open(
        filename, 'w', content_type='text/plain', options={
            'x-goog-meta-foo': 'foo', 'x-goog-meta-bar': 'bar'},
            retry_params=write_retry_params) as cloudstorage_file:
                cloudstorage_file.write('abcde\n')
                cloudstorage_file.write('f'*1024*4 + '\n')
    self.tmp_filenames_to_clean_up.append(filename)
    log.info(" end of the create method ")  
    
def read_file(self, filename):
    with cloudstorage.open(filename) as cloudstorage_file:)
        cloudstorage_file.seek(-1024, os.SEEK_END)
        log.info(cloudstorage_file.read())
    log.info('end of the read_file  ')

【问题讨论】:

  • 您请求的文件似乎是test,而不是test.txt...
  • NotFoundError:期望来自 Google 存储的状态 [200]。但状态为 404。路径:'/bucket-name-12345.appspot.com/test.txt'。请求标头:无。
  • 好的。那么您需要显示代码,您的帖子中没有足够的信息来弄清楚发生了什么。
  • 请再过一遍帖子,我已经添加了代码
  • 尝试将您的bucket_name 设置为app_identity.get_default_gcs_bucket_name(),如示例中所示(并出于调试目的打印它) - 我怀疑除了您的应用程序名称之外可能存在不匹配或一些额外的字符.或者尝试读取您在上一步中创建的文件。您在创建文件时是否也看到错误?

标签: python python-2.7 google-app-engine google-cloud-storage gcloud


【解决方案1】:

试试:

bucket_name = os.environ.get('BUCKET_NAME', 'bucket-name-12345.appspot.com')
filename1 = 'https://storage.googleapis.com/' + bucket_name + '/test.txt'

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-04-04
  • 2014-01-10
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
相关资源
最近更新 更多