我在谷歌存储中创建了一个存储桶并上传了一个示例文本文件并授予了所有者权限。
使用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 ')
答案 0 :(得分:0)
尝试:
bucket_name = os.environ.get('BUCKET_NAME', 'bucket-name-12345.appspot.com')
filename1 = 'https://storage.googleapis.com/' + bucket_name + '/test.txt'