简体   繁体   English

当文件在本地存储中时,如何使用 boto3 将文件上传到 s3

[英]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首先,从用户那里获取输入作为 excel 表单

second, process it with python code,其次,用python代码处理它,

third, upload it to aws s3 with boto3第三,使用boto3将其上传到aws s3

But I am having a trouble uploading to s3但是我在上传到 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我首先创建了 s3 对象,然后

        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我已经得到文件为 file = request.files['file'],并将它传递给我之前定义的函数

Now, the file which I want to upload to S3 is 'result object' , which is the result of select_bestitem function现在,我想上传到 S3 的文件是 'result object' ,这是 select_bestitem 函数的结果

But I don't know what to pass to Filename argument但我不知道传递给 Filename 参数的是什么

It seems like I have to give file path to it, but I can't find the path of file stored in localstorage好像我必须给它提供文件路径,但我找不到存储在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) (我使用 Pandas to_csv 命令创建了“结果”对象,看起来 boto3 以某种方式拒绝了这种类型)

I am quite new to python and flask stuff, so any help would be great!我对python和flask的东西很陌生,所以任何帮助都会很棒! 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. request.files['file']给出文件指针,使用该指针,您可以将文件保存到某个位置。 The path where the file will be saved can be done using os.path.join(UPLOAD_FOLDER, f.filename) as shown below:保存文件的路径可以使用os.path.join(UPLOAD_FOLDER, f.filename) ,如下所示:

@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:之后,可以看出,我调用了upload_file方法,该方法将写入s3,该函数的代码如下:

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!让我知道这是否有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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