如何使用Azure函数应用将文件从桌面上传到Azure Blob存储

发布于 2025-02-13 10:35:57 字数 1854 浏览 1 评论 0 原文

我正在尝试通过Azure函数应用上传一些文件。首先,我阅读了一些Excel文件,然后使用PANDA进行一些清洁过程,并想将其上传到Azure功能应用程序。离线每个作品都很好,但是当我发布到Azure时,它给出了错误500 。 我猜它无法从我的PC获取文件。

我的代码看起来像这样:

#I get my files offline
AISauftragID = req.params.get('ID','756382')
Position = req.params.get('Position','7')
location = fr'C:\Users\bb\ff\Doc\VS Code\{ID}-{Position}'
#Some Cleaning using pandas
#connecting to Azure
blob_service_client = BlobServiceClient.from_connection_string("blablabla")
# Instantiate a new ContainerClient
container_client = blob_service_client.get_container_client('foldername')
try:
    # Create new Container in the service
    container_client.create_container()
    properties = container_client.get_container_properties()
except ResourceExistsError:
    print("Container already exists.")
blob = BlobClient.from_connection_string(conn_str="blablabla",
                                                container_name="foldername",
                                                blob_name="filename") 
if blob.exists():
        logging.info('filename.csv exists')  
        return func.HttpResponse(
            "filename.csv exists",
            status_code=200)
        
else:
    blob_client = container_client.get_blob_client("filename.csv")
    # upload data
    blob_client.upload_blob(dataframe.to_csv(index=False, encoding = "utf-8"), blob_type="BlockBlob")
    return func.HttpResponse(
            "Konto.csv exists",
            status_code=200)

我的function.json文件看起来像这样:

    {
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
   
  ]
}

I'm trying to Upload some files form my PC through a azure function app. At first i read some excel files then i do some Cleaning processes using Pandas and i want to upload it to Azure function app. offline every works fine, but as i published to Azure it gives Error 500.
I'm guessing it can't get files from my PC.

my code looks like this:

#I get my files offline
AISauftragID = req.params.get('ID','756382')
Position = req.params.get('Position','7')
location = fr'C:\Users\bb\ff\Doc\VS Code\{ID}-{Position}'
#Some Cleaning using pandas
#connecting to Azure
blob_service_client = BlobServiceClient.from_connection_string("blablabla")
# Instantiate a new ContainerClient
container_client = blob_service_client.get_container_client('foldername')
try:
    # Create new Container in the service
    container_client.create_container()
    properties = container_client.get_container_properties()
except ResourceExistsError:
    print("Container already exists.")
blob = BlobClient.from_connection_string(conn_str="blablabla",
                                                container_name="foldername",
                                                blob_name="filename") 
if blob.exists():
        logging.info('filename.csv exists')  
        return func.HttpResponse(
            "filename.csv exists",
            status_code=200)
        
else:
    blob_client = container_client.get_blob_client("filename.csv")
    # upload data
    blob_client.upload_blob(dataframe.to_csv(index=False, encoding = "utf-8"), blob_type="BlockBlob")
    return func.HttpResponse(
            "Konto.csv exists",
            status_code=200)

my function.Json file looks like this:

    {
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
   
  ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

背叛残局 2025-02-20 10:35:57

我猜它无法从我的PC获取文件。

那是正确的。 Azure函数在Azure云数据中心的某个地方运行。该功能无法访问本地工作站的本地硬盘。另外,您真的不想这样做,因为这将是主要的安全风险。

如前所述,您似乎有使用Azure函数上传本地文件的想法。您需要一些客户端进程(例如浏览器或应用程序)将文件发送到Azure,然后可以编写Azure函数或其他处理该文件。

要处理一个文件,您要么创建一个http触发的azure函数,该函数接受文件内容或您将文件上传例如,azure blob存储并创建触发斑点 / 事件网格触发 azure函数来处理文件。

I'm guessing it can't get files from my PC.

That is correct. The azure function runs somewhere in an Azure Cloud data center. There is no way this function can access the local harddrive of an on-premises workstation. Also, you really don't want to because it would be a major security risk.

You seem to have the idea to use an azure function for uploading the local file, as stated, that is a no go. You need some client process like a browser or application to send the file to azure and then you can write an azure function or whatever to process that file.

To process a file you either create an http triggered azure function that accepts a POST action with the file content or you upload the file to, for example, azure blob storage and create a blob triggered / event grid triggered azure function to process the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文