เริ่มต้นใช้งาน Cloud Storage

Google Display & Video 360 จะจัดเก็บไฟล์อ่านเอนทิตีไว้ใน Google Cloud Storage โดยอัตโนมัติ เฉพาะบัญชีที่ได้รับสิทธิ์เท่านั้น จึงจะเข้าถึงไฟล์เหล่านี้ได้

สร้างโปรเจ็กต์

สร้างและกำหนดค่าโปรเจ็กต์ดังนี้

  1. ไปที่หน้า API ที่เปิดใช้คอนโซล Google API

    หากคุณยังไม่ได้ลงชื่อเข้าใช้ Google คุณจะได้รับข้อความแจ้งให้ลงชื่อเข้าใช้

  2. เลือกโปรเจ็กต์ที่มีอยู่หรือสร้างโปรเจ็กต์ใหม่จากรายการแบบเลื่อนลงของโปรเจ็กต์
    • หากคุณมีหลายโปรเจ็กต์ ให้เลือกโปรเจ็กต์ที่ต้องการเปิดใช้งาน Google Cloud Storage แล้วเลือกต่อไป
    • หรือหากยังไม่มีโปรเจ็กต์ ให้เลือกสร้างโปรเจ็กต์ใหม่ ป้อนชื่อโปรเจ็กต์ แล้วเลือกสร้าง

      หมายเหตุ: คุณควรทราบว่าตัวระบุทรัพยากรบางอย่าง (เช่น รหัสโปรเจ็กต์) อาจได้รับการเก็บรักษาไว้เกินอายุการใช้งานของโปรเจ็กต์ ด้วยเหตุนี้ ให้หลีกเลี่ยงการจัดเก็บข้อมูลที่ละเอียดอ่อนในตัวระบุทรัพยากร

  3. ในรายการ API ที่เปิดใช้ ให้ตรวจสอบว่ามีคอมโพเนนต์ Google Cloud Storage แสดงอยู่ หากไม่มี ให้คลิกแท็บ Google APIs จากนั้นค้นหาและเลือกคอมโพเนนต์ Google Cloud Storage แล้วคลิกเปิดใช้ API

    โปรดทราบว่าโปรเจ็กต์ส่วนใหญ่จะเปิดใช้คอมโพเนนต์ Google Cloud Storage อยู่แล้ว หากวางแผนที่จะเข้าถึง Google Cloud Storage โดยใช้ JSON API คุณควรยืนยันว่าเปิดใช้ Google Cloud Storage JSON API แล้ว

  4. เปิดใช้การเรียกเก็บเงินสำหรับโปรเจ็กต์

    ก่อนที่จะใช้ Google Cloud Storage คุณต้องเปิดใช้การเรียกเก็บเงินสำหรับโปรเจ็กต์หากยังไม่ได้ดำเนินการ การเปิดใช้การเรียกเก็บเงินไม่ได้หมายความว่าคุณจะมีการเรียกเก็บเงิน ดูข้อมูลเพิ่มเติมได้ที่ราคา

    1. ยังคงอยู่ในโครงการพื้นที่เก็บข้อมูลระบบคลาวด์ ให้คลิกเมนูแกลเลอรีที่มุมซ้ายบน แล้วเลือกการเรียกเก็บเงิน
    2. หากคุณยังไม่มีบัญชีสำหรับการเรียกเก็บเงิน ให้ทำตามวิธีการสร้างบัญชีสำหรับการเรียกเก็บเงินใหม่
    3. หากคุณมีบัญชีสำหรับการเรียกเก็บเงินอยู่แล้ว ให้เลือกบัญชีดังกล่าว แล้วเลือกตั้งค่าบัญชี

ในคอนโซล Google API ให้อยู่ในโปรเจ็กต์เดียวกันแล้วไปที่ส่วนถัดไป

