I am creating a web application that prompts the user to upload a file, which is stored in a Google Cloud Storage bucket. I use a parsing function (package on pip to extract the data, which takes a filepath and loads the specified file.
The path for the file in the bucket is
gs://my_bucket/myfile.ged
,
but this file can't be found when I pass the path to the parsing function. When run locally, it parses the file as expected. If I deploy the file in the project folder with the script, it works. But when run on App engine on Google Cloud Platform, it cannot find the file.
The problem is similar to described here. This is how I would expect it to work.
f = request.files['fileToUpload']
blob = bucket.blob("myfile.ged")
blob.upload_from_file(f)
gs_path = 'gs://my_bucket/myfile.ged'
parsing_function(gs_path)
And I guess I shouldn't be too surprised that the following testing function always returns 'empty':
def testing():
var = 'emtpy'
filename = 'gs://my_bucket/myfile.ged'
if(os.path.exists(filename)):
var = 'filename'
else:
blobs = bucket.list_blobs()
for blob in blobs:
if(os.path.exists(blob.path)):
var = blob.path
return var
I've tried using the temp_file method, which gives the TypeError: expected str, bytes or os.PathLike object, not _TemporaryFileWrapper
with TemporaryFile() as temp_file:
blob.download_to_file(temp_file)
temp_file.seek(0)
parsing_function(gs_path)
I've also tried:
- Obtaining the blob.path, but it comes in a format /b/my_bucket/o/myfile.ged that also can't be found.
The I/O method described here:
filepath = BytesIO() blob.download_to_file(filepath) parsing_function(filepath)
But this also retuns the TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO
So after an exhaustive search, I've come here for help. Any suggestions, or alternatives would be greatly appreciated.