Page 1 of 1

idea about attaching/retrieving a same-name file on servers

PostPosted: Tue Jul 12, 2011 2:12 am
by bhcho
Hi All,

what we want to do is trying to access to the same-named file from multiple different servers.
Eventually, we want to download all the files from those servers into local server.


So,
1.
do you have an idea how to attach(upload) a local file (txt file or binary file) to a local OMERO server (from python API)?
all I know is
Code: Select all
uploadAndAttachFile(queryService, updateService, rawFileStore, parent, localName, mimetype, description=None, namespace=None, origFilePathName=None) in omero.util.script_utils

but this function needs a parent (a project, dataset or image) to attach to, and I wonder if there is a way to attach(upload) a local file to server without parent.

2.
how can I download that file if I do not attach it to anything?

3.
how can I delete the attached file completely. (I'm asking this because I need to update the attached file frequently. I think there is no way to update the attached file in the server using API, thus I want to delete the existing file first and re-attach the updated file)

4.
Most importantly, I want the file to be accessed by any account. Is it possible to attach the file in this way?

Best,
BK

Re: idea about attaching/retrieving a same-name file on serv

PostPosted: Mon Jul 18, 2011 1:20 pm
by jmoore
bhcho wrote:1.
do you have an idea how to attach(upload) a local file (txt file or binary file) to a local OMERO server (from python API)?
all I know is
Code: Select all
uploadAndAttachFile(queryService, updateService, rawFileStore, parent, localName, mimetype, description=None, namespace=None, origFilePathName=None) in omero.util.script_utils

but this function needs a parent (a project, dataset or image) to attach to, and I wonder if there is a way to attach(upload) a local file to server without parent.


omero.client.upload() will save a local file to OMERO without attaching it to anything. omero.gateway.BlitzGateway.createFileAnnfromLocalFile will do the same but also create a FileAnnotation.

bhcho wrote:2.
how can I download that file if I do not attach it to anything?


omero.client.download will do the reverse.

bhcho wrote:3.
how can I delete the attached file completely. (I'm asking this because I need to update the attached file frequently. I think there is no way to update the attached file in the server using API, thus I want to delete the existing file first and re-attach the updated file)


Use the delete command: omero.api.delete.DeleteCommand("/OriginalFile", fileID, None)

bhcho wrote:4.
Most importantly, I want the file to be accessed by any account. Is it possible to attach the file in this way?


That is based on the permissions of the File itself, which in turns is based on the permissions of the group. You may want to make use of IAdmin.moveToCommonSpace(IObjectList objects) throws ServerError; which will use the "user" group making it visible for everyone.

Cheers,
~Josh

Re: idea about attaching/retrieving a same-name file on serv

PostPosted: Mon Jul 18, 2011 1:33 pm
by wmoore
For deleting the file annotation, see viewtopic.php?f=6&t=659

Re: idea about attaching/retrieving a same-name file on serv

PostPosted: Mon Jul 18, 2011 1:49 pm
by jmoore
Except if you don't have a FileAnnotation, then don't delete with /Annotation but with /OriginalFile. ~Josh.

Re: idea about attaching/retrieving a same-name file on serv

PostPosted: Mon Jul 18, 2011 3:58 pm
by bhcho
thanks

omero.client.upload() will save a local file to OMERO without attaching it to anything. omero.gateway.BlitzGateway.createFileAnnfromLocalFile will do the same but also create a FileAnnotation.


I'm still confused with omero.client and omero.gateway.BlitzGateWay.

1.
If I created a connection by
Code: Select all
client = omero.client( server, port )
session = client.createSession( username, password )

then I need to use client.upload().

But If I use the conn object in views.py of OMERO.web, such as
Code: Select all
@isUserConnected   
def myFunction( request, argA = None, argB = None, **kwargs):
      conn = kwargs["conn"]


then should I use like below?
Code: Select all
conn.createFileAnnfromLocalFile()


2.
Could you tell me the API link to those two methods? I don't know how to use them.
it seems client.upload method takes two args
Code: Select all
file_name = './test.vec'
file = client.upload(file_name, type="text/xxx")


but I'm not sure about createFileAnnfromLocalFile.

3.
what is the equivalent function of client.download() for blitzgateway?

4.
for IAdmin.moveToCommonSpace(IObjectList objects),
1)
Can I get the IAdmin service by
Code: Select all
admin = session.getAdminService()

2) what is the IObjectList? Assuming I have uploaded a file to DB, what will be the input argument IObjectList?
Code: Select all
admin.moveToCommonSpace(IObjectList objects)



