首页 文章

NotFoundError:期待来自Google Storage的状态[200] . 但在读取文件时获得状态404

提问于
浏览
1

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

使用google docs中的代码,我试图读取上传的文本文件(test.txt)并创建示例文本文件 . 我不能同时进行读/创操作 .

连接到 Cloud 存储:

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  ')

1 回答

  • 0

    尝试:

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

相关问题