ตั้งค่าการเข้าถึง Cloud Storage

  1. ขณะยังอยู่ในคอนโซล Google API ภายในโปรเจ็กต์ที่ต้องการ ให้คลิกเมนูแกลเลอรีที่มุมซ้ายบน เลือก API และบริการ แล้วคลิกข้อมูลเข้าสู่ระบบ ในหน้าข้อมูลเข้าสู่ระบบ ให้ค้นหาคีย์บัญชีบริการ

    ค้นหาอีเมลคีย์บัญชีบริการ ดังนี้

    1. เลือกลิงก์จัดการบัญชีบริการ (ทางด้านขวาของส่วนคีย์บัญชีบริการ)
    2. ดูที่อยู่อีเมลได้ในหน้าสิทธิ์ที่ได้รับ

    หากไม่มีคีย์บัญชีบริการ ให้สร้างบัญชีบริการใหม่โดยทำดังนี้

    1. เลือกรายการแบบเลื่อนลงสร้างข้อมูลเข้าสู่ระบบ แล้วเลือกคีย์บัญชีบริการ
    2. ในรายการแบบเลื่อนลงของบัญชีบริการ ให้เลือกบัญชีบริการใหม่
    3. ป้อนชื่อสำหรับบัญชีบริการ ระบบจะสร้างรหัสบัญชีบริการโดยอัตโนมัติจากชื่อและชื่อโปรเจ็กต์
    4. จดรหัสบัญชีบริการ gserviceaccount.com อีเมล ซึ่งจะไม่แสดงในหน้าข้อมูลเข้าสู่ระบบหลังจากที่คุณเลือกสร้าง
    5. ในส่วนประเภทคีย์ ให้เลือก JSON
    6. คลิกสร้าง ระบบจะบันทึกไฟล์ JSON ที่มีคู่คีย์สาธารณะ/ส่วนตัวของบัญชีลงในโฟลเดอร์ดาวน์โหลด คีย์นี้ใช้ตรวจสอบสิทธิ์แอปพลิเคชันของคุณกับ API และลงนามในคำขอทั้งหมดที่ส่งไปยัง API ให้จัดเก็บไฟล์ JSON ที่สร้างขึ้นไว้ในที่ปลอดภัย ในที่ที่แอปพลิเคชันเข้าถึงได้
  2. ติดต่อทีมสนับสนุนของ Display & Video 360 เพื่อขอเปิดใช้ไฟล์อ่านเอนทิตีและ/หรือ Google Bid Manager API

    หลังจากทีมสนับสนุนยืนยันการเข้าถึงแล้ว ให้สร้าง Google Group (หรือกลุ่ม) เฉพาะ และอัปเดตช่องต่อไปนี้ในรายละเอียดพาร์ทเนอร์ Display & Video 360 (โปรดทราบว่าเฉพาะผู้ใช้ที่มีสิทธิ์เข้าถึงระดับพาร์ทเนอร์เท่านั้นที่จะทำการอัปเดตเหล่านี้ได้)

    • กลุ่มใน Google ที่อ่านบันทึก
      กลุ่มแบบอ่านอย่างเดียวจะมีสิทธิ์อ่านไฟล์และแสดงเนื้อหาของที่เก็บข้อมูลของพื้นที่เก็บข้อมูลเท่านั้น
    • การจัดการบันทึกของกลุ่ม Google
      กลุ่มการจัดการมีสิทธิ์ทั้งหมดของกลุ่มแบบอ่านอย่างเดียว แต่เปลี่ยน ACL ของไฟล์ภายในที่เก็บข้อมูลได้ด้วย โปรดทราบว่าแม้แต่กลุ่มการจัดการก็ไม่มีสิทธิ์เปลี่ยน ACL ของที่เก็บข้อมูล หรือไม่มีสิทธิ์สร้าง แก้ไข หรือลบไฟล์

    หลังจากเพิ่ม Google Group แล้ว คุณจะเพิ่มผู้ใช้และนำผู้ใช้ออกจาก Google Group ได้ตามต้องการโดยไม่ต้องขอความช่วยเหลือจากทีมสนับสนุนของ Display & Video 360 อีก เฉพาะผู้ใช้ที่อยู่ในกลุ่มเท่านั้นที่จะเข้าถึงข้อมูลของคุณได้

เข้าถึงข้อมูลของคุณ

เข้าถึงข้อมูลโดยใช้ gsutil

เครื่องมือ gsutil เป็นแอปพลิเคชันบรรทัดคำสั่งที่ช่วยให้คุณเข้าถึงข้อมูล Google Cloud Storage ได้อย่างง่ายดายโดยไม่ต้องใช้ประสบการณ์ในการเขียนโปรแกรม เช่น คุณอาจใช้ gsutil เป็นส่วนหนึ่งของสคริปต์หรือไฟล์กลุ่มแทนการสร้างแอปพลิเคชันที่กำหนดเอง

