רשת המדיה של Google אפליקציית Video 360 מאחסנת באופן אוטומטי קובצי Entity Read ב-Google Cloud Storage. רק משתמשים עם הרשאת גישה לקבצים האלה חשבונות שקיבלו גישה.
יצירת פרויקט
יוצרים ומגדירים פרויקט באופן הבא:
- עוברים אל דף Google API Enabled API.
אם עדיין לא נכנסת לחשבון Google, תופיע בקשה להיכנס לחשבון.
- בתפריט הנפתח של הפרויקטים, בוחרים פרויקט קיים או יוצרים פרויקט חדש.
- אם יש לכם כמה פרויקטים קיימים, חשוב לבחור את הפרויקט שעבורו רוצים להפעיל את Google Cloud Storage, ואז בוחרים המשך.
- לחלופין, אם אין לכם פרויקט, בוחרים באפשרות Create a new project (יצירת פרויקט חדש), מזינים שם לפרויקט ובוחרים באפשרות Create.
הערה: חשוב לזכור שחלק ממזהי המשאבים (כמו מזהי פרויקטים), יכול להיות שהם יישמרו גם אחרי חיי הפרויקט. לכן כדאי להימנע משמירת מידע רגיש במשאב מזהים.
- ברשימה של Enabled APIs, מוודאים שהרכיב של Google Cloud Storage מופיע.
אם הוא לא מופיע, לוחצים על הכרטיסייה Google APIs, מחפשים את Google Cloud ובוחרים בו.
רכיב אחסון, ולוחצים על Enable API.
שימו לב: ברוב הפרויקטים הרכיב Google Cloud Storage כבר מופעל. אם אתם מתכננים כדי לגשת ל-Google Cloud Storage באמצעות API בפורמט JSON, ואז צריך גם לוודא ש-Google Cloud Storage JSON API מופעל.
- להפעיל את החיוב בפרויקט.
כדי להשתמש ב-Google Cloud Storage, צריך להפעיל את החיוב בחשבון הפרויקט, אם עדיין לא עשיתם זאת. הפעלת החיוב לא בהכרח המשמעות היא שתחויבו. מידע נוסף זמין במאמר תמחור.
- עדיין בתוך הפרויקט של האחסון בענן, לוחצים על תפריט הגלריה בפינה הימנית העליונה ובוחרים באפשרות חיוב.
- אם עדיין אין לכם חשבון לחיוב, צריך לפעול לפי ההוראות כדי ליצור חשבון חדש לחיוב.
- אם יש לכם חשבון קיים לחיוב, בוחרים אותו ואז בוחרים באפשרות הגדרת חשבון.
במסוף Google API, צריך להישאר באותו הפרויקט ולעבור לקטע הבא.
הגדרת הגישה ל-Cloud Storage
עדיין בתוך מסוף Google API בפרויקט הנדרש, לוחצים על תפריט הגלריה בפינה הימנית העליונה, בוחרים באפשרות APIs & שירותים, ולאחר מכן לוחצים על פרטי כניסה. ב בדף פרטי כניסה, מחפשים את האפשרות מפתחות לחשבונות שירות.
מוצאים את כתובת האימייל של המפתח לחשבון השירות:
- בוחרים בקישור ניהול חשבונות שירות (משמאל לקטע של מפתחות של חשבונות שירות).
- אפשר לראות את כתובת האימייל בדף ההרשאות שמתקבל.
אם אין מפתחות של חשבונות שירות, יוצרים חשבון שירות חדש:
- לוחצים על התפריט הנפתח Create credentials ובאפשרות Service account key.
- בתפריט הנפתח Service account, בוחרים באפשרות New service Account.
- מזינים Name לחשבון השירות. מזהה חשבון השירות מוגדר אוטומטית שנוצר מהשם ומשם הפרויקט.
- חשוב לשים לב של כתובת האימייל של חשבון השירות
gserviceaccount.com
. הוא לא מוצג בדף 'פרטי כניסה' אחרי שבוחרים באפשרות יצירה. - בקטע Key type, בוחרים באפשרות JSON.
- לוחצים על יצירה. קובץ ה-JSON עם זוג המפתחות הציבורי/פרטי של החשבון נשמר בתיקיית ההורדות. המפתח הזה משמש לאימות האפליקציה שלכם מול ה-API לחתום על כל הבקשות שהוא שולח אל ה-API. שומרים במקום בטוח את קובץ ה-JSON שנוצר, במקום שבו האפליקציה תוכל לגשת אליו.
יצירת קשר עם צוות התצוגה צוות התמיכה של Video 360 מבקש להפעיל קובצי Entity Read ו/או Google Bid Manager API.
אחרי שצוות התמיכה יאשר את הגישה, צריך ליצור קבוצה ייעודית ב-Google (או קבוצות) ולעדכן את השדות הבאים ב-Display & פרטי השותף ב-Video 360 (לתשומת ליבכם: רק משתמשים עם גישה ברמת השותף, תוכלו לבצע את העדכונים הבאים:
- רישום ביומן, קריאת הקבוצה ב-Google
לקבוצה לקריאה בלבד יש הרשאות רק לקריאת קבצים ולרישום התוכן של קטגוריות האחסון. - קבוצת Google לניהול היומנים
לקבוצת הניהול יש את כל ההרשאות של הקבוצה לקריאה בלבד, אבל אפשר גם לשנות אותה את רשימות ה-ACL של הקבצים בקטגוריות. שימו לב שאפילו לקבוצת הניהול אין הרשאה כדי לשנות רשימות ACL של קטגוריות, וגם אין להן הרשאה ליצור, לשנות או למחוק קבצים.
אחרי שמוסיפים את הקבוצה ב-Google, אפשר להוסיף משתמשים לקבוצה ולהסיר אותם ממנה לפי הצורך. ללא מעורבות נוספת של רשת המדיה צוות התמיכה של Video 360; רק המשתמשים האלה שנמצאים בקבוצה שלך יקבלו גישה לנתונים שלך.
- רישום ביומן, קריאת הקבוצה ב-Google
גישה לנתונים
גישה לנתונים באמצעות gsutil
כלי gsutil הוא אפליקציית שורת פקודה שמאפשרת לכם לגשת נתונים של Google Cloud Storage בקלות ללא צורך לתכנת חוויה אישית. לדוגמה, תוכלו להשתמש ב-gsutil כחלק מסקריפט או מקובץ אצווה במקום להשתמש ב-gsutil. במהלך יצירת אפליקציות מותאמות אישית.
כדי להתחיל להשתמש ב-gsutil, כדאי לקרוא את מסמכי התיעוד של gsutil. עדיין תצטרכו לבצע אימות באמצעות OAuth 2 כמו קודם, אבל gsutil עצמו יבקש מכם לבצע אימות את פרטי הכניסה בפעם הראשונה שתשתמשו בהם ואז תאחסנו אותם לשימוש במועד מאוחר יותר.
גישה לנתונים באופן פרוגרמטי
ל-Google Cloud Storage יש ממשקי API לשפות תכנות רבות,
כדי לגשת לנתונים באופן פרוגרמטי. הדוגמה הבאה מראה איך לבצע
הורדה של אחד מ-Entity Read Files
מ-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 APIs ל- .NET מאפשרת גישה קלה ופשוטה לנתונים ששמורים ב-Google Cloud Storage.
קודם כל צריך להגדיר את פרטי הכניסה שישמשו בתקשורת עם ה-API, כדי לקבל הרשאה לגשת לנתונים שלכם. כדי לעשות את זה, יוצרים אובייקט AssertionFlowClient
שמשתמש בכתובת האימייל developer.gserviceaccount.com
של חשבון השירות בתור ServiceAccountId
, וגם טוען את קובץ המפתח הפרטי שהורד מ-Google API Console כשיצרת את חשבון השירות.
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:
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; } } }