Campaign Manager 360 API की मदद से, रिपोर्ट फ़ाइलें डाउनलोड की जा सकती हैं. ये फ़ाइलें, रिपोर्ट बिल्डर रिपोर्ट चलाने की वजह से मिलती हैं. इसकी मदद से, उपयोगकर्ताओं को लिंक का इस्तेमाल करके, फ़ाइल का सीधा ऐक्सेस भी दिया जा सकता है.
आपको जिस तरह का डाउनलोड करना है उसके आधार पर, इनमें से किसी एक तरीके का इस्तेमाल करें:
- डायरेक्ट डाउनलोड —
alt=media
पैरामीटर के साथ Files.get. - फ़ाइलों को ब्राउज़र में डाउनलोड करें —
browserUrl
को फ़ाइलें रिसॉर्स से.
इस गाइड के बाकी हिस्से में, फ़ाइल संसाधन के ज़रिए इस तरह के डाउनलोड करने के बारे में ज़्यादा जानकारी दी गई है.
ज़रूरी शर्तें
फ़ाइल डाउनलोड करने के लिए, आपको कुछ जानकारी की ज़रूरत होगी:
- उस रिपोर्ट का आईडी जिससे फ़ाइल जुड़ी है. ऐसा करने के लिए नई रिपोर्ट बनाएं या मौजूदा रिपोर्ट देखें.
- डाउनलोड की जाने वाली फ़ाइल का आईडी. पिछले चरण से रिपोर्ट चलाकर या मौजूदा फ़ाइलों की सूची की क्वेरी करके, इसका पता लगाया जा सकता है. इसका उदाहरण नीचे दिया गया है:
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
पर सेट करना ज़रूरी है.
डायरेक्ट डाउनलोड
सीधे तौर पर डाउनलोड करने के लिए, Files सेवा को अनुमति वाला एचटीटीपी GET
अनुरोध करें और क्वेरी पैरामीटर alt=media
को शामिल करें. अनुरोध का एक उदाहरण कुछ ऐसा दिख सकता है:
GET https://www.googleapis.com/dfareporting/v3.4/reports/12345/files/12345?alt=media HTTP/1.1 Authorization: Bearer your_auth_token
ध्यान रखें कि आपको मिलने वाले जवाब में एक रीडायरेक्ट शामिल होगा, इसलिए आपके ऐप्लिकेशन को बेहतर तरीके से कॉन्फ़िगर करना चाहिए, ताकि वह इसे अपने-आप मैनेज कर सके. अगर आपको इसे मैन्युअल तरीके से मैनेज करना है, तो रिस्पॉन्स के Location
हेडर में दूसरा वेबलिंक मिल जाएगा.
Google की ज़्यादातर आधिकारिक क्लाइंट लाइब्रेरी में, डेटा को सीधे तौर पर डाउनलोड करने के लिए आसान तरीके उपलब्ध हैं. इसका उदाहरण नीचे दिया गया है. अगर आपको मैन्युअल तरीके से डाउनलोड शुरू करना है, तो फ़ाइल रिसॉर्स के 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
फिर से शुरू किए जा सकने वाले डाउनलोड
बड़ी रिपोर्ट फ़ाइलें डाउनलोड करते समय, हो सकता है कि डाउनलोड पूरा न हो पाए. किसी फ़ेल हुए डाउनलोड को वापस पाने और उसे फिर से शुरू करने की प्रोसेस को आसान बनाने के लिए, फ़ाइल सेवा आंशिक डाउनलोड की सुविधा के साथ काम करती है.
आंशिक डाउनलोड में, फ़ाइल के खास हिस्सों के लिए अनुरोध करना शामिल है. इससे, डाउनलोड के बड़े हिस्सों को छोटे-छोटे हिस्सों में बांटा जा सकता है. आपके पास यह तय करने का विकल्प होता है कि आपको फ़ाइल का कौनसा हिस्सा डाउनलोड करना है. इसके लिए, अपने अनुरोध के Range
एचटीटीपी हेडर में बाइट रेंज शामिल करें. उदाहरण के लिए:
Range: bytes=500-999
कई क्लाइंट लाइब्रेरी, मीडिया डाउनलोड सेवा के ज़रिए, कुछ हद तक डाउनलोड की गई फ़ाइलें उपलब्ध कराती हैं. ज़्यादा जानकारी के लिए, क्लाइंट लाइब्रेरी का दस्तावेज़ देखें.
ब्राउज़र में फ़ाइलें डाउनलोड करना
अगर आपको उपयोगकर्ताओं को सीधे उनके वेब ब्राउज़र से फ़ाइल डाउनलोड करने का तरीका उपलब्ध कराना है, तो फ़ाइल रिसॉर्स के browserUrl
फ़ील्ड में दिए गए यूआरएल का इस्तेमाल करें. उपयोगकर्ता को इस यूआरएल पर रीडायरेक्ट किया जा सकता है या उसे क्लिक किए जा सकने वाले लिंक के तौर पर उपलब्ध कराया जा सकता है. दोनों ही मामलों में, उपयोगकर्ता को ऐसे 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