【问题标题】:Azure-storage-blob create_container fails in dockerized Django appAzure-storage-blob create_container 在 dockerized Django 应用程序中失败
【发布时间】:2020-02-28 04:13:16
【问题描述】:

我有一个 django 视图,它应该接受带有压缩文件的 POST 请求并将它们上传到 Azure 存储 blob 容器。

这个视图在我的本地机器上工作,但一旦容器化和部署失败,无论是作为 Azure 云服务上的 web 应用程序还是作为我本地机器上的 Docker 容器。

一开始我以为是权限问题,因为:

在本地运行应用程序 - 发送 zipfile 返回 204(成功)

  • 如果我断开 VPN,发送 zipfile 会返回 500 我还没有弄清楚如何通过 azure 运行它来获得 500。

  • 添加网络规则后,允许我当前的 IP 地址(后来我删除了),发送 zip 文件再次返回 204。但是,我必须将 IP 地址添加到存储容器和数据库中(因为 API 必须检查令牌)。

在 azure 中运行应用程序

  • 发送压缩文件返回 502

  • 将 IP 地址添加到存储容器和数据库仍然返回 502

  • 添加整个 IP 地址范围(Azure Web 应用程序有多个可能的,一个虚拟的)不会改变这一点。 有与该应用程序关联的第三个可能的 IP 地址列表,但它适用于主要应用程序。我认为这不适用,但我想我可以回去检查一下。

隔离问题: 尽管收到 502,而不是我要求的任何错误代码,但我已将问题隔离到一行:

block_blob_service.create_container(container_name)

如果没有这一行,我会收到相应的错误代码。 在本地运行此行,在 REPL 中,我能够得到响应,除非我离开 VPN,在这种情况下,它表示请求未经授权。通过 AZ CLI 添加网络规则可以解决此问题。

此外,谷歌搜索向我指出了存储容器的 --default-action 设置,以及前面提到的 IP 想法。 --default-action 是允许或拒绝通过新的/随机 IP 地址创建容器。 将此设置更改为“允许” 让我在本地创建容器,关闭 VPN,使用 REPL 中的第 120 行没有明确的 IP 规则 改回“拒绝” 不允许我在本地创建容器,使用第 120 行关闭 VPN。 引发错误。 这两种设置都不会影响 Azure 的 502 错误 我认为这可能是一个超时问题,因为 502 错误只发生在这个页面上。因此,这不是 gunicorn/server 配置错误。所以我给 block_blob_service 对象添加了一个超时值。这没有帮助。

我终于在本地加载了容器并通过 ssh'ed 进入它以尝试获得更多的调试输出。看来 View 正在进入身份验证循环并导致 python 崩溃。

这是负责上传的视图:

class ZipUpload(APIView):

    authentication_classes = [TokenAuthentication]
    permission_classes = (IsAuthenticated,)
    parser_classes = [FileUploadParser]
    parser_classes = [MultiPartParser]

    def post(self, request, format=None):
        # Todo: add isAuthenticated check and response upon failure.
        try:
            try:
                # Create the container from the settings file (fluxdb_clinical/settings_test.py, or prod, depending on stack).
                account_name = settings.BLOCK_BLOB_SERVICE['account_name']
                account_key = settings.BLOCK_BLOB_SERVICE['account_key']
                block_blob_service = BlockBlobService(account_name=account_name, account_key=account_key, socket_timeout=60)
                container_name = settings.BLOCK_BLOB_SERVICE['container_name']
                # this next line breaks on the azure web-app platform. I think the container/blob must be
                # IP restricted in some way, as this line works on my dev machine until I leave the VPN
                block_blob_service.create_container(container_name)
                # Disable public access, restricting access to requests using keys.
                block_blob_service.set_container_acl(container_name, public_access=None)
            except Exception as e:
                logger.exception(e)
                return(Response(status=STATUS['container_creation_failure']))

            try:
                # Create a filename, using the timestamp to ensure unique names.
                # Do we need to make a way to name the file by the actual test name? Or should I use the token to look up the user
                # and make a name that way so we can sort zipfiles by user?
                now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
                filename = 'zipfile' + str(now) + '.zip'
            except:
                return(Response(status=STATUS['dunno why would be breaking shit']))


            try:
                file_obj = request.FILES['archive']
            except:
                return(Response(status=STATUS['archive_retrieval_failure']))


            try:
                zipp = file_obj.read()
                block_blob_service.create_blob_from_bytes(container_name, filename, zipp)
            except Exception as e:
                return(Response(status=STATUS['zip_write_failure']))

            return Response(status=STATUS['successful'])
        except Exception as e:
            return(Response(e))


如果问题与凭据或令牌身份验证相关,那么无论应用程序在何处运行,我都希望失败发生。此外,如果我错误地实例化容器,我希望它到处都失败。

据我所知,http 请求(由该方法使用)和 docker 之间的交互导致了问题。但是,我可以在 docker 容器中打开一个 python REPL 并在那里运行它而不会出现问题。

