Skip to content

Media Creation

Jean-Sébastien Sevestre edited this page Jul 8, 2019 · 2 revisions

Media creation

See Auth&Connect for how to build a service.

import httplib2
import requests
import json

CUSTOMER_ID = '5769928858664960'
INSTANCE_ID = '6325588397129728'

service = BuildLumAppService()


# Upload

uploadUrl = service.file().uploadUrl().execute()
uploadUrl = uploadUrl.get('uploadUrl')
uploadFile = {'file': ('My image.png', open('lumapps.png', 'rb'), 'image/png')}


# Get the access token to authenticate the post request

accessToken = service.credentials.access_token
headers = {'Authorization': 'Bearer %s' % accessToken}

print 'Headers ready, upload url ready, file ready.. let\'s start the upload!'
uploadedFile = requests.post(uploadUrl, data={}, files=uploadFile, headers=headers)
uploadedFile = uploadedFile.json()

print 'File is now uploaded!'

# We have now all the informations to save the media!
# Just have to respect the model and create the media as an object
mediaObject = {
    'instance': INSTANCE_ID,
    'customer': CUSTOMER_ID,
    'name': { 'en': 'My incredible image' },
    'content': [{
        'lang': 'en',
        'name': 'My Media Name',
        'ext': uploadedFile.get('ext'),
        'height': uploadedFile.get('height'),
        'width': uploadedFile.get('width'),
        'mimeType': uploadedFile.get('mimeType'),
        'size': uploadedFile.get('fileSize'),
        'type': uploadedFile.get('type'),
        'url': uploadedFile.get('url'),
        'value': uploadedFile.get('blobKey'),
    }]
}

# Create the Media using the API
response = service.media().save(body=mediaObject).execute()

print response
Clone this wiki locally