I'm trying to use the following python code to upload the Image Asset to a given ad group/campaign, but I found I can't set the specific ad group ID or campaign ID with AssetService, I was wondering how can I upload the assets to a specific ad group with given ad group ID? Besides, I found for AssetService with AssetOperation, you can only use 'create' to upload a new asset. But what if I'm trying to delete an existing asset, how can I do so?
def main(client, customer_id):
"""Main method, to run this code example as a standalone application."""
# Download image from URL
image_content = requests.get(URL).content
asset_operation = client.get_type('AssetOperation', version='v2')
asset = asset_operation.create
#Define the asset type
asset.type = client.get_type('AssetTypeEnum', version='v2').IMAGE
asset.image_asset.data.value = image_content
asset.image_asset.file_size.value = len(image_content)
asset.image_asset.mime_type = client.get_type('MimeTypeEnum').IMAGE_JPEG
# Use your favorite image library to determine dimensions
asset.image_asset.full_size.height_pixels.value = 315
asset.image_asset.full_size.width_pixels.value = 600
asset.image_asset.full_size.url.value = URL
# If you specify the name field, then both the asset name and the image
# being uploaded should be unique, and should not match another ACTIVE
# asset in this customer account.
asset_service = client.get_service('AssetService', version='v2')
try:
mutate_asset_response = (
asset_service.mutate_assets(customer_id,
[asset_operation])
)
print('Uploaded file(s):')
for row in mutate_asset_response.results:
print(f'\tResource name: {row.resource_name}')
except GoogleAdsException as ex:
print(f'Request with ID "{ex.request_id}" failed with status '
f'"{ex.error.code().name}" and includes the following errors:')
for error in ex.failure.errors:
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print(f'\t\tOn field: {field_path_element.field_name}')
sys.exit(1)
if __name__ == '__main__':
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
google_ads_client = GoogleAdsClient.load_from_storage()
parser = argparse.ArgumentParser(description='Upload an image asset from a URL.')
# The following argument(s) should be provided to run the example.
parser.add_argument('-c', '--customer_id', type=str, required=True, help='The Google Ads customer ID.')
args = parser.parse_args()
main(google_ads_client, args.customer_id)