【问题标题】:Generate and upload a file to s3 with flask使用烧瓶生成并上传文件到s3
【发布时间】:2020-02-25 18:43:17
【问题描述】:

是否可以生成文件并将其上传到 s3?

我试图实现这个烧瓶应用程序,但它因以下错误而死:

OSError: [Errno 30] Read-only file system: '/report.csv'

这是我的 app.py:

@app.route("/upload/<string:START_REPORT>/<string:STOP_REPORT>/<string:IDS>/", methods=['POST'])
def upload(START_REPORT, STOP_REPORT, IDS):
    """
    Function to upload a file to an S3 bucket
    """
    os.environ["START_REPORT"] = START_REPORT
    os.environ["STOP_REPORT"] = STOP_REPORT
    os.environ["IDS"] = IDS
    dft, filename = report_visit.report() 
    dft.to_csv(filename, index=False) # <-- Error here
    object_name = filename
    s3_client = boto3.client(
        "s3",
        region_name='eu-west-1',
        endpoint_url="url",
        aws_access_key_id="key",
        aws_secret_access_key="key"
    )
    with open(filename, "rb") as f:
        response = s3_client.upload_fileobj(f, app.config['UPLOAD_FOLDER'], f.filename)
    return response

if __name__ == '__main__':
    app.run (debug=True, port='8080', host='127.0.0.1')

其中文件名是“report.csv”

这是我对邮递员的发帖请求:

POST http://localhost:8080/upload/2020_2_10_0_0_0_0/2020_2_10_1_0_0_0/138/

我认为我很难给出一个合适的工作示例,但欢迎提出任何建议

函数report_visit.report()返回一个pandas数据框和文件名

任何建议都将不胜感激,谢谢

马特奥

【问题讨论】:

    标签: python flask amazon-s3


    【解决方案1】:

    您正在尝试写入根目录“/”。因此,您的应用程序需要 root 权限(使用 sudo 运行;安全思想强烈不推荐)或指定一些路径到 tmp 文件夹以将这些文件存储在那里(看起来您想使用“./report.csv” - 将创建文件进入工作目录)。

    【讨论】:

    • tmp文件夹必须在本地电脑还是s3服务器上?
    • @MatteoPeluso 肯定在生成此文件的计算机上
    猜你喜欢
    • 2014-02-09
    • 1970-01-01
    • 2020-08-30
    • 2021-07-03
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    相关资源
    最近更新 更多