简体   繁体   中英

how to upload file to s3 with boto3, while the file is in localstorage

What I am trying to do is

first, get the input as an excel form from the user

second, process it with python code,

third, upload it to aws s3 with boto3

But I am having a trouble uploading to s3

s3 = boto3.client(
"s3",
aws_access_key_id=access_key,
aws_secret_access_key=secret_key
)
bucket_resource = s3

I created s3 object first, and

        excel_file = pd.read_excel(file.stream)
    try:
        result = compute.select_bestitem(excel_file, brand_name, col_name, methods, operator, value)
        filename = secure_filename(file.filename)
        bucket_resource.upload_file(
            Bucket=bucket_name,
            Filename=,
            Key=filename
        )

I already got file as file = request.files['file'], and passed it to the function I defined earlier

Now, the file which I want to upload to S3 is 'result object' , which is the result of select_bestitem function

But I don't know what to pass to Filename argument

It seems like I have to give file path to it, but I can't find the path of file stored in localstorage

Plus, I am really not sure if it works even if I pass the correct file path, since the type of the file

I am trying to upload is string

(I created the 'result' object with Pandas to_csv command, and it looks like somehow boto3 rejects this type)

I am quite new to python and flask stuff, so any help would be great! Thanks in advance :)

Yeah, you are right. We need to give the path to the file which needs to be uploaded. request.files['file'] gives the file pointer and using that pointer, you can save the file into a location. The path where the file will be saved can be done using os.path.join(UPLOAD_FOLDER, f.filename) as shown below:

@app.route("/upload", methods=['POST'])
def upload():
    if request.method == "POST":
        f = request.files['file']
        file_path=os.path.join(UPLOAD_FOLDER, f.filename) # path where file can be saved
        f.save(file_path)
        upload_file(file_path, BUCKET) # send the file path
        return redirect("/storage")

After that, as it can be seen, I called upload_file method which will write to s3 and the code for that function is given below:

BUCKET = "flaskdrive"
AWS_ACCESS_KEY="aws_access_key"
AWS_SECERT_KEY="aws_secret_key"

def upload_file(file_name, bucket):
    """
    Function to upload a file to an S3 bucket
    """
    object_name = file_name
    s3_client = boto3.client('s3',
                             aws_access_key_id=AWS_ACCESS_KEY,
                             aws_secret_access_key=AWS_SECERT_KEY)
    response = s3_client.upload_file(filename=file_name, bucket=bucket, key=object_name)

    return response

Let me know if this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM