Comments (5)
@bcail no, you should be able to assign a file-like object with a read method. (Possibly only relevant for for FileDatastream
and not the base datastream class, but that's mentioned in the documentation you linked to). The save and ingest methods check for and handle reading from the file and posting it. This allows eulfedora to handle large files, so the contents don't get read into memory all at once.
That example may not be great for another reason, though, which is that if you use the with block then the file will get closed before you actually use it to send content to fedora.
If you see a way to make this clearer in the documentation, please make a suggestion or pull request.
from eulfedora.
@rlskoeser thanks for replying.
What I'm trying to do is create a fedora object and add a tiff datastream. I have some code working, and this is basically what it looks like:
obj = models.DigitalObject(md.REPO.api, create=True, default_pidspace='testsuite')
obj.save()
ds_obj = models.DatastreamObject(obj, ds_name, ds_label, mimetype='image/tiff', versionable=True)
with open(file_name) as f:
ds_obj.content = f.read()
ds_obj.save()
If I change f.read()
to just f
in the above code, it doesn't save the tiff datastream properly.
Is there a different/better way to structure this code, to fit more with how eulfedora is designed?
I see in the code comments the pattern of defining my own ImageObject class with an "image" FileDatastream, and then instantiating the class and setting obj.image.content = f
, but I guess I'm looking to be able to create objects and datastreams without having to create a separate class to define the datastream structure first.
from eulfedora.
Wow, would not have occurred to me to try that. I'm almost surprised it works at all (although it's always nice when things work in other contexts than you anticipate). I think in this case the problem is that you're using the generic DatastreamObject
rather than the FileDatastreamObject
, which has the file object support - I suspect if you switch it you'll be able to pass the file object in directly and have it work.
I do think it might be better to use the DigitalObject
getDatastreamObject method to initialize your datastream object (and specify a dsobj_type
of FileDatastreamObject
in this case) - although I guess I don't have a very strong argument for why you should use that instead of your own approach. The documentation notes that when you add new datastreams via getDatastreamObject
you have to set all the datastream metadata, so perhaps your approach is more efficient for that.
FWIW, it might be useful to add some more examples along these lines, because sometimes you do need to work with objects in a more ad-hoc manner rather than with the pre-defined content models that are described in the documentation.
(I modified your post for formatting to make the code easier to read - indentation in particular; hope you don't mind.)
from eulfedora.
You're right - I switched it to FileDatastreamObject, and then just passing in the file object worked. Thanks!
I think the documentation might be a bit clearer if the example in DatastreamObject didn't reference the FileDatastream class. The FileDatastreamObject already has the example of reading from a file object, so I think in the DatastreamObject class I would remove that FileDatastream example (or just make it a new example that passes in content directly in a way that works in DatastreamObject itself).
from eulfedora.
Glad it worked. Your suggestion about the documentation sounds sensible - I'll leave this issue open as a reminder to revise that (but feel free to submit a pull request!).
from eulfedora.
Related Issues (18)
- Retrieving previous versions of a datastream's content HOT 2
- Missing Content-Disposition in raw_datastream view HOT 5
- progressbar versions HOT 6
- syncutil - window size for reading datastream information can be too small HOT 3
- custom panel for django-debug-toolbar
- raw_datastream view documentation not included on readthedocs.org
- fedora-checksums not repairing HOT 5
- eulfedora not compatible with requests 2.11.0 HOT 3
- Checksum Mismatch
- Eulfedora and Fedora 4.x
- Use pycryptodome instead of pycrypto
- Status of the project
- documentation (installation, tutorial) is out of date for current versions of Django HOT 2
- TypeError at /objects/simplerepo:1/ HOT 1
- syncrepo command not found HOT 4
- References to XMLDatastreamObjects being overwritten HOT 3
- add_relationship() does not recognize object PIDs when provided as unicode strings HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eulfedora.