Upload creative assets

Creatives hosted by Display & Video 360 are built from Asset objects. An asset is defined by a mediaId or some text content . Use the upload method to upload media for use in assets before creating the creative object.

An asset must have the same parent advertiser as the creative that uses it. To use an asset across advertisers, upload the media file under each advertiser.

Here's how to upload a media asset:

Java

 // Provide the parent advertiser ID to upload the media file under. 
 long 
  
 advertiserId 
  
 = 
  
  advertiser 
 - 
 id 
 
 ; 
 // Provide the local path to the media file. 
 String 
  
 assetPath 
  
 = 
  
  asset 
 - 
 path 
 
 // Get filename from path 
 String 
  
 assetFilename 
  
 = 
  
 new 
  
 File 
 ( 
 assetPath 
 ). 
 getName 
 (); 
 // Create the asset upload request content 
 CreateAssetRequest 
  
 content 
  
 = 
  
 new 
  
 CreateAssetRequest 
 (). 
 setFilename 
 ( 
 assetFilename 
 ); 
 // Parse filename for appropriate MIME type 
 FileNameMap 
  
 filenameMap 
  
 = 
  
 URLConnection 
 . 
 getFileNameMap 
 (); 
 String 
  
 mimeType 
  
 = 
  
 filenameMap 
 . 
 getContentTypeFor 
 ( 
 assetFilename 
 ); 
 if 
  
 ( 
 mimeType 
  
 == 
  
 null 
 ) 
  
 { 
  
 mimeType 
  
 = 
  
 "application/octet-stream" 
 ; 
 } 
 // Create input stream for the creative asset. 
 InputStreamContent 
  
 assetStream 
  
 = 
  
 new 
  
 InputStreamContent 
 ( 
 mimeType 
 , 
  
 new 
  
 FileInputStream 
 ( 
 assetPath 
 )); 
 // Configure the asset upload request. 
 Assets 
 . 
 Upload 
  
 assetRequest 
  
 = 
  
 service 
 . 
 advertisers 
 (). 
 assets 
 (). 
 upload 
 ( 
 advertiserId 
 , 
  
 content 
 , 
  
 assetStream 
 ); 
 // Upload the asset. 
 CreateAssetResponse 
  
 assetResponse 
  
 = 
  
 assetRequest 
 . 
 execute 
 (); 
 // Display the new asset media ID. 
 System 
 . 
 out 
 . 
 printf 
 ( 
 "Asset was created with media ID %s." 
 , 
  
 assetResponse 
 . 
 getAsset 
 (). 
 getMediaId 
 ()); 

Python

 # Import the object used as the media body for the upload request. 
 from 
  
 apiclient.http 
  
 import 
 MediaFileUpload 
 # Provide the parent advertiser ID to upload the media file under. 
 advertiser_id 
 = 
  advertiser 
 - 
 id 
 
 # Provide the filename and local path to the media file. 
 asset_filename 
 = 
  asset 
 - 
 filename 
 
 asset_path 
 = 
  asset 
 - 
 path 
 
 # Create the request body. 
 body 
 = 
 { 
 "filename" 
 : 
 asset_filename 
 } 
 # Create the upload object and use a default MIME type if not identified. 
 media 
 = 
 MediaFileUpload 
 ( 
 asset_path 
 ) 
 if 
 not 
 media 
 . 
 mimetype 
 (): 
 media 
 = 
 MediaFileUpload 
 ( 
 asset_path 
 , 
 "application/octet-stream" 
 ) 
 # Upload the asset. 
 upload_response 
 = 
 ( 
 service 
 . 
 advertisers 
 () 
 . 
 assets 
 () 
 . 
 upload 
 ( 
 advertiserId 
 = 
 advertiser_id 
 , 
 body 
 = 
 body 
 , 
 media_body 
 = 
 media 
 ) 
 . 
 execute 
 () 
 ) 
 # Print resulting media ID. 
 print 
 ( 
 f 
 'Asset was created with media ID 
 { 
 upload_response 
 [ 
 "asset" 
 ][ 
 "mediaId" 
 ] 
 } 
 .' 
 ) 

PHP

 // Configure the asset upload request. 
 $body = new Google_Service_DisplayVideo_CreateAssetRequest(); 
 $body->setFilename( asset-filename 
); 
 $optParams = array( 
 'data' => file_get_contents( asset-path 
), 
 'mimeType' => mime_content_type( asset-filename 
), 
 'uploadType' => 'media' 
 ); 
 // Call the API to upload the creative asset. 
 try { 
 $result = $service->advertisers_assets->upload( 
  advertiser-id 
 
 $body, 
 $optParams 
 ); 
 } catch (\Exception $e) { 
 $this->renderError($e); 
 return; 
 } 
 // Display the new asset media ID. 
 printf( 
 '<p>Asset was created with media ID %s.</p>', 
 $result->getAsset()->getMediaId() 
 ); 

cURL

 curl --request POST \ 
 'https://displayvideo.googleapis.com/upload/v4/advertisers/ advertiser-id 
/assets?uploadType=multipart' 
 --header 'Authorization: Bearer access-token 
' \ 
 -F "data={\"filename\": \" asset-filename 
\"};type=application/json;charset=UTF-8" \ 
 -F "file=@ asset-path 
;type= asset-mime-type 
" 
Create a Mobile Website
View Site in Mobile | Classic
Share by: