【问题标题】:unable to upload file in azure blob无法在 azure blob 中上传文件
【发布时间】:2020-01-31 03:07:18
【问题描述】:

我正在尝试将文件上传到 Azure Blob 存储。我的应用程序托管在 Azure 应用服务 Linux 服务器中。 现在,当我请求从远程机器上传文件时,我希望从给定路径上传文件。

我有三个请求参数,它们采用值形式的 GET 请求

  1. https://testApp.azurewebsites.net/blobs/fileUpload/
  2. 容器名称:测试
  3. 文件名:testFile.txt
  4. 文件路径:C:\Users\testUser\Documents

    @app.route("/blobs/fileUpload/")
    def fileUpload():
    
       container_name = request.form.get("containerName")
       print(container_name)
       local_file_name =request.form.get("fileName")
       print(local_file_name)
       local_path =request.form.get('filePath')
       ntpath.normpath(local_path)
       print(local_path)
       full_path_to_file=ntpath.join(local_path,local_file_name)
       print(full_path_to_file)
       # Upload the created file, use local_file_name for the blob name
       block_blob_service.create_blob_from_path(container_name, 
       local_file_name, full_path_to_file)
       return jsonify({'status': 'fileUploaded'})
    

local_path =request.form.get('filePath') 我从请求中得到的值是 C:\Users\testUser\Documents\ 因为我得到了这个错误

OSError: [Errno 22] 无效参数:'C:\Users\testUser\Documents\testFile.txt'

我想要的只是获得与我在请求中发送的相同路径。由于应用程序托管在 Linux 机器中,如果我使用 OS.path,它会将路径视为 UNIX 文件系统

请帮帮我

【问题讨论】:

  • 您还有其他问题吗?
  • 谢谢@Ivan,我明白了,该文件应该在本地存在以使用 create_blob_from_path。但是如果有一个客户端-服务器场景说我想从客户端请求文件上传,应该怎么做呢?
  • 由于您使用的是网络应用程序,这里有一个issue,它使用 django 框架将本地文件上传到 blob 存储。

标签: python azure rest file azure-blob-storage


【解决方案1】:

根据错误消息,“C:\Users\testUser\Documents\testFile.txt”的本地路径无效。说明你的本地系统中没有这样的文件路径。

如果你想使用create_blob_from_path方法,你应该先将文件下载到本地系统,然后使用该方法上传到blob存储。

或者你可以从远程获取文件的流/文本,然后分别使用create_blob_from_stream/create_blob_from_text方法。

【讨论】:

    猜你喜欢
    • 2021-12-30
    • 2013-01-01
    • 2019-10-31
    • 2011-02-07
    • 1970-01-01
    • 2021-09-07
    • 2013-10-04
    • 2014-01-23
    • 2017-03-14
    相关资源
    最近更新 更多