I have an Android app that will send a video file through HTTP request. However, when I try to send the file the Google Cloud will give an error. I am using Python 3.
There is a problem in the second last line blob.upload_from_filename(filename) and I want to know how to fix this problem.
Here is the code:
import os
from google.cloud import storage
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
bucket_name = 'Censored.appspot.com'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'mp4'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['SECRET_KEY'] = "TESTSTRING"
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = '/{}/{}'.format(bucket_name, filename)
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = storage.Blob(filename, bucket)
blob.upload_from_filename(filename)
return "Hello World!"
Here is the error that it gives when uploading:
Traceback (most recent call last):
File "", line 1
Traceback (most recent call last):
File "/env/lib/python3.6/site-packages/flask/app.py",
line 2292, in wsgi_app response = self.full_dispatch_request()
File "/env/lib/python3.6/site-packages/flask/app.py",
line 1815, in full_dispatch_request rv = self.handle_user_exception(e)
File "/env/lib/python3.6/site-packages/flask/app.py",
line 1718, in handle_user_exception reraise(exc_type, exc_value, tb)
File "/env/lib/python3.6/site-packages/flask/_compat.py",
line 35, in reraise raise value File "/env/lib/python3.6/site-packages/flask/app.py",
line 1813, in full_dispatch_request rv = self.dispatch_request()
File "/env/lib/python3.6/site-packages/flask/app.py",
line 1799, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args)
File "/home/vmagent/app/__init__.py",
line 32, in upload_file blob.upload_from_filename(filename)
File "/env/lib/python3.6/site-packages/google/cloud/storage/blob.py",
line 1021, in upload_from_filename with open(filename, 'rb') as file_obj:
FileNotFoundError: [Errno 2] No such file or directory: '377bdc67c74336b6_101.mp4'
^
SyntaxError: invalid syntax
As a sidenote; in my local Flask server I use:
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
This of course doesn't work in Google cloud as it uses buckets.
I am getting desperate here. I think, I am missing some common knowledge about the Google Cloud.