0

Problem Description:

I'm encountering an issue with file uploads to MinIO using Python code. When I upload files with the the Python script, the uploaded files appear in the MinIO bucket but have no size and cannot be opened. However, when I manually upload files, they are viewable and have a size.

What I've Tried:

Verified MinIO bucket settings: I have confirmed that the MinIO bucket settings are correct and that both manually uploaded files and files uploaded via the code are stored in the correct bucket.

Checked content type: I'm setting the content type correctly using the get_content_type() function to ensure that the uploaded files are recognized correctly.

Expected Outcome:

I expect that files uploaded via the Python script should be viewable and have a size, similar to manually uploaded files.

from minio import Minio
from minio.error import S3Error
from flask import Flask, render_template, send_file
from flask_wtf import FlaskForm
from wtforms import FileField, SubmitField
from werkzeug.utils import secure_filename
import os
from wtforms.validators import InputRequired
import mimetypes

minio_client = Minio(endpoint="play.min.io",
                     access_key="Q3AM3UQ867SPQQA43P2F",
                     secret_key="zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")

app = Flask(__name__)
app.config['SECRET_KEY'] = 'supersecretkey'

class UploadFileForm(FlaskForm):
    file = FileField("File", validators=[InputRequired()])
    submit = SubmitField("Upload File")

def get_content_type(filename):
    content_type, _ = mimetypes.guess_type(filename)
    if not content_type:
        extension = os.path.splitext(filename)[1].lower()
        if extension in ('.jpg', '.jpeg', '.png', '.gif', '.bmp'):
            content_type = f'image/{extension.lstrip(".")}'
    return content_type or 'application/octet-stream'

@app.route('/', methods=['GET', 'POST'])
@app.route('/home', methods=['GET', 'POST'])
def home():
    form = UploadFileForm()
    if form.validate_on_submit():
        file = form.file.data
        filename = secure_filename(file.filename)
        bucket_name = "saving-files" 
        try:
            
            content_type = get_content_type(filename)

            minio_client.put_object(bucket_name, filename, file, file.content_length, content_type=content_type)
            return "File has been uploaded to MinIO."
        except S3Error as e:
            return f"Error uploading file to MinIO: {e}"

    return render_template('index.html', form=form)

@app.route('/files/<filename>', methods=['GET'])
def get_file(filename):
    bucket_name = "saving-files"  
    try:
        file_data = minio_client.get_object(bucket_name, filename)
        return send_file(file_data, attachment_filename=filename)
    except Exception as e:
        return f"Error retrieving file from MinIO: {e}"

if __name__ == '__main__':
    app.run(debug=True)

1 Answer 1

0

You are not reading the file from the request.FILES.

# read the file data
file_data = request.FILES[form.file.name].read()
# pass the file data to the `client.put_object`
minio_client.put_object(bucket_name, filename, file_data, file.content_length, content_type=content_type)

https://wtforms.readthedocs.io/en/2.3.x/fields/#wtforms.fields.FileField

Sign up to request clarification or add additional context in comments.

1 Comment

That could true, but my main problem other than this is that the files that being uploaded to minio aren't being sent correctly, they are being shown corrupted.

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.