TechplexEngineer
8/5/2015 - 10:02 PM

_Readme.md

I'm retrieving a Base64 encoded image from a webservice and trying to upload it to Cloudinary. I've tried a number of avenues to attempt the upload and all have seemed to fail.

So here was my first attempt. The error is: "TypeError: file() argument 1 must be encoded string without NULL bytes, not str"

#!/usr/bin/env python

import urllib2
import xml.dom.minidom
import base64

GRIDS = {
    'kitely': 'http://grid.kitely.com:8002/assets/',
    'osgrid': 'http://assets.osgrid.org/assets/'
}

NULL_KEY = '00000000-0000-0000-0000-000000000000'

class Asset:
    def __init__(self):
        pass
    @staticmethod
    def get(assetID, grid):
        b64_texture = Asset.getBase64(assetID, grid)

        texture = base64.b64decode(b64_texture)

        return texture

    @staticmethod
    def getBase64(assetID, grid):
        if assetID == NULL_KEY:
            raise Exception('NULL_KEY');

        if grid in GRIDS:
            assetURL = GRIDS[grid]
        else:
            raise Exception("Unknown Grid")

        assetURL = GRIDS[grid]+assetID
        req = urllib2.Request(assetURL)
        response = urllib2.urlopen(req)
        the_page = response.read()

        dom = xml.dom.minidom.parseString(the_page)
        root = dom.documentElement

        b64_texture = root.getElementsByTagName("Data")[0].childNodes[0].nodeValue

        return b64_texture


# Simple test routine
if __name__ == "__main__":

    import cloudinary
    import cloudinary.uploader
    import cloudinary.api

    cloudinary.config(
        cloud_name = "<cloud>",
        api_key = "<key>",
        api_secret = "<secret>"
    )

    assetID = 'da16e105-5a65-4211-a351-35cb78c9e13d'
    grid = 'osgrid'

    texture = Asset.get(assetID, grid)
    cloudinary.uploader.upload(texture, public_id='fred')

So then I thought maybe if I don't decode the Base64 image and pass it to the uploader that might work. Alas Nope. Here is the error "cloudinary.api.Error: Server returned unexpected status code - 502 - "

Here is the code:

#!/usr/bin/env python

import urllib2
import xml.dom.minidom
import base64

GRIDS = {
    'kitely': 'http://grid.kitely.com:8002/assets/',
    'osgrid': 'http://assets.osgrid.org/assets/'
}

NULL_KEY = '00000000-0000-0000-0000-000000000000'

class Asset:
    def __init__(self):
        pass
    @staticmethod
    def get(assetID, grid):
        b64_texture = Asset.getBase64(assetID, grid)

        texture = base64.b64decode(b64_texture)

        return texture

    @staticmethod
    def getBase64(assetID, grid):
        if assetID == NULL_KEY:
            raise Exception('NULL_KEY');

        if grid in GRIDS:
            assetURL = GRIDS[grid]
        else:
            raise Exception("Unknown Grid")

        assetURL = GRIDS[grid]+assetID
        req = urllib2.Request(assetURL)
        response = urllib2.urlopen(req)
        the_page = response.read()

        dom = xml.dom.minidom.parseString(the_page)
        root = dom.documentElement

        b64_texture = root.getElementsByTagName("Data")[0].childNodes[0].nodeValue

        return b64_texture


# Simple test routine
if __name__ == "__main__":

    import cloudinary
    import cloudinary.uploader
    import cloudinary.api

    cloudinary.config(
        cloud_name = "<cloud>",
        api_key = "<key>",
        api_secret = "<secret>"
    )

    assetID = 'da16e105-5a65-4211-a351-35cb78c9e13d'
    grid = 'osgrid'

    # begin -- OR --
    # texture = Asset.get(assetID, grid)
    # cloudinary.uploader.upload(texture, public_id='fred')
    #  -- OR --
    texture = Asset.getBase64(assetID, grid)
    data_url = 'data:image/jp2;base64,'+texture
    cloudinary.uploader.upload(data_url, public_id='fred')
    # end -- OR --

So then I thought that I could just write the file to disk then tell cloudinary to upload it. Nope. Error: "cloudinary.api.Error: General Error"

#!/usr/bin/env python

import urllib2
import xml.dom.minidom
import base64

GRIDS = {
    'kitely': 'http://grid.kitely.com:8002/assets/',
    'osgrid': 'http://assets.osgrid.org/assets/'
}

NULL_KEY = '00000000-0000-0000-0000-000000000000'

