上传结构化数据文件

您可以使用 Display & Video 360 API 上传结构化数据文件,以通过程序化方式批量创建和更新资源,包括 YouTube 资源。本页讨论了如何使用 API 准备和上传结构化数据文件。

每个 Google Cloud 项目每天只能上传 30 个结构化数据文件。

准备要上传的结构化数据文件

上传的结构化数据文件应是最近下载的结构化数据文件的修改版本,并且应具有相同的文件类型和来自同一广告客户。通过进行以下更新,准备下载的结构化数据文件以供上传:

  • 为要创建的任何新资源添加条目。
  • 更新要更新的资源的所有现有条目。
  • 移除所有不是新条目或更新条目的条目,以减小文件大小、缩短处理时间,并避免出现任何意外更新。

在上传文件之前,请验证以下内容:

  • 该文件是一个 CSV 文件。
  • 文件中的所有条目都适用于同一父广告客户下的资源。
  • 文件中的条目不超过 10,000 个。

上传文件

使用 advertisers.sdfuploadtasks.upload 方法上传结构化数据文件。此请求会创建 SDF 上传任务 Operation。此长时间运行的操作会处理上传的文件,根据文件内容创建和更新资源,并生成列出成功和失败更新的结果文件。

以下是如何上传结构化数据文件并检索生成的 Operation

# Import the object used as the media body for the upload request.
from apiclient.http import MediaFileUpload

# Provide the parent advertiser ID for the resources in the SDF.
# The ID value must be a str and not an int.
advertiser_id = advertiser-id

# Provide the filename and local path to the media file.
sdf_filename = sdf-filename
sdf_path = sdf-path

# Create the request body.
body = {
  'filename': sdf_filename,
  'advertiserId': advertiser_id
}

# Create the upload object and use a default MIME type if not identified.
media = MediaFileUpload(sdf_path)
if not media.mimetype():
  media = MediaFileUpload(sdf_filename,'application/octet-stream')

# Upload the structured data file.
upload_response = service.advertisers().sdfuploadtasks().upload(
  advertiserId=advertiser_id,
  body=body,
  media_body=media
).execute()

# Print resulting Operation name.
print(f'SDF was successfully uploaded. Operation {upload_response["name"]} was '
       'created to process the uploaded file.')