1
votes

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
It looks like you're requesting a file named test, not test.txt... - Dan Cornilescu
NotFoundError: Expect status [200] from Google Storage. But got status 404. Path: '/bucket-name-12345.appspot.com/test.txt'. Request headers: None. - Bhavana
OK. then you need to show the code, there's not enough info in your post to figure out what's going on. - Dan Cornilescu
please go through the post again, i have added the code - Bhavana
Try to set your bucket_name to app_identity.get_default_gcs_bucket_name() as in the example (and print it for debug purposes) - I suspect there might be a mismatch or some extra chars in there besides just your app's name. Or try reading the file you created at the previous step. Do you see errors at creating the file, too? - Dan Cornilescu

1 Answers

0
votes

Try:

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