class Asset:
    def __init__(self):
        pass
    @staticmethod
    def get(assetID, grid):
        b64_texture = Asset.getBase64(assetID, grid)

        texture = base64.b64decode(b64_texture)

        return texture

    @staticmethod
    def getBase64(assetID, grid):
        if assetID == NULL_KEY:
            raise Exception('NULL_KEY');

        if grid in GRIDS:
            assetURL = GRIDS[grid]
        else:
            raise Exception("Unknown Grid")

        assetURL = GRIDS[grid]+assetID
        req = urllib2.Request(assetURL)
        response = urllib2.urlopen(req)
        the_page = response.read()

        dom = xml.dom.minidom.parseString(the_page)
        root = dom.documentElement

        b64_texture = root.getElementsByTagName("Data")[0].childNodes[0].nodeValue

        return b64_texture


# Simple test routine
if __name__ == "__main__":

    import cloudinary
    import cloudinary.uploader
    import cloudinary.api

    cloudinary.config(
        cloud_name = "<cloud>",
        api_key = "<key>",
        api_secret = "<secret>"
    )

    assetID = 'da16e105-5a65-4211-a351-35cb78c9e13d'
    grid = 'osgrid'

    # begin -- OR --
    # texture = Asset.get(assetID, grid)
    # cloudinary.uploader.upload(texture, public_id='fred')
    #  -- OR --
    # texture = Asset.getBase64(assetID, grid)
    # data_url = 'data:image/jp2;base64,'+texture
    # cloudinary.uploader.upload(data_url, public_id='fred')
    # -- OR --
    filename = 'test.jp2'
    with open(filename, 'a') as the_file:
        texture = Asset.get(assetID, grid)
        the_file.write(texture)

    cloudinary.uploader.upload(filename, public_id='fred')
    # end -- OR --

So then I thought that maybe it didn't like the jp2. So I wrote a small program to read a jpeg base64 encode it and try to upload it with a data_url. Nope. Error: "cloudinary.api.Error: Server returned unexpected status code - 502 - "

#!/usr/bin/env python

# send to cloudinary
import cloudinary
import cloudinary.uploader
import cloudinary.api

cloudinary.config(
  cloud_name = "<name>",
  api_key = "<key>",
  api_secret = "<secret>"
)

# open file
with open('lake.jpg', 'r') as f:
    d = f.read()

    # base 64 encode
    import base64
    e = base64.b64encode(d)

    # build URL
    # print e
    data_url = 'data:image/jpeg;base64,'+e
    # print data_url
    cloudinary.uploader.upload(data_url)

So then I tried uploading the file(test.jp2) with the web interface. Nope. Error: "Upload failed: General Error" (See screenshot)

I'm out of ideas. Please advise of next steps. Am I doing something really wrong?

#!/usr/bin/env python

import urllib2
import xml.dom.minidom
import base64

GRIDS = {
	'kitely': 'http://grid.kitely.com:8002/assets/',
	'osgrid': 'http://assets.osgrid.org/assets/'
}

NULL_KEY = '00000000-0000-0000-0000-000000000000'

class Asset:
	def __init__(self):
		pass
	@staticmethod
	def get(assetID, grid):
		b64_texture = Asset.getBase64(assetID, grid)

		texture = base64.b64decode(b64_texture)

		return texture

	@staticmethod
	def getBase64(assetID, grid):
		if assetID == NULL_KEY:
			raise Exception('NULL_KEY');

		if grid in GRIDS:
			assetURL = GRIDS[grid]
		else:
			raise Exception("Unknown Grid")

		assetURL = GRIDS[grid]+assetID
		req = urllib2.Request(assetURL)
		response = urllib2.urlopen(req)
		the_page = response.read()

		dom = xml.dom.minidom.parseString(the_page)
		root = dom.documentElement

		b64_texture = root.getElementsByTagName("Data")[0].childNodes[0].nodeValue

		return b64_texture


# Simple test routine
if __name__ == "__main__":

	import cloudinary
	import cloudinary.uploader
	import cloudinary.api

	cloudinary.config(
		cloud_name = "<cloud>",
		api_key = "<key>",
		api_secret = "<secret>"
	)

	assetID = 'da16e105-5a65-4211-a351-35cb78c9e13d'
	grid = 'osgrid'

	texture = Asset.get(assetID, grid)
	cloudinary.uploader.upload(texture, public_id='fred')
#!/usr/bin/env python

