# Upload Structured Data File

You can create and update resources, including YouTube resources,
programmatically and in bulk by uploading [Structured Data Files](https://developers.google.com/display-video/api/structured-data-file/format) using
the Display \& Video 360 API. This page discusses how to prepare and upload a Structured
Data File using the API.

Only thirty Structured Data Files per advertiser ID can be uploaded by a Google
Cloud project in a day.

## Prepare a Structured Data File for upload

Uploaded Structured Data Files should be modified versions of recently
[downloaded Structured Data Files](https://developers.google.com/display-video/api/sdf-upload/download-guide/overview) of the same file type and
from the same advertiser. Prepare the downloaded Structured Data File for upload
by making the following updates:

- Add entries for any new resources you want to create.
- Update any existing entries for resources you want to update.
- Remove any entries that are not new or updated in order to reduce file size, speed up processing time, and remove the likelihood for any unintentional updates.

> [!TIP]
> **Tip:** Use a recently-downloaded SDF when preparing an SDF for upload. Resources that have been modified since the SDF was downloaded may not update successfully on file upload.

Verify the following before uploading the file:

- The file is a `CSV` file.
- All entries in the file are for resources under the same parent advertiser.
- The file doesn't have more than 10,000 entries.

## Upload a file

Upload a Structured Data File using the
[`advertisers.sdfuploadtasks.upload`](https://developers.google.com/display-video/api/sdf-upload/rest/v4/advertisers.sdfuploadtasks/upload) method. This request
creates an SDF upload task [`Operation`](https://developers.google.com/display-video/api/sdf-upload/rest/v4/sdfuploadtasks.operations#Operation). This long-running
operation processes the uploaded file, creates and updates resources based on
the file contents, and generates result files that list successful and failed
updates.

Here's how to upload a Structured Data File and retrieve the resulting
[`Operation`](https://developers.google.com/display-video/api/sdf-upload/rest/v4/sdfuploadtasks.operations#Operation):

> [!WARNING]
> **Warning:** The [`advertisers.sdfuploadtasks.upload`](https://developers.google.com/display-video/api/sdf-upload/rest/v4/advertisers.sdfuploadtasks/upload) method can only be successfully called thirty times per day per advertiser by a Google Cloud project in a day. A day starts at midnight in the US Pacific timezone (PST). If a request to the method fails, it does not count against your quota.

```
# 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.')
```