【问题标题】:Download or pull a file from azure git repo using rest api使用 rest api 从 azure git repo 下载或提取文件
【发布时间】:2021-07-16 07:23:45
【问题描述】:

我的要求是从 azure git repo 下载/提取文件并将其转换为字节数组。我在 Azure git repo API 中进行了搜索,但找不到其余的 api 调用。请帮忙解决。

我尝试使用以下 url,但它在内容对象中返回 unicode 值。

GET https://dev.azure.com{organization}/{project}/_apis/git/repositories/{repositoryId}/items?path={path}&versionDescriptor.version={versionDescriptor.version}&versionDescriptor.versionType ={versionDescriptor.versionType}&includeContent=true&api-version=6.0

【问题讨论】:

  • 嗨,Harshita,以下答案对您有用吗?请检查并告知我们结果。

标签: java azure-devops-rest-api


【解决方案1】:

您是说要使用来自 Microsoft azure 的获取请求吗?我可能会建议使用 fetch 来检索您的数据。类似于:

fetch("https://westus.api.cognitive.microsoft.com/face/v1.0/detect? returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=emotion&recognitionModel= 
   recognition_01&returnRecognitionModel=false&detectionModel=detection_01"
            , {
                method: 'post',
                headers: {
                    'Content-Type': 'application/octet-stream',
                    'Ocp-Apim-Subscription-Key': '<subscription key>'
                },
                body: makeblob(contents)
            }).then((response) => response.json()).then(success => {
                that.setState({selectedFile: url1});

            that.setState({facesArray: success});
            console.log("facesArray is", that.state.facesArray);
            console.log("new selected state is", that.state.selectedFile);
            // console.log(success);
        }).catch(error =>
            console.log("did not work ",error))

我知道我正在使用发布请求,但如果您更改订阅密钥、正文、获取 url 和内容类型,您可能会得到您正在寻找的内容。此外,您可以通过某种方式联系 microsoft azure services 以获取有关其 api 的更多帮助。

【讨论】:

  • 我正在使用 Azure devops git repo。
  • @Harshita 没关系。您需要一个 HTTP 库来执行您的 GET 请求。只需确保您在必要时进行了身份验证。您在发出 GET 请求时需要帮助吗?
【解决方案2】:

你试过吗?确保将 httpclient 添加到您的 gradle/madle 构建中。将azurePathString 替换为您的网址。

public byte[] executeBinary(URI uri) throws IOException, ClientProtocolException {
    HttpGet httpget = new HttpGet(azurePathString);
    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    entity.writeTo(baos);
    return baos.toByteArray();
}

【讨论】:

    【解决方案3】:

    您可以使用 API:https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/items?path={path}&amp;versionDescriptor.version={versionDescriptor.version}&amp;download=true&amp;api-version=6.0 下载目标文件,然后读取该文件并将其内容转换为字节数组。请参阅:Items - Get 了解更多详情。

    【讨论】:

    • 嗨,Harshita,我的回答有效吗?请检查它并请告诉我结果。
    猜你喜欢
    • 2020-09-17
    • 2020-12-08
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多