2

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.

1
  • Not sure if you realize this, but the filenotfound error looks like it's saying it can't find the file you are wanting to upload. It appears that you're specifying a "relative" file path, which means it is looking for the file in the app's working directory. You need to add some logging to determine where the app is looking, and then compare that to where the file actually is. Commented Jan 31, 2019 at 19:22

2 Answers 2

2

You're missing the step where you actually save the file:

filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)

For Google App Engine, the UPLOAD_FOLDER must be /tmp.

Once you've saved the file, you can then upload it to Google Cloud Storage:

name = '/{}/{}'.format(bucket_name, filename)
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = storage.Blob(name, bucket)
blob.upload_from_filename(filepath)
Sign up to request clarification or add additional context in comments.

3 Comments

line 42, in upload_file file.save(filepath) File "/env/lib/python3.6/site-packages/werkzeug/datastructures.py", line 2703, in save dst = open(dst, 'wb') FileNotFoundError: [Errno 2] No such file or directory: '/home/kristian_whl/files/377bdc67c74336b6_108.mp4' I get this error from file.save(filepath) line. I think I need to use the Google Clouds buckets.
You need to set app.config['UPLOAD_FOLDER'] to a directory that exists on your machine if you're running this locally. The directory /home/kristian_whl/files/ doesn't seem to exist.
I didn't add the "/tmp" to UPLOAD_FOLDER. Thanks a lot, you were a great help!
0

I think the line:

blob = storage.Blob(name, bucket)

should really be

blob = storage.Blob(filename, bucket)

At least that's how it's working for me. Otherwise it ends up in a folder nested in the bucket with the same name as the bucket.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.