रिपोर्ट की फ़ाइलें डाउनलोड करें

Campaign Manager 360 API की मदद से, रिपोर्ट फ़ाइलें डाउनलोड की जा सकती हैं. ये फ़ाइलें, Report Builder रिपोर्ट को चलाने के बाद मिलती हैं. यह उपयोगकर्ताओं को लिंक के ज़रिए, किसी फ़ाइल का ऐक्सेस सीधे तौर पर देने की सुविधा भी देता है.

आपको जिस तरह का डेटा डाउनलोड करना है उसके हिसाब से, इनमें से किसी एक तरीके का इस्तेमाल करें:

  • सीधे तौर पर डाउनलोड करना — alt=media पैरामीटर के साथ Files.get.
  • ब्राउज़र में फ़ाइलें डाउनलोड करना — browserUrl फ़ाइल रिसॉर्स से.

इस गाइड के बाकी हिस्से में, Files resource के ज़रिए इस तरह के डाउनलोड करने के बारे में ज़्यादा जानकारी दी गई है.

ज़रूरी शर्तें

फ़ाइल डाउनलोड करने के लिए, आपको यह जानकारी देनी होगी:

  1. उस रिपोर्ट का आईडी जिससे फ़ाइल जुड़ी है. इसे नई रिपोर्ट बनाकर या मौजूदा रिपोर्ट देखकर पता लगाया जा सकता है.
  2. डाउनलोड की जाने वाली फ़ाइल का आईडी. इसे पिछले चरण से रिपोर्ट जनरेट करके या मौजूदा फ़ाइलों की सूची को क्वेरी करके पता लगाया जा सकता है. उदाहरण के लिए, यहां दिया गया तरीका अपनाएं:

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 पर सेट किया जाना चाहिए.

सीधे तौर पर डाउनलोड करना

सीधे तौर पर डाउनलोड करने के लिए, फ़ाइल सेवा को अनुमति वाला एचटीटीपी 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