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

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

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

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

इस गाइड के बाकी हिस्से में, फ़ाइल संसाधन की मदद से इस तरह के डाउनलोड करने के बारे में पूरी जानकारी दिए गए हैं.

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

फ़ाइल डाउनलोड करने के लिए, आपको कुछ जानकारी की ज़रूरत होगी:

  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));

129

$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());
}

129

// 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();

129

$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