Reputation: 41
I want to download a single LFS file from gitLab using c#. Currently I have the following:
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
*/
In my research I came across this link, which is helpful.
https://gitlab.com/gitlab-org/gitlab/-/issues/27892#note_395085312
But I'm having a hard time setting this up in 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')"
Here's what I've attempted and I'm able to get the oid and size. Any help would be greatly appreciated.
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";
Upvotes: 0
Views: 783
Reputation: 41
Thank you so much for your response. This works up to a point, returning a pointer file. I ended up having to create a second json request using the pointer info that was returned from the original json. The result then provides the download url. From there, extract, convert to byte array, write to file, and check to see that the lfs was created. :) This was my first time dealing with this so I hope this is somewhat helpful.
Upvotes: -2
Reputation: 40941
Use the repository files API. No special handling is needed for files that happen to use LFS. It's simply an HTTPS request to the endpoint.
You've already requested the information which should be contained in pkgResult
. The content of the file is in the response payload as content
encoded in base64.
Just extract the content
field from the deserialized JSON and base64 decode it to get the contents of the file.
Upvotes: 1