หากต้องการเริ่มต้นใช้งาน gsutil โปรดอ่านเอกสารเกี่ยวกับ gsutil คุณยังคงต้องตรวจสอบสิทธิ์โดยใช้ OAuth 2 ตามเดิม แต่ gsutil เองจะแสดงข้อความแจ้งให้คุณป้อนข้อมูลเข้าสู่ระบบในครั้งแรกที่ใช้ จากนั้นจะจัดเก็บไว้เพื่อใช้ในภายหลัง

เข้าถึงข้อมูลด้วยโปรแกรม

Google Cloud Storage มี API สำหรับภาษาโปรแกรมต่างๆ ที่ช่วยให้คุณเข้าถึงข้อมูลด้วยวิธีแบบเป็นโปรแกรม ตัวอย่างด้านล่างแสดงวิธีดาวน์โหลดไฟล์อ่านเอนทิตีอย่างง่ายๆ จาก Google Cloud Storage โปรดทราบว่าระบบจะจัดเก็บเฉพาะไฟล์อ่านเอนทิตีสาธารณะในที่เก็บข้อมูล gdbm-public โดยระบบจะจัดเก็บ ไฟล์อ่านเอนทิตีส่วนตัวของคุณไว้ในที่เก็บข้อมูลเฉพาะพาร์ทเนอร์และผู้ลงโฆษณา

Java

ตัวอย่างต่อไปนี้ใช้ไลบรารีของไคลเอ็นต์ Google API สำหรับ Java ซึ่งทำให้การเข้าถึงข้อมูลที่เก็บไว้ใน Google Cloud Storage เป็นเรื่องง่าย นอกเหนือจากไลบรารีของไคลเอ็นต์ Google APIs หลักทั่วไปแล้ว คุณจะต้องดาวน์โหลดไลบรารีสำหรับ Cloud Storage API แยกต่างหากด้วย

ก่อนอื่น คุณจะต้องตั้งค่าข้อมูลเข้าสู่ระบบที่จะใช้เมื่อสื่อสารกับ API เพื่อรับสิทธิ์ในการเข้าถึงข้อมูล ซึ่งทำได้โดยการสร้างออบเจ็กต์ Credential ที่ใช้อีเมล developer.gserviceaccount.com ของบัญชีบริการเป็น ServiceAccountId และโหลดไฟล์คีย์ส่วนตัวที่ดาวน์โหลดจากคอนโซล Google API ขณะสร้างบัญชีบริการด้วย การดำเนินการนี้จะช่วยให้มั่นใจว่าคำขอทั้งหมดในอนาคตที่ส่งไปยัง API ได้รับการลงนามด้วยคีย์ส่วนตัวของบัญชีบริการ

NetHttpTransport HTTP_TRANSPORT = new NetHttpTransport();
JacksonFactory JSON_FACTORY = new JacksonFactory();

...

Credential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
   .setJsonFactory(JSON_FACTORY)
   .setServiceAccountId("...@developer.gserviceaccount.com")
   .setServiceAccountScopes(StorageScopes.DEVSTORAGE_READ_ONLY)
   .setServiceAccountPrivateKeyFromP12File(new File("...-privatekey.p12"))
   .build();

หลังจากสร้างออบเจ็กต์ Credential แล้ว คุณจะใช้ออบเจ็กต์ดังกล่าวเพื่อสร้างออบเจ็กต์ Storage ใหม่ที่คุณใช้สำหรับคำขอทั้งหมดในอนาคตไปยัง API ของ Google Cloud Storage ได้ โดยทำดังนี้

Storage storage = new Storage(HTTP_TRANSPORT, JSON_FACTORY, credential);

เมื่อมีออบเจ็กต์ Storage ใหม่แล้ว คุณก็พร้อมที่จะเริ่มส่งคำขอไปยัง API แล้ว คุณกำลังส่งคำขอเพื่อเรียกรายการ bucketObjectName ที่เฉพาะเจาะจงจากที่เก็บข้อมูล bucket ไลบรารีจะจัดการการดาวน์โหลด HTTP ของไฟล์จาก Google Cloud Storage จากนั้นพิมพ์เนื้อหาออกในคอนโซล โดยทำดังนี้

Get bucketObject = storage.objects().get(bucketName, bucketObjectName);

