简体   繁体   English

如何将文件从 Azure 存储复制到管道中代理池中的 VM?

[英]How to copy a file from Azure Storage to a VM in agent pool in a pipeline?

So, I'm setting up a pipeline where an agent pool assigns a virtual machine (VM) to do the work.因此,我正在设置一个管道,代理池会在其中分配一个虚拟机 (VM) 来完成工作。 One of the steps requires a large file to be used.其中一个步骤需要使用一个大文件。 That file is in Azure Storage (storage account.)该文件位于 Azure 存储(存储帐户。)

I'd like to be able to access the file through an azure service and not through the internet (if that makes sense?) For example, I don't want to provide a SAS token in the url and get it using AzureCLI.我希望能够通过 azure 服务而不是通过互联网访问文件(如果这有意义吗?)例如,我不想在 Z572D4E421E5E6B9BC11Z1D81E 中提供 SAS 令牌并获取它I want to be able to define the task in the pipeline using AzureFileCopy.我希望能够使用 AzureFileCopy 在管道中定义任务。

I read the docs over here but I don't know what I should put for sourcepath .我在这里阅读了文档,但我不知道应该为sourcepath放什么。 Is that the full URL for the file I need?那是我需要的文件的完整 URL 吗? Also, the VM is assigned from the pool.此外,VM 是从池中分配的。 I don't have a vmsAdminUserName or vmsAdminPassword to it.我没有vmsAdminUserNamevmsAdminPassword If I select destination to be a azureVMs , vmsAdminUserName or vmsAdminPassword are mandatory fields.如果我 select 目标是azureVMsvmsAdminUserNamevmsAdminPassword是必填字段。

I just want to have a file available on the VM the pool assigns to my pipeline!我只想在池分配给我的管道的 VM 上有一个可用的文件!

I think you misunderstand the usage od 'Azure File Copy'.我认为您误解了“Azure 文件复制”的用法。

Your requirement is get big file from azure storage.您的要求是从 azure 存储中获取大文件。

But 'Azure File Copy' is used for 'Copy Files to Azure Blob Storage or virtual machines'.但“Azure 文件复制”用于“将文件复制到 Azure Blob 存储或虚拟机”。

在此处输入图像描述

No built-in structure to achieve your requirements.没有内置结构来满足您的要求。

I write a code which can meet your requirements, you just need to pass required variables is ok(The underlying principle of this script is the same as the 'Azure File Copy task', which interacts with Azure Storage through the service principal.).我写了一个可以满足你要求的代码,你只需要传递所需的变量就可以了(这个脚本的基本原理与'Azure文件复制任务'相同,它通过服务主体与Azure Storage交互。)。

my pipeline:我的管道:

trigger:
- none

pool:
  name: VMAS #This is my agent pool, and this YAML is tested on Windows VM

steps:
- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      import requests
      import os
      from urllib.parse import urlencode
      
      grant_type = "client_credentials"
      client_id = "$(client_id)"
      client_secret = "$(client_secret)"
      resource = "https://storage.azure.com/"
      tenant_id = "$(tenant_id)"
      
      blobstorage_account_name = "<Your Storage account name>"
      blobstorage_container_name = "<Your Blob container Name>"
      blob_name = "SomeFiles/Folder1/Folder2/test.csv" #This is my blob name.
      
      keepstructure = True #Define whether keep the folder structure.
      
      def getazuretoken(grant_type,client_id,client_secret,resource):
          url = "https://login.microsoftonline.com/"+tenant_id+"/oauth2/token"
      
          payload='grant_type='+grant_type+'&client_id='+client_id+'&client_secret='+client_secret+'&resource='+resource
          headers = {
          'Content-Type': 'application/x-www-form-urlencoded'
          }
      
          response = requests.request("POST", url, headers=headers, data=payload)
          #get access token
          access_token = response.json()['access_token']
          return access_token
      
      
      token = getazuretoken(grant_type,client_id,client_secret,resource)
      
      
      #get azure blob using service principal
      def downloadazureblob(blob_name,keepstructure):
          
          blob_url = "https://"+blobstorage_account_name+".blob.core.windows.net/"+blobstorage_container_name+"/"+blob_name
          blob_headers = {
              'Content-Type': 'application/x-www-form-urlencoded'
          }
          blob_payload='Authorization=Bearer%20'+token
          
          blob_response = requests.request("GET", blob_url, headers=blob_headers,data=blob_payload)
          if keepstructure == True: #Save blob with folder structure
              #get folder structure
              folder_structure = blob_name.split('/')[:-1]
              #folder_structure to string
              folder_structure_string = '\\'+'\\'.join(folder_structure)+'\\'
              #for loop to create folder and file
              for i in range(blob_name.count('/')):
                  #split blob_name to get folder name
                  folder_name = blob_name.split('/')[i]
                  #create folder
                  os.mkdir(folder_name)
                  #move to folder
                  os.chdir(folder_name)
              #split blob_name to get file name
              file_name = blob_name.split('/')[blob_name.count('/')]
              cur_path = "$(Build.SourcesDirectory)"
              file_path = os.path.join(cur_path+folder_structure_string, file_name)
              #create file under folder_structure_string
              with open(file_path, 'wb') as f:
                  f.write(blob_response.content)
          elif keepstructure == False: #Save blob without folder structure
              #split blob_name to get file name
              file_name = blob_name.split('/')[-1]
              with open(file_name, 'wb') as f:
                  f.write(blob_response.content)
          else:
              print("Please input True or False")
          return blob_response.text
      
      
      downloadazureblob(blob_name,keepstructure)
- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
    includeRootFolder: true
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
    replaceExistingArchive: true

For secure reasons, please store the client_id, client-secret and tenant_id in the pipeline definition variables and secure them:出于安全原因,请将 client_id、client-secret 和tenant_id 存储在管道定义变量中并保护它们:

在此处输入图像描述

在此处输入图像描述

This is the original blob:这是原始的blob:

在此处输入图像描述

Successfully to Copy the files to VM:成功将文件复制到 VM:

在此处输入图像描述

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

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