Some ad types, such as
[ImageAd](/google-ads/scripts/docs/reference/adsapp/adsapp_imagead) and
[ResponsiveDisplayAd](/google-ads/scripts/docs/reference/adsapp/adsapp_responsivedisplayad),
contain images and other media elements. This guide describes how to upload and
query media using Google Ads scripts.

Uploading images

Images can be uploaded using the
[`ImageBuilder`](/google-ads/scripts/docs/reference/adsapp/adsapp_imagebuilder)
class, which takes a name and image data. The data is provided as a
[`Blob`](/apps-script/reference/base/blob) data interchange object that can be
created by services such as [Drive](/apps-script/reference/drive) or
[URL fetch](/apps-script/reference/url-fetch).

The following snippet shows how to upload an image from an external URL:  

    let imageUrl = "http://www.example.com/example.png";
    let imageBlob = UrlFetchApp.fetch(imageUrl).getBlob();
    let mediaOperation = AdsApp.adMedia().newImageBuilder()
        .withName("IMAGE_NAME")
        .withData(imageBlob)
        .build();

Alternatively, the image blob may be loaded from Google Drive:  

    let imageFileId = "IMAGE_FILE_ID";
    let imageBlob = DriveApp.getFileById(imageFileId).getBlob();
    let mediaOperation = AdsApp.adMedia().newImageBuilder()
        .withName("IMAGE_NAME")
        .withData(imageBlob)
        .build();

Uploading media bundles

Media bundles are ZIP archives that contain HTML5 assets, which can be used to
create [HTML5 ads](//support.google.com/google-ads/answer/1722096#otherhtml5).
Upload media bundles with the
[`MediaBundleBuilder`](/google-ads/scripts/docs/reference/adsapp/adsapp_mediabundlebuilder)
class, which takes a name and file data. As with images, data is provided as
a [`Blob`](/apps-script/reference/base/blob) data interchange object.

The following snippet shows how to upload a media bundle from an external URL:  

    let mediaBundleUrl = "http://www.example.com/example.zip";
    let mediaBundleBlob = UrlFetchApp.fetch(mediaBundleUrl).getBlob();
    let mediaOperation = AdsApp.adMedia().newMediaBundleBuilder()
        .withName("bundle name")
        .withData(mediaBundleBlob)
        .build();

Querying media

Media of every type can be queried in Google Ads scripts using a
[`MediaSelector`](/google-ads/scripts/docs/reference/adsapp/adsapp_mediaselector).
Use the
[`withCondition()`](/google-ads/scripts/docs/reference/adsapp/adsapp_mediaselector#withCondition_1)
predicate to filter media by name, type, or other fields. For example, the
following snippet finds all images in an account:  

    let mediaIterator = AdsApp.adMedia().media()
        .withCondition("Type = IMAGE")
        .get();
    while (mediaIterator.hasNext()) {
      let image = mediaIterator.next();
    }

Creating ads with media

See our [ads article](/google-ads/scripts/docs/examples/ads) for some code
examples of creating supported ads with attached media.