import urllib2
import xml.dom.minidom
import base64

GRIDS = {
	'kitely': 'http://grid.kitely.com:8002/assets/',
	'osgrid': 'http://assets.osgrid.org/assets/'
}

NULL_KEY = '00000000-0000-0000-0000-000000000000'

class Asset:
	def __init__(self):
		pass
	@staticmethod
	def get(assetID, grid):
		b64_texture = Asset.getBase64(assetID, grid)

		texture = base64.b64decode(b64_texture)

		return texture

	@staticmethod
	def getBase64(assetID, grid):
		if assetID == NULL_KEY:
			raise Exception('NULL_KEY');

		if grid in GRIDS:
			assetURL = GRIDS[grid]
		else:
			raise Exception("Unknown Grid")

		assetURL = GRIDS[grid]+assetID
		req = urllib2.Request(assetURL)
		response = urllib2.urlopen(req)
		the_page = response.read()

		dom = xml.dom.minidom.parseString(the_page)
		root = dom.documentElement

		b64_texture = root.getElementsByTagName("Data")[0].childNodes[0].nodeValue

		return b64_texture


# Simple test routine
if __name__ == "__main__":

	import cloudinary
	import cloudinary.uploader
	import cloudinary.api

	cloudinary.config(
		cloud_name = "<cloud>",
		api_key = "<key>",
		api_secret = "<secret>"
	)

	assetID = 'da16e105-5a65-4211-a351-35cb78c9e13d'
	grid = 'osgrid'

	# begin -- OR --
	# texture = Asset.get(assetID, grid)
	# cloudinary.uploader.upload(texture, public_id='fred')
	#  -- OR --
	texture = Asset.getBase64(assetID, grid)
	data_url = 'data:image/jp2;base64,'+texture
	cloudinary.uploader.upload(data_url, public_id='fred')
	# end -- OR --
#!/usr/bin/env python

import urllib2
import xml.dom.minidom
import base64

GRIDS = {
	'kitely': 'http://grid.kitely.com:8002/assets/',
	'osgrid': 'http://assets.osgrid.org/assets/'
}

NULL_KEY = '00000000-0000-0000-0000-000000000000'

class Asset:
	def __init__(self):
		pass
	@staticmethod
	def get(assetID, grid):
		b64_texture = Asset.getBase64(assetID, grid)

		texture = base64.b64decode(b64_texture)

		return texture

	@staticmethod
	def getBase64(assetID, grid):
		if assetID == NULL_KEY:
			raise Exception('NULL_KEY');

		if grid in GRIDS:
			assetURL = GRIDS[grid]
		else:
			raise Exception("Unknown Grid")

		assetURL = GRIDS[grid]+assetID
		req = urllib2.Request(assetURL)
		response = urllib2.urlopen(req)
		the_page = response.read()

		dom = xml.dom.minidom.parseString(the_page)
		root = dom.documentElement

		b64_texture = root.getElementsByTagName("Data")[0].childNodes[0].nodeValue

		return b64_texture


# Simple test routine
if __name__ == "__main__":

	import cloudinary
	import cloudinary.uploader
	import cloudinary.api

	cloudinary.config(
		cloud_name = "<cloud>",
		api_key = "<key>",
		api_secret = "<secret>"
	)

	assetID = 'da16e105-5a65-4211-a351-35cb78c9e13d'
	grid = 'osgrid'

	# begin -- OR --
	# texture = Asset.get(assetID, grid)
	# cloudinary.uploader.upload(texture, public_id='fred')
	#  -- OR --
	# texture = Asset.getBase64(assetID, grid)
	# data_url = 'data:image/jp2;base64,'+texture
	# cloudinary.uploader.upload(data_url, public_id='fred')
	# -- OR --
	filename = 'test.jp2'
	with open(filename, 'a') as the_file:
		texture = Asset.get(assetID, grid)
		the_file.write(texture)

	cloudinary.uploader.upload(filename, public_id='fred')
	# end -- OR --
#!/usr/bin/env python

# send to cloudinary
import cloudinary
import cloudinary.uploader
import cloudinary.api

cloudinary.config(
  cloud_name = "<name>",
  api_key = "<key>",
  api_secret = "<secret>"
)

# open file
with open('lake.jpg', 'r') as f:
	d = f.read()

	# base 64 encode
	import base64
	e = base64.b64encode(d)

	# build URL
	# print e
	data_url = 'data:image/jpeg;base64,'+e
	# print data_url
	cloudinary.uploader.upload(data_url)