0

What I'm trying to accomplish is this.

I'm working on an integration web app that lets our techs pull data from our crm and sharepoint.

What I'd like to be able to do is display images held in sharepoint in this web app. I think I have 2 choices.

  1. I can create anonymous links to those images programmatically then call those inline.

  2. Pull them down locally and display them that way.

Since there are only a few images I'm leaning towards 2. So what I don't understand is how to pull them down since they're binary.

I've been using the shareplum library. I can pull down a text file easily like:

from shareplum import Site
from shareplum import Office365
from shareplum.site import Version

server_url = "https://xxx.sharepoint.com/"
site_url = "sites/Technicians/"

un = "un"
pw = "pw"

authcookie = Office365(server_url, username=un, password=pw).GetCookies()
site = Site(server_url + site_url, version=Version.v2016, authcookie=authcookie)

folder = site.Folder('Shared Documents/Technical client documentation' \
                     '/Clients/Company Name, Inc/System Information')
p = folder.get_image('test.jpg')

So from the library we have.

def get_file(self, file_name):
    response = self._session.get(self.site_url + f"/_api/web/GetFileByServerRelativeUrl('{self.info['d']['ServerRelativeUrl']}/{file_name}')/$value")
    return response.text

I added my own function:

def get_image(self, file_name):
    response = self._session.get(self.site_url + f"/_api/web/GetFileByServerRelativeUrl('{self.info['d']['ServerRelativeUrl']}/{file_name}')/$value")
    print(response)
    if response.status_code== 200:
        with open('/home/bruce/test_scripts/pictestNew.jpg', 'wb') as f:
            #response.raw.decode_content = True
            shutil.copyfileobj(response.raw, f)
    return "ok"

I'm stuck trying to save the image. The above code gets me empty files. I also tried returning response.text the doing :

p = folder.get_image('test.jpg')
p = bytearray(p)

P would be the returned response.text then:

with open('picOut1.jpg', 'wb') as f:
    f.write(p)

That gets me a image file of the right size but throws a:

Error interpreting JPEG image file (Not a JPEG file: starts with 0xef 0xbf

I did post this on Stackoverflow also.( StackOverflow question I know that's frowned upon but my reasoning is this. Here I'm hoping someone can tell me if I'm even able to do what I want. The other one is just to make sure I'm not screwing up the python. Honestly I don't know where the problem is right now.

This is my first run around with python and I'm guessing I'm doing something wrong here. Would it just be easier to create a public link to that image?

1 Answer 1

0

For downloading an image via python and the rest api this works. I dded this function to folder.py inside of shareplum

def get_image(self, file_name):
    response = self._session.get(self.site_url + f"/_api/web/GetFileByServerRelativeUrl('{self.info['d']['ServerRelativeUrl']}/{file_name}')/$value")
    #print(response)
    if response.status_code== 200:
        with open('/home/bruce/test_scripts/testFriday1.jpg', 'wb') as f:
            f.write(response.content)
    return "ok"

File is written and viewable with no errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.