ByteArrayOutputStream output = new ByteArrayOutputStream();
bucketObject.getMediaHttpDownloader().setDirectDownloadEnabled(true);
bucketObject.executeAndDownloadTo(output);

System.out.println(output.toString());

ค่าสำหรับ bucket และ bucketObjectName จะแตกต่างกันไปตามส่วนของ API ที่คุณกำลังเข้าถึง โปรดดูส่วนที่เกี่ยวข้องเกี่ยวกับแบบแผนการตั้งชื่อ

C#

ตัวอย่างต่อไปนี้ใช้ไลบรารีของไคลเอ็นต์ Google API สำหรับ .NET ซึ่งทำให้การเข้าถึงข้อมูลที่เก็บไว้ใน Google Cloud Storage เป็นเรื่องง่าย

โดยก่อนอื่นคุณจะต้องตั้งค่าข้อมูลเข้าสู่ระบบที่จะใช้เมื่อสื่อสารกับ API เพื่อรับสิทธิ์ในการเข้าถึงข้อมูล ซึ่งทำได้โดยการสร้างออบเจ็กต์ AssertionFlowClient ที่ใช้อีเมล developer.gserviceaccount.com ของบัญชีบริการเป็น ServiceAccountId และยังโหลดไฟล์คีย์ส่วนตัวที่ดาวน์โหลดจากคอนโซล Google API ขณะสร้างบัญชีบริการด้วย

string keyPath = @"...-privatekey.p12";
X509Certificate2 certificate = new X509Certificate2(keyPath, "notasecret", X509KeyStorageFlags.Exportable);
var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
{
    ServiceAccountId = "...@developer.gserviceaccount.com",
    Scope = StorageService.Scopes.DevstorageRead_only.GetStringValue()
};

หลังจากสร้างออบเจ็กต์ AssertionFlowClient แล้ว คุณจะสามารถใช้ออบเจ็กต์ดังกล่าวในการสร้างออบเจ็กต์ OAuth2Authenticator ใหม่ที่คุณสามารถใช้สร้างอินสแตนซ์ Storage ใหม่สำหรับคำขอในอนาคตทั้งหมดที่ส่งไปยัง API ของ Google Cloud Storage API ได้ โดยทำดังนี้

var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);
StorageService service = new StorageService(auth);

เมื่อคุณมีออบเจ็กต์ Storage ใหม่แล้ว คุณก็พร้อมที่จะเริ่มส่งคำขอไปยัง API แล้ว คุณกำลังส่งคำขอเพื่อเรียกข้อมูลรายการ bucketObjectName จากที่เก็บข้อมูล bucket เราใช้คลาสไลบรารีของไคลเอ็นต์ .NET มาตรฐานในการจัดการการดาวน์โหลด HTTP จาก Google Cloud Storage แต่คุณต้องจัดการกรณีที่มีการเปลี่ยนเส้นทางด้วยตนเองเพื่อให้มั่นใจว่าคำขอจะมีส่วนหัว Authorization ที่ถูกต้องเสมอ

var results = service.Objects.Get(bucketName, bucketObjectName).Fetch();

HttpWebRequest request = createRequest(results.Media.Link, auth);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// Handle redirects manually to ensure that the Authorization header is present if
// your request is redirected.
if (response.StatusCode == HttpStatusCode.TemporaryRedirect)
{
    request = createRequest(response.Headers["Location"], auth);
    response = (HttpWebResponse)request.GetResponse();
}

จากนั้นจะอ่านเนื้อหาของการตอบกลับได้จากออบเจ็กต์ HttpWebResponse ผ่าน GetResponseStream ตามปกติ ค่าสำหรับ bucket และ bucketObjectName จะแตกต่างกันไปตามส่วนของ API ที่เข้าถึง โปรดดูส่วนที่เกี่ยวข้องเกี่ยวกับแบบแผนการตั้งชื่อ

ภาคผนวก: ตัวอย่างที่สมบูรณ์

Java

/*
 * Copyright (c) 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.bidmanager.api.samples;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStreamReader;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.Storage.Objects.Get;
import com.google.api.services.storage.StorageScopes;

/**
 * A simple class that demonstrates how to download a specific object from a bucket using a
 * service account
 */
public class ServiceDownload {

  /**
   * This is the HTTP Transport object used for automatically refreshing access tokens.
   */
  static final NetHttpTransport HTTP_TRANSPORT = new NetHttpTransport();