这是在 Docker 容器中运行的服务器的输出 - 这似乎表明存在身份验证循环。






.
.
.
  File "/usr/local/lib/python3.6/site-packages/azure_storage_logging/handlers.py", line 73 in put_file_into_storage
  File "/usr/local/lib/python3.6/site-packages/azure_storage_logging/handlers.py", line 152 in doRollover
  File "/usr/local/lib/python3.6/logging/handlers.py", line 72 in emit
  File "/usr/local/lib/python3.6/logging/__init__.py", line 865 in handle
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1516 in callHandlers
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1454 in handle
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1444 in _log
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1308 in info
  File "/usr/local/lib/python3.6/site-packages/azure/storage/common/storageclient.py", line 330 in _perform_request
  File "/usr/local/lib/python3.6/site-packages/azure/storage/blob/baseblobservice.py", line 693 in create_container
  File "/usr/local/lib/python3.6/site-packages/azure_storage_logging/handlers.py", line 73 in put_file_into_storage
  File "/usr/local/lib/python3.6/site-packages/azure_storage_logging/handlers.py", line 152 in doRollover
  File "/usr/local/lib/python3.6/logging/handlers.py", line 72 in emit
  File "/usr/local/lib/python3.6/logging/__init__.py", line 865 in handle
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1516 in callHandlers
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1454 in handle
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1444 in _log
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1308 in info
  File "/usr/local/lib/python3.6/site-packages/azure/storage/common/storageclient.py", line 330 in _perform_request
  File "/usr/local/lib/python3.6/site-packages/azure/storage/blob/baseblobservice.py", line 693 in create_container
  File "/usr/local/lib/python3.6/site-packages/azure_storage_logging/handlers.py", line 73 in put_file_into_storage
  File "/usr/local/lib/python3.6/site-packages/azure_storage_logging/handlers.py", line 152 in doRollover
  File "/usr/local/lib/python3.6/logging/handlers.py", line 72 in emit
  File "/usr/local/lib/python3.6/logging/__init__.py", line 865 in handle
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1516 in callHandlers
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1454 in handle
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1444 in _log
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1308 in info
  File "/usr/local/lib/python3.6/site-packages/azure/storage/common/storageclient.py", line 330 in _perform_request
  File "/usr/local/lib/python3.6/site-packages/azure/storage/blob/baseblobservice.py", line 693 in create_container
  File "/usr/local/lib/python3.6/site-packages/azure_storage_logging/handlers.py", line 73 in put_file_into_storage

.
.
.

  File "/usr/local/lib/python3.6/site-packages/azure/storage/blob/baseblobservice.py", line 693 in create_container
  File "/usr/local/lib/python3.6/site-packages/azure_storage_logging/handlers.py", line 73 in put_file_into_storage
  File "/usr/local/lib/python3.6/site-packages/azure_storage_logging/handlers.py", line 152 in doRollover
  File "/usr/local/lib/python3.6/logging/handlers.py", line 72 in emit
  File "/usr/local/lib/python3.6/logging/__init__.py", line 865 in handle
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1516 in callHandlers
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1454 in handle
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1444 in _log
  File "/usr/local/lib/python3.6/logging/__init__.py", line 1308 in info
  File "/usr/local/lib/python3.6/site-packages/azure/storage/common/storageclient.py", line 330 in _perform_request
  File "/usr/local/lib/python3.6/site-packages/azure/storage/blob/baseblobservice.py", line 693 in create_container
  ...
[2019-11-01 21:47:57 +0000] [33] [INFO] Booting worker with pid: 33



我已经用虚线换行符截断了输出。

感谢任何帮助。这是我第二次发帖,我已经尽力做到彻底,但如果我遗漏了什么,请告诉我。

谢谢。

编辑:我找到了解决方案。

我的 pipfile 仅包含 azure-storage-logging 和 azure-storage-blob。通过安装整个天蓝色轮(pip install azure 然后pip freeze >> requirements.txt),它运行良好。

【问题讨论】:

    标签: python-3.x docker django-views azure-blob-storage


    【解决方案1】:

    我尝试比较pip install azure-storage-logging azure-storage-blob安装的包和pip install azure安装的包,并从azure包中删除azure-storage这些不相关的包得到列表,如下图。

    我认为您可以尝试仅添加左侧列表中缺少的这些包(不包括azure),它也适合您。

    希望对你有帮助。

    【讨论】:

    • 谢谢你,我会试一试的。我认为它也会起作用,因为现在我想起来了,当我第一次构建 requirements.txt 文件时,我只是添加了两个模块,而不是将 pip-freeze 导出到文件中。
    猜你喜欢
    • 2022-12-15
    • 2022-11-17
    • 2020-07-15
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 2019-03-17
    • 2020-06-15
    • 1970-01-01
    相关资源
    最近更新 更多