Gitlab API 在没有 lfs 安装 C# 的情况下下载 LFS 文件

Gitlab API download LFS file without lfs install C#

我想使用 C# 从 gitLab 下载单个 LFS 文件。目前我有以下内容:

string pkgUri = $"{projectURL}files/{pgkFilePath}/raw?private_token={projectToken}&ref=master";
string pkgResult = await client.GetStringAsync(pkgUri);

/*
result: 
DATA: version https://git-lfs.github.com/spec/v1
oid sha256:h2d00e87q97a965c6b125ce3ee2fe35ea4e66c12345w7129740bd40645a3bbd0
size 812345
*/

在我的研究中,我遇到了这个 link,这很有帮助。

https://gitlab.com/gitlab-org/gitlab/-/issues/27892#note_395085312

但我很难在 c# 中进行设置:

# request actions for given lfs objects
LFS_OBJECT_REQUEST_JSON="$(jq -rnc --arg oid $OID --arg size $SIZE --arg ref "refs/heads/$BRANCH" '{"operation":"download","objects":[{"oid":$oid,"size":$size}],"transfers":["lfs-standalone-file","basic"],"ref":{"name": $ref }}')"

LFS_OBJECT_ACTION_DOWNLOAD="$(set -o pipefail; curl -sSlf -H "Content-Type: application/json" -X POST -d "$JSON" "https://oauth2:${GITLAB_TOKEN}@gitlab.com/${PROJECT}.git/info/lfs/objects/batch" | jq -er '.objects[0].actions.download')"

这是我尝试过的方法,我能够获得 oid 和大小。任何帮助将不胜感激。

    AssetEntry pkgEntry = JsonConvert.DeserializeObject<AssetEntry>(pkgResult);
    string size = pkgEntry.size;
    string oid = pkgEntry.content_sha256;
                            
    Console.WriteLine($"OID: {oid}\nSIZE: {size}"); // This works

    //Get package
    string downloadLFS = $"{projectURL}info/lfs/objects/{oid}?private_token={projectToken}&ref=master";

使用repository files API。碰巧使用 LFS 的文件不需要特殊处理。它只是对端点的 HTTPS 请求。

您已经请求了应该包含在pkgResult中的信息。文件的内容在响应有效负载中作为 content 以 base64 编码。

只需从反序列化的JSON和base64 decode it中提取content字段即可获取文件内容。

非常感谢您的回复。这在一定程度上起作用,返回一个指针文件。我最终不得不使用从原始 json 返回的指针信息创建第二个 json 请求。然后结果提供下载url。从那里提取、转换为字节数组、写入文件并检查是否创建了 lfs。 :) 这是我第一次处理这个问题,所以我希望这会有所帮助。