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
질문됨 2년 전1414회 조회
3개 답변
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.

답변함 2년 전
  • 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.

답변함 2년 전
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
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