How upload file in s3 and after generate presigned url in python

0

Im using api boto3 for upload files in subfolders s3, but when i try generate url, the file is downloaded. if I include the file directly through the s3 interface, the url works normally, but when the file comes from the api the file is downloaded. I believe that the way I'm using the upload is causing this problem. how do i solve this? the code below is what i am using to upload >>>> import boto3 file_path = r'C:\users\Documents\file.pdf' file_name = 'file.pdf' folder_name = 'folder' s3_client = boto3.client('s3') s3_client.upload_file(file_path, S3_BUCKET, folder_name + '/{}'.format(file_name))

JrdoBem
asked 2 years ago1366 views
3 Answers
0

Could these statements be returning an exception you are not catching?

formupload = FormUpload()

if formupload.validate_on_submit():
    item_drop_down = formupload.pesquisa_instrumento.data #variable to folder_name
    arquivo = formupload.arquivo.data #variable to file_name

    if not arquivo:
        flash('Arquivo não selecionado','alert-danger')
        return render_template('partitura-incluir.html', formupload=formupload)

Or possibly the return statement? Sorry for the long delay.

answered 2 years ago
  • this sentence is a treaty if you have not selected any files. Sorry this code is portuguese.

0

Did you see this document? https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html

I've coded in python how to upload files, and this is what I've used successfully:

    upload_file_response = s3.upload_file(source, destination, key, ExtraArgs=extra_args, Callback=callback,
                                          Config=config)

Source is the file with path, destination is the bucket, key is the key you want displayed in the bucket, the other parms are optional.

Display the results you get in upload_file_response. It might help you debug why you are getting an error. Also, you will need to catch exceptions.

You could probably do with just s3.exceptions.ClientError and botocore.exceptions.ClientError. Note: I use s3 instead of s3_client. Hope this helps.

answered 2 years ago
0

Yes i do! I have no problems with uploads, I want the files that were uploaded to be able to load a valid url to view the text files. I've coded this way in python to upload files in subfolders.

@app.route('/partitura-incluir', methods=['POST', 'GET'])
@login_required
def partitura_incluir():
    formupload = FormUpload()

    if formupload.validate_on_submit():
        item_drop_down = formupload.pesquisa_instrumento.data #variable to folder_name
        arquivo = formupload.arquivo.data #variable to file_name

        if not arquivo:
            flash('Arquivo não selecionado','alert-danger')
            return render_template('partitura-incluir.html', formupload=formupload)
        try:
            file = secure_filename(arquivo.filename)
            arquivo.save(file)
            s3_client = boto3.client('s3')
            s3_client.upload_file(file, S3_BUCKET, item_drop_down + '/{}'.format(arquivo.filename))
            flash(f'included {arquivo.filename} on subfolder {item_drop_down.capitalize()}', 'alert-success')

            return redirect(url_for('pasta_consultar'))
        except Exception as error:
            print(error)

    return render_template('partitura-incluir.html', formupload=formupload)
JrdoBem
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions