File uploaded looks like junk when opened from Storage bucket

I gave myself a good chuckle as I intended to reply back a few days ago but kept getting busy and then I got hung up the PUT API call as I decided to try External Applications for the first time and wasn’t paying attention when I kept passing the authentication in both my Python and Postman PUT request when I shouldn’t have been … something silly for far too long… I blame it on me not having written Python…

ANYWAYS
Don’t mind the spaghetti code and lack of parametrization and formatting.

The following Python code works for me, tested on a small image and downloading that image returns expected output.

import requests
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session

token_url = 'https://cloud.uipath.com/identity_/connect/token'
clientId = 'MAGIC_CLIENT_ID'
clientSecret = 'SUPERDUPERSECRET'
clientScope = 'SOMEOVERTHETOPAPPLICATIONSCOPE'

client = BackendApplicationClient(client_id=clientId)
uipath = OAuth2Session(client=client)

uipath.fetch_token(
    token_url=token_url,
    client_id=clientId,
    client_secret=clientSecret,
    response_type='client_credentials',
    scope=clientScope
)
bucketUrl = 'https://cloud.uipath.com/org/tenant/odata/Buckets(132456)/UiPath.Server.Configuration.OData.GetWriteUri'
headers = {
    'X-UIPATH-OrganizationUnitId': '5454645654645'
}
params = {
    'path': '\\path\\tim-tuque.jpg',
    'contentType': 'image/jpeg'
}
r = uipath.request('GET', bucketUrl, headers=headers, params=params)
jsonResponse = r.json()

data = open('.\\tim-tuque.jpg', 'rb').read()
r = requests.put(jsonResponse['Uri'], data=data, headers={'x-ms-blob-type': 'BlockBlob'})
if r.status_code == 201:
    print('SUCCESS')

Possible the difference with how files and data is treated and looking at the documentation of Requests.Put it doesn’t appear that it takes files as an argument [But I find the formatting of those docs to be awkward and not sure what the kwargs / Key Word Arguments intails].

r = requests.put(BlobUploadUri, files={"src": file_data},headers=headers)

vs

r = requests.put(BlobUploadUri, data=data, headers=headers)

  • files – (optional) Dictionary of 'name': file-like-objects (or {'name': file-tuple} ) for multipart encoding upload. file-tuple can be a 2-tuple ('filename', fileobj) , 3-tuple ('filename', fileobj, 'content_type') or a 4-tuple ('filename', fileobj, 'content_type', custom_headers) , where 'content-type' is a string defining the content type of the given file and custom_headers a dict-like object containing additional headers to add for the file.
  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request .

requests. put ( url , data=None , **kwargs )[source]

Sends a PUT request.

Parameters: * url – URL for the new Request object.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request .
  • json – (optional) json data to send in the body of the Request .
  • **kwargs – Optional arguments that request takes.
    Returns: Response object
    Return type:
1 Like