NotFoundError:预期来自Google存储的状态[200]。但在阅读文件时获得状态404 [英] NotFoundError: Expect status [200] from Google Storage. But got status 404 while reading a file
本文介绍了NotFoundError:预期来自Google存储的状态[200]。但在阅读文件时获得状态404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Google存储中创建了一个存储桶,并上传了一个示例文本文件并授予了所有者权限。 使用 google文档,我正在阅读上传的文本文件(test.txt)并创建一个示例文本文件。我无法同时执行读取/创建操作。
连接到云端存储:
bucket_name = os.environ.get(
'BUCKET_NAME','bucket-name-12345.appspot.com')
错误 - NotFoundError:Google Storage的预期状态[200],但状态为404.
路径: '/bucket-name-12345.appspot.com/test'
代码:
def get_cloudstorage(self,site,start_date):
bucket_name = os.environ.get(
'BUCKET_NAME','bucket- '
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(文件名)
self.read_file(filename1)
#将文件写入Cloud Stor age
def create_file(self,filename):
创建一个文件。
#self.response.write('Creating file {} \\\
'.format(文件名))
#打开调用中指定的retry_params将覆盖此特定文件句柄的默认
#retry参数。
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\\\
')
cloudstorage_file.write('f'* 1024 * 4 +'\\\
')
self.tmp_filenames_to_clean_up。 append(filename)
log.info(创建方法结束)
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('read_file'的结尾)
解决方案
试试:
bucket_name = os.environ.get('BUCKET_NAME','bucket-name-12345.appspot.com')
filename1 ='https://storage.googleapis.com /'+ bucket_name +'/test.txt'
I have created a bucket in google storage and uploaded a sample text file and gave owner permissions.
Using the code in google docs, I am trying to read the uploaded text file (test.txt) and create a sample text file. I couldn't do both read/create operations.
Connecting to cloud storage:
bucket_name = os.environ.get(
'BUCKET_NAME', 'bucket-name-12345.appspot.com')
Error - "NotFoundError: Expect status [200] from Google Storage. But got status 404.
Path: '/bucket-name-12345.appspot.com/test'
Code:
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 ')
解决方案
Try:
bucket_name = os.environ.get('BUCKET_NAME', 'bucket-name-12345.appspot.com')
filename1 = 'https://storage.googleapis.com/' + bucket_name + '/test.txt'
这篇关于NotFoundError:预期来自Google存储的状态[200]。但在阅读文件时获得状态404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文