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

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

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

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

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

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

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

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

  3. ในรายการ API ที่เปิดใช้ ให้ตรวจสอบว่ามีการแสดงคอมโพเนนต์ Google Cloud Storage หากไม่อยู่ในรายการ ให้คลิกแท็บ Google APIs ค้นหาและเลือก Google Cloud คอมโพเนนต์พื้นที่เก็บข้อมูล แล้วคลิกเปิดใช้ 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. ติดต่อจอแสดงผลและ ทีมสนับสนุนของ Video 360 เพื่อขอเปิดใช้ไฟล์อ่านเอนทิตี และ/หรือ Google Bid Manager API

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

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

    หลังจากเพิ่ม Google Group แล้ว คุณจะเพิ่มและนำผู้ใช้ออกจาก Google Group ได้ตามต้องการ โดยไม่ต้องเกี่ยวข้องใดๆ เพิ่มเติมจากดิสเพลย์และ ทีมสนับสนุน 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 API ทั่วไปแล้ว คุณยังต้องดาวน์โหลด ห้องสมุด สำหรับ 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 APIs สำหรับ .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;
        }

    }
}