https://lakefs.io/ logo
Title
a

Ayush Paudel

04/27/2023, 7:46 AM
Hi, I am beginner at lakefs and was playing a bit with lakectl’s upload and download. Also I came across the python SDK for lakefs but didnt see the download api. I am not sure if I missed it. Anyone knows?
i

Idan Novogroder

04/27/2023, 8:40 AM
Hi @Ayush Paudel, Does get_object is what you are looking for?https://pydocs.lakefs.io/docs/ObjectsApi.html#get_object And I think
lakectl fs download
is what you are looking for in lakectl https://docs.lakefs.io/reference/cli.html#lakectl-fs-download
a

Ayush Paudel

04/27/2023, 12:04 PM
Thankyou, @Idan Novogroder yes, get_object works for me but I see its downloading at /var/ path why is that. Cannot we assign the download path ourself?
i

Idan Novogroder

04/27/2023, 12:38 PM
As far as I know, It downloads the file to the current working directory. I think you can change it in Python like this:
os.chdir("your_new_download_path")
a

Ayush Paudel

04/27/2023, 12:39 PM
ohh. Thank you🙏
i

Idan Novogroder

04/27/2023, 12:40 PM
Sure, let me know if it worked 🙂
a

Ayush Paudel

04/27/2023, 12:49 PM
It does not seem to work that way. Here is my code
import lakefs_client
from lakefs_client.api import objects_api
from pprint import pprint
import os

# The client must configure the authentication and authorization parameters
# in accordance with the API server security policy.
# Examples for each auth method are provided below, use the example that
# satisfies your auth use case.

# Configure HTTP basic authorization: basic_auth
configuration = lakefs_client.Configuration(
    username="AKIAJNGGC7PVSVQG7G3Q",
    password="3NUzNiTiJdGtCP1urPyLztoW/XHy7yPBhzHFK48L",
    host="<http://localhost:8000/api/v1>",
)


# Enter a context with an instance of the API client
with lakefs_client.ApiClient(configuration) as api_client:
    # Create an instance of the API class
    api_instance = objects_api.ObjectsApi(api_client)
    repository = "example-repo"  # str |
    ref = "main"  # str | destination branch for the copy
    path = "test.py"  # str | destination path relative to the branch

    # example passing only required values which don't have defaults set
    try:
        os.chdir("/Users/ayushpaudel/codebase/lakefs-poc/")
        # create a copy of an object
        api_response = api_instance.get_object(repository, ref, path)
        pprint(api_response)
    except lakefs_client.ApiException as e:
        print("Exception when calling ObjectsApi->copy_object: %s\n" % e)
and this is the output
-> python3 lakefs-sdk.py
<_io.BufferedReader name='/var/folders/jk/jv05v3l53pl4kx83db_wmr840000gn/T/tmpl2ohp9m7'>
also the filename is random everytime🤔
b

Barak Amar

04/27/2023, 1:35 PM
Hi @Ayush Paudel, the return
file_type
from
get_object
is
io.BufferedReader
as it say in the output. You will need to read the content to your desired location.
So for example if you like to save it to a file you can open one and write the content:
with client.objects.get_object("repository", "branch", "README.md") as r, \
    open('README.md', 'wb') as w:
    w.write(r.read())
👍 1
a

Ayush Paudel

04/27/2023, 2:01 PM
thanks @Barak Amar. It worked Previously I was assuming it to be like
lakectl fs download
where we do specify download path. thats why
b

Barak Amar

04/27/2023, 2:08 PM
The lakectl leverage the same API so if you like to read the code and do the same check out https://github.com/treeverse/lakeFS/blob/8e801bed3d39043bec2f65128d307a9d18744bc9/cmd/lakectl/cmd/fs.go#L461 🙂
:gratitude-thank-you: 1