NotFoundError: Expect status [200] from Google Storage. But got status 404 while reading a file

白昼怎懂夜的黑 提交于 2019-12-11 02:27:39

问题


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

回答1:


Try:

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


来源:https://stackoverflow.com/questions/48240489/notfounderror-expect-status-200-from-google-storage-but-got-status-404-while

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!