Skip to content

Add folder to IPFS - helpful details #2012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

Conversation

ProximaNova
Copy link
Contributor

@ProximaNova ProximaNova commented Mar 26, 2025

Describe your changes

Adds an example on how to add a directory (not just a single file) by using curl and /api/v0/add. API docs are pretty clear as I've read about all of the page. The one unclear part is about adding folders.

Files changed

  • ipfs-docs/blob/main/tools/http-api-docs/markdown.go

What issue(s) does this address?

  • None. No GitHub issue/PR on this found. Maybe similar to adding directories: Clarify how to use the x-directory header in Curl requests. #704 . About that, as far as I know, the only way to add a folder by using /api/v0/add is to specify one or multiple files plus the options in this PR. You cannot have a path to a folder and expect /api/v0/add to work. Or can you? (Something like curl -XPOST -F "file=@folder/;filename=folder" 127.0.0.1:5001/api/v0/add?) The documentation is unclear and this PR helps to fix that.

Does this update depend on any other PRs?

  • No

Checklist before requesting a review

  • Passing the beta version of the Check Markdown links for modified files check. Action results can be viewed here.

Checklist before merging

  • Passing all required checks (The beta Check Markdown links for modified files check is not required)

@ProximaNova
Copy link
Contributor Author

The "Files changed" tab shows a basic way of adding a folder via the API. I haven't come up with a great automated way to add any folder. Not really important for this PR but still worth noting - add current working directory to IPFS via the HTTP API:

# Works, but fails if whitespace is in any filename, no eval is used:
filesf() { basedir="$(pwd)"; basedirlen=$(echo "$basedir" | wc --bytes); find "$basedir" -type f | basedirlen="$basedirlen" xargs -d "\n" sh -c 'for args do nobasedir=$(echo "$args" | sed -E "s/^.{$basedirlen}//g"); echo " -F "file=@"\"$args\";filename=\"$nobasedir\"" | tr -d \\n; done' _; }
curl -sLk -XPOST -H "Content-Type: multipart/form-data" $(filesf) "127.0.0.1:5001/api/v0/add?cid-version=1&chunker=size-1048576&recursive=true&wrap-with-directory=true&pin=false"

# Works on any filename, but uses eval
eval $(echo "arr=($(basedir="$(pwd)"; basedirlen=$(echo "$basedir" | wc --bytes); find "$basedir" -type f | basedirlen="$basedirlen" xargs -d "\n" sh -c 'for args do nobasedir=$(echo "$args" | sed -E "s/^.{$basedirlen}//g"); echo "\"file=@$args;filename=$nobasedir\" " | tr -d \\n; done' _ | sed "s/ $//g"))")
toexe="$(i=0; text='curl -sLk -XPOST -H "Content-Type: multipart/form-data"'; while [[ i -lt ${#arr[@]} ]]; do text="$text -F \"${arr[$i]}\""; echo $text; ((i++)); done | tail -n1 | sed "s/$/ \"127.0.0.1:5001\/api\/v0\/add?cid-version=1\&chunker=size-1048576\&recursive=true\&wrap-with-directory=true\&pin=false\"/g")"
eval $(echo "$toexe")

@2color
Copy link
Member

2color commented Mar 27, 2025

This file shouldn't be edited directly.

# Kubo RPC API v0 reference
<!-- DO NOT EDIT THIS FILE.
This file is auto-generated.
Any changes you make to this file will be overwritten.
To edit this file, change the contents of the https://github.com/ipfs/ipfs-docs/blob/main/tools/http-api-docs/markdown.go template.

@lidel
Copy link
Member

lidel commented Mar 28, 2025

Let's continue in #2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants