Campaign Manager 360 API ช่วยให้คุณดาวน์โหลดไฟล์รายงานซึ่งเกิดจากการเรียกใช้รายงานเครื่องมือสร้างรายงานได้ นอกจากนี้ ยังรองรับให้ผู้ใช้เข้าถึงไฟล์โดยตรงผ่านลิงก์ได้ด้วย
คุณจะใช้วิธีใดวิธีหนึ่งต่อไปนี้ ทั้งนี้ขึ้นอยู่กับประเภทการดาวน์โหลดที่คุณต้องการดำเนินการ
- ดาวน์โหลดโดยตรง — Files.get โดยใช้พารามิเตอร์
alt=media
- ดาวน์โหลดไฟล์ในเบราว์เซอร์ —
browserUrl
จากทรัพยากรของ Files
ส่วนที่เหลือของคู่มือนี้มีวิธีการโดยละเอียดสำหรับการดำเนินการดาวน์โหลดประเภทนี้ผ่านทางแหล่งข้อมูลของ Files
ข้อกำหนดเบื้องต้น
ในการดาวน์โหลดไฟล์ คุณจะต้องมีข้อมูลดังต่อไปนี้
- รหัสของรายงานที่มีไฟล์อยู่ โดยสร้างรายงานใหม่หรือหารายงานที่มีอยู่
- รหัสของไฟล์ที่จะดาวน์โหลด คุณสามารถค้นหาได้โดยการเรียกใช้รายงานจากขั้นตอนก่อนหน้า หรือค้นหารายการไฟล์ที่มีอยู่ตามตัวอย่างด้านล่าง
C#
File target = null;
FileList files;
String nextPageToken = null;
do {
// Create and execute the files list request.
ReportsResource.FilesResource.ListRequest request =
service.Reports.Files.List(profileId, reportId);
request.PageToken = nextPageToken;
files = request.Execute();
foreach (File file in files.Items) {
if (IsTargetFile(file)) {
target = file;
break;
}
}
// Update the next page token.
nextPageToken = files.NextPageToken;
} while (target == null
&& files.Items.Any()
&& !String.IsNullOrEmpty(nextPageToken));
Java
File target = null;
FileList files;
String nextPageToken = null;
do {
// Create and execute the files list request.
files = reporting.reports().files().list(profileId, reportId).setPageToken(nextPageToken)
.execute();
for (File file : files.getItems()) {
if (isTargetFile(file)) {
target = file;
break;
}
}
// Update the next page token.
nextPageToken = files.getNextPageToken();
} while (target == null
&& !files.getItems().isEmpty()
&& !Strings.isNullOrEmpty(nextPageToken));
PHP
$target = null;
$response = null;
$pageToken = null;
do {
// Create and execute the file list request.
$response = $this->service->reports_files->listReportsFiles(
$userProfileId,
$reportId,
['pageToken' => $pageToken]
);
foreach ($response->getItems() as $file) {
if ($this->isTargetFile($file)) {
$target = $file;
break;
}
}
$pageToken = $response->getNextPageToken();
} while (empty($target) && !empty($response->getItems()) && !empty($pageToken));
Python
target = None
request = service.reports().files().list(
profileId=profile_id, reportId=report_id)
while True:
response = request.execute()
for report_file in response['items']:
if is_target_file(report_file):
target = report_file
break
if not target and response['items'] and response['nextPageToken']:
request = service.reports().files().list_next(request, response)
else:
break
Ruby
page_token = nil
target = nil
loop do
result = service.list_report_files(profile_id, report_id,
page_token: page_token)
result.items.each do |file|
if target_file?(file)
target = file
break
end
end
page_token = (result.next_page_token if target.nil? && result.items.any?)
break if page_token.to_s.empty?
end
โปรดทราบว่าช่อง status
ของแหล่งข้อมูลไฟล์ต้องตั้งค่าเป็น REPORT_AVAILABLE
จึงจะมีสิทธิ์ดาวน์โหลด
การดาวน์โหลดโดยตรง
หากต้องการดาวน์โหลดโดยตรง คุณจะต้องส่งคำขอ HTTP GET
ที่ได้รับอนุญาตไปยังบริการ Files และรวมพารามิเตอร์การค้นหา alt=media
ไว้ด้วย ตัวอย่างคำขออาจมีลักษณะดังนี้
GET https://www.googleapis.com/dfareporting/v3.4/reports/12345/files/12345?alt=media HTTP/1.1 Authorization: Bearer your_auth_token
โปรดทราบว่าการตอบกลับที่คุณได้รับจะมีการเปลี่ยนเส้นทาง ดังนั้นคุณควรกำหนดค่าแอปพลิเคชันให้จัดการเรื่องนี้ได้โดยอัตโนมัติ หากต้องการจัดการเรื่องนี้ด้วยตนเอง คุณก็หา URL เปลี่ยนเส้นทางได้ในส่วนหัวของ Location
ในการตอบกลับ
ไลบรารีของไคลเอ็นต์อย่างเป็นทางการของ Google ส่วนใหญ่มอบวิธีที่สะดวกในการเริ่มต้นการดาวน์โหลดโดยตรง ดังที่แสดงในตัวอย่างด้านล่าง หากต้องการเริ่มการดาวน์โหลดด้วยตนเอง ระบบจะระบุ URL ที่สร้างไว้ล่วงหน้าในช่อง apiUrl
ของแหล่งข้อมูลไฟล์
C#
// Retrieve the file metadata.
File file = service.Files.Get(reportId, fileId).Execute();
if ("REPORT_AVAILABLE".Equals(file.Status)) {
// Create a get request.
FilesResource.GetRequest getRequest = service.Files.Get(reportId, fileId);
// Optional: adjust the chunk size used when downloading the file.
// getRequest.MediaDownloader.ChunkSize = MediaDownloader.MaximumChunkSize;
// Execute the get request and download the file.
using (System.IO.FileStream outFile = new System.IO.FileStream(GenerateFileName(file),
System.IO.FileMode.Create, System.IO.FileAccess.Write)) {
getRequest.Download(outFile);
Console.WriteLine("File {0} downloaded to {1}", file.Id, outFile.Name);
}
}
Java
// Retrieve the file metadata.
File file = reporting.files().get(reportId, fileId).execute();
if ("REPORT_AVAILABLE".equals(file.getStatus())) {
// Prepare a local file to download the report contents to.
java.io.File outFile = new java.io.File(Files.createTempDir(), generateFileName(file));
// Create a get request.
Get getRequest = reporting.files().get(reportId, fileId);
// Optional: adjust the chunk size used when downloading the file.
// getRequest.getMediaHttpDownloader().setChunkSize(MediaHttpDownloader.MAXIMUM_CHUNK_SIZE);
// Execute the get request and download the file.
try (OutputStream stream = new FileOutputStream(outFile)) {
getRequest.executeMediaAndDownloadTo(stream);
}
System.out.printf("File %d downloaded to %s%n", file.getId(), outFile.getAbsolutePath());
}
PHP
// Retrieve the file metadata.
$file = $this->service->files->get($reportId, $fileId);
if ($file->getStatus() === 'REPORT_AVAILABLE') {
try {
// Prepare a local file to download the report contents to.
$fileName = join(
DIRECTORY_SEPARATOR,
[sys_get_temp_dir(), $this->generateFileName($file)]
);
$fileResource = fopen($fileName, 'w+');
$fileStream = \GuzzleHttp\Psr7\stream_for($fileResource);
// Execute the get request and download the file.
$httpClient = $this->service->getClient()->authorize();
$result = $httpClient->request(
'GET',
$file->getUrls()->getApiUrl(),
[\GuzzleHttp\RequestOptions::SINK => $fileStream]
);
printf('<h3>Report file saved to: %s</h3>', $fileName);
} finally {
$fileStream->close();
fclose($fileResource);
}
}
Python
# Retrieve the file metadata.
report_file = service.files().get(
reportId=report_id, fileId=file_id).execute()
if report_file['status'] == 'REPORT_AVAILABLE':
# Prepare a local file to download the report contents to.
out_file = io.FileIO(generate_file_name(report_file), mode='wb')
# Create a get request.
request = service.files().get_media(reportId=report_id, fileId=file_id)
# Create a media downloader instance.
# Optional: adjust the chunk size used when downloading the file.
downloader = http.MediaIoBaseDownload(
out_file, request, chunksize=CHUNK_SIZE)
# Execute the get request and download the file.
download_finished = False
while download_finished is False:
_, download_finished = downloader.next_chunk()
print('File %s downloaded to %s' % (report_file['id'],
os.path.realpath(out_file.name)))
Ruby
# Retrieve the file metadata.
report_file = service.get_file(report_id, file_id)
return unless report_file.status == 'REPORT_AVAILABLE'
# Prepare a local file to download the report contents to.
File.open(generate_file_name(report_file), 'w') do |out_file|
# Execute the download request. Providing a download destination
# retrieves the file contents rather than the file metadata.
service.get_file(report_id, file_id, download_dest: out_file)
puts format('File %s downloaded to %s', file_id,
File.absolute_path(out_file.path))
end
ดาวน์โหลดต่อได้
เมื่อดาวน์โหลดไฟล์รายงานขนาดใหญ่ อาจเป็นไปได้ว่าการดาวน์โหลดล้มเหลวในระหว่างนี้ บริการไฟล์จะสนับสนุนฟังก์ชันการดาวน์โหลดบางส่วนเพื่อให้การกู้คืนและการดาวน์โหลดที่ล้มเหลวต่อง่ายขึ้น
การดาวน์โหลดบางส่วนเกี่ยวข้องกับการขอไฟล์เฉพาะส่วนหนึ่ง ทำให้คุณสามารถแบ่งการดาวน์โหลดขนาดใหญ่ออกเป็นส่วนย่อยๆ ได้ คุณระบุส่วนของไฟล์ที่ต้องการดาวน์โหลดได้โดยใส่ช่วงไบต์ไว้ในส่วนหัว HTTP ของ Range
ในคำขอ เช่น
Range: bytes=500-999
ฟังก์ชันการดาวน์โหลดบางส่วนมีให้บริการโดยไลบรารีของไคลเอ็นต์จำนวนมากผ่านบริการดาวน์โหลดสื่อ โปรดดูรายละเอียดในเอกสารประกอบของไลบรารีของไคลเอ็นต์
ดาวน์โหลดไฟล์ในเบราว์เซอร์
หากต้องการระบุวิธีดาวน์โหลดไฟล์จากเว็บเบราว์เซอร์โดยตรงให้แก่ผู้ใช้ ให้ใช้ URL ที่ระบุในช่อง browserUrl
ของแหล่งข้อมูลไฟล์ คุณสามารถเปลี่ยนเส้นทางผู้ใช้ไปยัง URL นี้ หรือเสนอเป็นลิงก์ที่คลิกได้ ในทั้ง 2 กรณี ผู้ใช้จะต้องเข้าสู่ระบบบัญชี Google ที่มีสิทธิ์เข้าถึงการรายงานของ Campaign Manager 360 และมีสิทธิ์ที่ถูกต้องในการเข้าถึงไฟล์ที่ระบุเพื่อเริ่มการดาวน์โหลด
C#
File file = service.Files.Get(reportId, fileId).Execute();
String browserUrl = file.Urls.BrowserUrl;
Java
File file = reporting.files().get(reportId, fileId).execute();
String browserUrl = file.getUrls().getBrowserUrl();
PHP
$file = $this->service->files->get($reportId, $fileId);
$browserUrl = $file->getUrls()->getBrowserUrl();
Python
report_file = service.files().get(
reportId=report_id, fileId=file_id).execute()
browser_url = report_file['urls']['browserUrl']
Ruby
report_file = service.get_file(report_id, file_id)
browser_url = report_file.urls.browser_url