CINXE.COM

Reading Files in Invenio-Files-Record - Community - Invenio

<!DOCTYPE html> <html lang="en-GB"> <head> <meta charset="utf-8"> <title>Reading Files in Invenio-Files-Record - Community - Invenio</title> <meta name="description" content="Hi I was exploring Invenio-Files-Record module for the user to access the content of the files without having to download the files locally. My goal is for the user to process on the file without actually downloading it &amp;hellip;"> <meta name="generator" content="Discourse 3.5.0.beta1-dev - https://github.com/discourse/discourse version 4a7a9efbb95c1976f56c3bddc36a9236eade8fa4"> <link rel="icon" type="image/png" href="https://invenio-talk.web.cern.ch/uploads/default/optimized/1X/_129430568242d1b7f853bb13ebea28b3f6af4e7_2_32x32.png"> <link rel="apple-touch-icon" type="image/png" href="https://invenio-talk.web.cern.ch/uploads/default/optimized/1X/_129430568242d1b7f853bb13ebea28b3f6af4e7_2_180x180.png"> <meta name="theme-color" media="all" content="#0377cd"> <meta name="color-scheme" content="light"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, viewport-fit=cover"> <link rel="canonical" href="https://invenio-talk.web.cern.ch/t/reading-files-in-invenio-files-record/340" /> <link rel="search" type="application/opensearchdescription+xml" href="https://invenio-talk.web.cern.ch/opensearch.xml" title="Invenio Search"> <link href="/stylesheets/color_definitions_invenio-colors_3_3_78717404254d1ec7295464cdb414dbe578ccec05.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" class="light-scheme"/> <link href="/stylesheets/desktop_32d4b9f3691d5368f2eacb10ff7a08a4055d57ca.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="desktop" /> <link href="/stylesheets/checklist_32d4b9f3691d5368f2eacb10ff7a08a4055d57ca.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="checklist" /> <link href="/stylesheets/discourse-details_32d4b9f3691d5368f2eacb10ff7a08a4055d57ca.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="discourse-details" /> <link href="/stylesheets/discourse-lazy-videos_32d4b9f3691d5368f2eacb10ff7a08a4055d57ca.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="discourse-lazy-videos" /> <link href="/stylesheets/discourse-local-dates_32d4b9f3691d5368f2eacb10ff7a08a4055d57ca.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="discourse-local-dates" /> <link href="/stylesheets/discourse-narrative-bot_32d4b9f3691d5368f2eacb10ff7a08a4055d57ca.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="discourse-narrative-bot" /> <link href="/stylesheets/discourse-presence_32d4b9f3691d5368f2eacb10ff7a08a4055d57ca.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="discourse-presence" /> <link href="/stylesheets/discourse-solved_32d4b9f3691d5368f2eacb10ff7a08a4055d57ca.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="discourse-solved" /> <link href="/stylesheets/discourse-templates_32d4b9f3691d5368f2eacb10ff7a08a4055d57ca.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="discourse-templates" /> <link href="/stylesheets/discourse-voting_32d4b9f3691d5368f2eacb10ff7a08a4055d57ca.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="discourse-voting" /> <link href="/stylesheets/footnote_32d4b9f3691d5368f2eacb10ff7a08a4055d57ca.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="footnote" /> <link href="/stylesheets/poll_32d4b9f3691d5368f2eacb10ff7a08a4055d57ca.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="poll" /> <link href="/stylesheets/spoiler-alert_32d4b9f3691d5368f2eacb10ff7a08a4055d57ca.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="spoiler-alert" /> <link href="/stylesheets/discourse-voting_desktop_32d4b9f3691d5368f2eacb10ff7a08a4055d57ca.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="discourse-voting_desktop" /> <link href="/stylesheets/poll_desktop_32d4b9f3691d5368f2eacb10ff7a08a4055d57ca.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="poll_desktop" /> <link href="/stylesheets/desktop_theme_3_5df97cdc18ad8ad890b86b5c67d94411ef6ea99e.css?__ws=invenio-talk.web.cern.ch" media="all" rel="stylesheet" data-target="desktop_theme" data-theme-id="3" data-theme-name="shades of blue"/> <link rel="alternate nofollow" type="application/rss+xml" title="RSS feed of &#39;Reading Files in Invenio-Files-Record&#39;" href="https://invenio-talk.web.cern.ch/t/reading-files-in-invenio-files-record/340.rss" /> <meta property="og:site_name" content="Invenio" /> <meta property="og:type" content="website" /> <meta name="twitter:card" content="summary" /> <meta name="twitter:image" content="https://invenio-talk.web.cern.ch/uploads/default/original/1X/365b3da954a64ad5402137fe610018898acec6a9.png" /> <meta property="og:image" content="https://invenio-talk.web.cern.ch/uploads/default/original/1X/365b3da954a64ad5402137fe610018898acec6a9.png" /> <meta property="og:url" content="https://invenio-talk.web.cern.ch/t/reading-files-in-invenio-files-record/340" /> <meta name="twitter:url" content="https://invenio-talk.web.cern.ch/t/reading-files-in-invenio-files-record/340" /> <meta property="og:title" content="Reading Files in Invenio-Files-Record" /> <meta name="twitter:title" content="Reading Files in Invenio-Files-Record" /> <meta property="og:description" content="Hi I was exploring Invenio-Files-Record module for the user to access the content of the files without having to download the files locally. My goal is for the user to process on the file without actually downloading it , just by calling the invenio db methods and work on it. I looked through the content of uploading and retrieving files to the bucket, but none of these has methods to actually read the content of the file, can someone please help on how do actually read or access the content to..." /> <meta name="twitter:description" content="Hi I was exploring Invenio-Files-Record module for the user to access the content of the files without having to download the files locally. My goal is for the user to process on the file without actually downloading it , just by calling the invenio db methods and work on it. I looked through the content of uploading and retrieving files to the bucket, but none of these has methods to actually read the content of the file, can someone please help on how do actually read or access the content to..." /> <meta property="og:article:section" content="Community" /> <meta property="og:article:section:color" content="12A89D" /> <meta property="article:published_time" content="2024-01-31T19:09:21+00:00" /> <meta property="og:ignore_canonical" content="true" /> </head> <body class="crawler browser-update"> <header> <a href="/"> Invenio </a> </header> <div id="main-outlet" class="wrap" role="main"> <div id="topic-title"> <h1> <a href="/t/reading-files-in-invenio-files-record/340">Reading Files in Invenio-Files-Record</a> </h1> <div class="topic-category" itemscope itemtype="http://schema.org/BreadcrumbList"> <span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"> <a href="/c/community/11" class="badge-wrapper bullet" itemprop="item"> <span class='badge-category-bg' style='background-color: #12A89D'></span> <span class='badge-category clear-badge'> <span class='category-name' itemprop='name'>Community</span> </span> </a> <meta itemprop="position" content="1" /> </span> </div> </div> <div itemscope itemtype='http://schema.org/DiscussionForumPosting'> <meta itemprop='headline' content='Reading Files in Invenio-Files-Record'> <link itemprop='url' href='https://invenio-talk.web.cern.ch/t/reading-files-in-invenio-files-record/340'> <meta itemprop='datePublished' content='2024-01-31T19:09:21Z'> <meta itemprop='articleSection' content='Community'> <meta itemprop='keywords' content=''> <div itemprop='publisher' itemscope itemtype="http://schema.org/Organization"> <meta itemprop='name' content='Invenio'> <div itemprop='logo' itemscope itemtype="http://schema.org/ImageObject"> <meta itemprop='url' content='https://invenio-talk.web.cern.ch/uploads/default/original/1X/365b3da954a64ad5402137fe610018898acec6a9.png'> </div> </div> <div id='post_1' class='topic-body crawler-post'> <div class='crawler-post-meta'> <span class="creator" itemprop="author" itemscope itemtype="http://schema.org/Person"> <a itemprop="url" rel='nofollow' href='https://invenio-talk.web.cern.ch/u/VentusTWY'><span itemprop='name'>VentusTWY</span></a> </span> <link itemprop="mainEntityOfPage" href="https://invenio-talk.web.cern.ch/t/reading-files-in-invenio-files-record/340"> <span class="crawler-post-infos"> <time datetime='2024-01-31T19:09:21Z' class='post-time'> 31 January 2024 19:09 </time> <meta itemprop='dateModified' content='2024-01-31T19:09:21Z'> <span itemprop='position'>1</span> </span> </div> <div class='post' itemprop='text'> <p>Hi I was exploring Invenio-Files-Record module for the user to access the content of the files without having to download the files locally. My goal is for the user to process on the file without actually downloading it , just by calling the invenio db methods and work on it.</p> <p>I looked through the <a href="https://invenio-records-files.readthedocs.io/en/latest/usage.html" rel="noopener nofollow ugc">content of uploading and retrieving files to the bucket</a>, but none of these has methods to actually read the content of the file, can someone please help on how do actually read or access the content to the file?</p> <p>I have tried something like</p> <p>files = Record(record)<br> fileobj = record.files[‘hello.txt’]</p> <p>with fileobj.storage.open(‘rb’) as file:<br> file_content = file.read().decode(‘utf-8’)</p> <p>and</p> <p>file_content = fileobj.read().decode(‘utf-8’)<br> print(file_content)</p> <p>but none of these seems to be working as ‘ObjectVersion’ object has no attribute ‘storage’.</p> <p>Can someone please help?</p> </div> <div itemprop="interactionStatistic" itemscope itemtype="http://schema.org/InteractionCounter"> <meta itemprop="interactionType" content="http://schema.org/LikeAction"/> <meta itemprop="userInteractionCount" content="0" /> <span class='post-likes'></span> </div> </div> <div id='post_2' itemprop='comment' itemscope itemtype='http://schema.org/Comment' class='topic-body crawler-post'> <div class='crawler-post-meta'> <span class="creator" itemprop="author" itemscope itemtype="http://schema.org/Person"> <a itemprop="url" rel='nofollow' href='https://invenio-talk.web.cern.ch/u/nitarocc'><span itemprop='name'>nitarocc</span></a> </span> <span class="crawler-post-infos"> <time itemprop='datePublished' datetime='2024-02-06T10:50:10Z' class='post-time'> 6 February 2024 10:50 </time> <meta itemprop='dateModified' content='2024-02-06T10:50:10Z'> <span itemprop='position'>2</span> </span> </div> <div class='post' itemprop='text'> <p>Hello,<br> to access the file, you need to do something different: <code>record.files</code> gives you <a href="https://github.com/inveniosoftware/invenio-records-files/blob/master/invenio_records_files/api.py#L106">an iterator</a>, where each item is just <a href="https://github.com/inveniosoftware/invenio-records-files/blob/master/invenio_records_files/api.py#L24">a representation</a> of the real file.</p> <p>If you get an <a href="https://github.com/inveniosoftware/invenio-files-rest/blob/master/invenio_files_rest/models.py#L1004">ObjectVersion</a>, then you can try to do the following:</p> <pre data-code-wrap="python"><code class="lang-python">object_version = record.files[‘hello.txt’] fileobj = object_version.file with fileobj.storage.open(‘rb’) as file: file_content = file.read().decode(‘utf-8’) </code></pre> </div> <div itemprop="interactionStatistic" itemscope itemtype="http://schema.org/InteractionCounter"> <meta itemprop="interactionType" content="http://schema.org/LikeAction"/> <meta itemprop="userInteractionCount" content="0" /> <span class='post-likes'></span> </div> </div> </div> </div> <footer class="container wrap"> <nav class='crawler-nav'> <ul> <li itemscope itemtype='http://schema.org/SiteNavigationElement'> <span itemprop='name'> <a href='/' itemprop="url">Home </a> </span> </li> <li itemscope itemtype='http://schema.org/SiteNavigationElement'> <span itemprop='name'> <a href='/categories' itemprop="url">Categories </a> </span> </li> <li itemscope itemtype='http://schema.org/SiteNavigationElement'> <span itemprop='name'> <a href='/guidelines' itemprop="url">Guidelines </a> </span> </li> <li itemscope itemtype='http://schema.org/SiteNavigationElement'> <span itemprop='name'> <a href='/tos' itemprop="url">Terms of Service </a> </span> </li> <li itemscope itemtype='http://schema.org/SiteNavigationElement'> <span itemprop='name'> <a href='/privacy' itemprop="url">Privacy Policy </a> </span> </li> </ul> </nav> <p class='powered-by-link'>Powered by <a href="https://www.discourse.org">Discourse</a>, best viewed with JavaScript enabled</p> </footer> <div class="buorg"><div>Unfortunately, <a href="https://www.discourse.org/faq/#browser">your browser is unsupported</a>. Please <a href="https://browsehappy.com">switch to a supported browser</a> to view rich content, log in and reply.</div></div> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10