Thanks again,
BK

Re: idea about attaching/retrieving a same-name file on serv

PostPosted: Mon Jul 18, 2011 10:41 pm
by wmoore
Hi BK,

The "conn" connection is the Blitz Gateway as described http://trac.openmicroscopy.org.uk/ome/w ... Py/Gateway. Under "The Gateway Methods" you'll find links to the "conn" API and the method you're looking for: http://hudson.openmicroscopy.org.uk/job ... mLocalFile

However, I believe that you're using 4.2.1 and that method is only in the 4.3 release. 4.2 "conn" API is http://hudson.openmicroscopy.org.uk/vie ... class.html.

You have a couple of choices: You can look at the code for that method in the 4.3 Blitz gateway and port it to your 4.2.1 code. The best place to put it would be in you own Blitz Gateway subclass, in the same way that the web client has extend Blitz Gateway with omeroweb/webclient/webclient_gateway.py. If this seems a bit too tricky then put it somewhere else that makes sense to you.

This code is based on the method you mentioned in script_utils.py. You could look there at the other available methods and you should see that there are ways to create a file attachment and upload file without adding the annotation to any parent.

The code for uploadAndAttachFile looks like this:

Code: Select all
    filename = localName
    if origFilePathName == None:
        origFilePathName = localName
    originalFile = createFile(updateService, filename, mimetype, origFilePathName)
    uploadFile(rawFileStore, originalFile, localName)
    fileLink = attachFileToParent(updateService, parent, originalFile, description, namespace)
    return fileLink.getChild()


So you could simply do
Code: Select all
originalFile = script_utils.createFile(updateService, filename, mimetype, origFilePathName)
script.utils.uploadFile(rawFileStore, originalFile, localName)


If you wanted this to be a FileAnnotation instead of a regular OriginalFile, you could do
Code: Select all
fa = omero.model.FileAnnotationI()
fa.setFile(originalFile)
fa.setDescription(rstring(desc))
fa.setNs(rstring(ns))
fa = updateService.saveAndReturnObject(fa)



To download that file, you can use
Code: Select all
fa = conn.getFileAnnotation(annId)


To provide the file as a download from the web, this is how the webclient does it:

Code: Select all
@isUserConnected
def download_annotation(request, action, iid, **kwargs):
    conn = None
    try:
        conn = kwargs["conn"]
    except:
        logger.error(traceback.format_exc())
        return handlerInternalError("Connection is not available. Please contact your administrator.")
   
    try:
        # ann = conn.getObject("Annotation", long(iid))  # 4.3 API
        ann = conn.getFileAnnotation(annId)    # This is with 4.2 API
       
        from django.conf import settings
        tempdir = settings.FILE_UPLOAD_TEMP_DIR
        temp = os.path.join(tempdir, ('%i-%s.download' % (ann.file.id.val, conn._sessionUuid))).replace('\\','/')
        logger.info("temp path: %s" % str(temp))
        f = open(str(temp),"wb")
        for piece in ann.getFileInChunks():
            f.write(piece)
        f.seek(0)
               
        from django.core.servers.basehttp import FileWrapper
        originalFile_data = FileWrapper(file(temp))
    except Exception, x:
        logger.error(traceback.format_exc())
        return handlerInternalError("Cannot download annotation (id:%s)." % (iid))
    rsp = HttpResponse(originalFile_data)
    if originalFile_data is None:
        return handlerInternalError("Cannot download annotation (id:%s)." % (iid))
    if action == 'download':
        rsp['Content-Type'] = 'application/force-download'
        rsp['Content-Length'] = ann.getFileSize()
        rsp['Content-Disposition'] = 'attachment; filename=%s' % (ann.getFileName().replace(" ","_"))
    return rsp


I'm not sure what this code looked like in 4.2.1, but if it's similar, you could provide a "download" link on your pages with

Code: Select all
<a href="{% url download_annotation 'download' annId %}" > Download </a>



I assume IObjectList in Python is simply a list of IObjects, E.g.

Code: Select all
a = omero.model.DatasetI(dsId)
b = omero.model.ImageI(iId)

list = [a, b]



Will.