  /**
   * This is the JSON factory used for parsing refresh token responses.Your first requirement
   */
  static final JacksonFactory JSON_FACTORY = new JacksonFactory();

  /**
   * The main method will attempt to download a specific named object from the gdbm-public bucket
   * using a service account.
   *
   * @param args Not used.
   */
  public static void main(String[] args) {

    try {
      // Prompt the user for the details of the object to download.
      System.out.print("Name of object to download, e.g. entity/20130430.0.Browser.json:");
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      String bucketObjectName = in.readLine();
      in.close();
      String bucketName = "gdbm-public";

      Credential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
           .setJsonFactory(JSON_FACTORY)
           .setServiceAccountId("...@developer.gserviceaccount.com")
           .setServiceAccountScopes(StorageScopes.DEVSTORAGE_READ_ONLY)
           .setServiceAccountPrivateKeyFromP12File(
               new File("...-privatekey.p12"))
           .build();

      Storage storage = new Storage(HTTP_TRANSPORT, JSON_FACTORY, credential);

      Get bucketObject = storage.objects().get(bucketName, bucketObjectName);

      ByteArrayOutputStream output = new ByteArrayOutputStream();
      bucketObject.getMediaHttpDownloader().setDirectDownloadEnabled(true);
      bucketObject.executeAndDownloadTo(output);
      System.out.println(output.toString());

    } catch (Exception e) {
      System.err.println("Unexpected exception caught: " + e.getMessage());
      e.printStackTrace();
    }

  }


}

C#

/*
 * Copyright (c) 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;

using DotNetOpenAuth.OAuth2;

using Google.Apis;
using Google.Apis.Requests;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Storage.v1beta1;
using Google.Apis.Storage.v1beta1.Data;
using Google.Apis.Util;

namespace ApiSamples
{
    /// <summary>
    /// A simple class that demonstrates how to download a specific object from a bucket using a
    /// service account
    /// </summary>
    class ServiceDownload
    {
        /// <summary>
        /// The main method will attempt to download a specific named object from the
        /// gdbm-public bucket using a service account.
        /// </summary>
        /// <param name="args">Not used.</param>
        public static void Main(string[] args)
        {

            // Prompt the user for the details of the object to download
            Console.WriteLine("Name of object to download, e.g. entity/20130430.0.Browser.json:");
            string bucketObjectName = Console.ReadLine();
            string bucketName = "gdbm-public";

            try
            {
                string keyPath = @"...-privatekey.p12";
                X509Certificate2 certificate = new X509Certificate2(keyPath, "notasecret", X509KeyStorageFlags.Exportable);
                var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
                {
                    ServiceAccountId = "...@developer.gserviceaccount.com",
                    Scope = StorageService.Scopes.DevstorageRead_only.GetStringValue()
                };
                var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);

                StorageService service = new StorageService(auth);
                var results = service.Objects.Get(bucketName, bucketObjectName).Fetch();

                HttpWebRequest request = createRequest(results.Media.Link, auth);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                // Handle redirects manully to ensure that the Authorization header is present if
                // our request is redirected.
                if (response.StatusCode == HttpStatusCode.TemporaryRedirect)
                {
                    request = createRequest(response.Headers["Location"], auth);
                    response = (HttpWebResponse)request.GetResponse();
                }

                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream);
                String data = reader.ReadToEnd();
                Console.Write(data);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception caught: " + e.Message);
                Console.Write(e.StackTrace);
            }
            Console.ReadKey();
        }

        /// <summary>
        /// Generate a HttpWebRequest for the given URL with the appropriate OAuth2 authorization
        /// header applied.  The HttpWebRequest object returned has its AllowAutoRedirect option
        /// disabled to allow us to manually handle redirects.
        /// </summary>
        /// <param name="url">URL that is to be requested with this object</param>
        /// <param name="auth">The OAuth2Authenticator instance that contains the appropriate keys.</param>
        /// <returns>HttpWebRequest object ready to be used to make requests to the API</returns>
        private static HttpWebRequest createRequest(string url, OAuth2Authenticator<AssertionFlowClient> auth)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Headers.Add("Authorization", "Bearer " + auth.State.AccessToken);
            request.AllowAutoRedirect = false;
            return request;
        }

    }
}