SlideShare ist ein Scribd-Unternehmen logo
1 von 2
Downloaden Sie, um offline zu lesen
GAE - Using CloudStorage
 
GAE中使用CloudStorage中的資源可以省略一些認證動作,透過FileReadChannel即可快速的取得
存取CloudStorage的方法,飯粒如下: 
 
Step1: 首先,Library的部分需要import下面藍色部分 
import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.nio.ByteBuffer; 
import java.nio.channels.Channels; 
import java.util.logging.Logger; 
 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import com.google.appengine.api.files.AppEngineFile; 
import com.google.appengine.api.files.FileReadChannel; 
import com.google.appengine.api.files.FileService; 
import com.google.appengine.api.files.FileServiceFactory; 
import com.google.appengine.api.files.FileWriteChannel; 
import com.google.appengine.api.files.GSFileOptions.GSFileOptionsBuilder; 
import com.google.appengine.api.files.LockException; 
 
 
最後,透過FileReadChannel讀取指定的CloudStorage檔案 
public void readFile(HttpServletRequest req, HttpServletResponse resp)  
      throws FileNotFoundException, LockException, IOException { 
  // Get the file service 
  FileService fileService = FileServiceFactory.getFileService(); 
  boolean lockForRead = false; 
  String filename = "/gs/mitac­cp300­eu/test.txt"; 
  AppEngineFile readableFile = new AppEngineFile(filename); 
  FileReadChannel readChannel =  
    fileService.openReadChannel(readableFile, lockForRead); 
 
  // Read the file in whichever way you'd like 
  BufferedReader reader =  
    new BufferedReader(Channels.newReader(readChannel, "UTF8")); 
  String line = ""; 
  int i = 0; 
  while((line = reader.readLine()) != null){ 
    resp.getWriter().println(i + ": " + line); 
    i++; 
  } 
 
  readChannel.close(); 
} 
 
 
附註: 
 
目前Google著手進行大規模的API Refactory,看起來將會讓所有的API遵循使用RESTful的呼叫方
式,因此API終會實作Credential的認證流程,本範例1.8.9版本以前測試均可以正常執行。 

Weitere ähnliche Inhalte

Andere mochten auch

GCS - Java to store data in Cloud Storage
GCS - Java to store data in Cloud StorageGCS - Java to store data in Cloud Storage
GCS - Java to store data in Cloud StorageSimon Su
 
JCConf 2016 - Dataflow Workshop Labs
JCConf 2016 - Dataflow Workshop LabsJCConf 2016 - Dataflow Workshop Labs
JCConf 2016 - Dataflow Workshop LabsSimon Su
 
GCE Windows Serial Console Usage Guide
GCE Windows Serial Console Usage GuideGCE Windows Serial Console Usage Guide
GCE Windows Serial Console Usage GuideSimon Su
 
Google Cloud Monitoring
Google Cloud MonitoringGoogle Cloud Monitoring
Google Cloud MonitoringSimon Su
 
Google Cloud Platform專案建立說明
Google Cloud Platform專案建立說明Google Cloud Platform專案建立說明
Google Cloud Platform專案建立說明Simon Su
 
Try Cloud Spanner
Try Cloud SpannerTry Cloud Spanner
Try Cloud SpannerSimon Su
 
均一Gae甘苦談
均一Gae甘苦談均一Gae甘苦談
均一Gae甘苦談蘇 倚恩
 

Andere mochten auch (7)

GCS - Java to store data in Cloud Storage
GCS - Java to store data in Cloud StorageGCS - Java to store data in Cloud Storage
GCS - Java to store data in Cloud Storage
 
JCConf 2016 - Dataflow Workshop Labs
JCConf 2016 - Dataflow Workshop LabsJCConf 2016 - Dataflow Workshop Labs
JCConf 2016 - Dataflow Workshop Labs
 
GCE Windows Serial Console Usage Guide
GCE Windows Serial Console Usage GuideGCE Windows Serial Console Usage Guide
GCE Windows Serial Console Usage Guide
 
Google Cloud Monitoring
Google Cloud MonitoringGoogle Cloud Monitoring
Google Cloud Monitoring
 
Google Cloud Platform專案建立說明
Google Cloud Platform專案建立說明Google Cloud Platform專案建立說明
Google Cloud Platform專案建立說明
 
Try Cloud Spanner
Try Cloud SpannerTry Cloud Spanner
Try Cloud Spanner
 
均一Gae甘苦談
均一Gae甘苦談均一Gae甘苦談
均一Gae甘苦談
 

Ähnlich wie GAE - Using CloudStorage through FileReadChannel

在雲端上啜飲爪哇
在雲端上啜飲爪哇在雲端上啜飲爪哇
在雲端上啜飲爪哇建興 王
 
Java 1(Java概述)
Java 1(Java概述)Java 1(Java概述)
Java 1(Java概述)xzdbd
 
Tku-網路資料的串接與資料儲存
Tku-網路資料的串接與資料儲存Tku-網路資料的串接與資料儲存
Tku-網路資料的串接與資料儲存Xavier Yin
 
跟我一起學 CakePHP
跟我一起學 CakePHP跟我一起學 CakePHP
跟我一起學 CakePHPRay Wei
 
Using google appengine_1027
Using google appengine_1027Using google appengine_1027
Using google appengine_1027Wei Sun
 
Auto fac的介紹 20131018
Auto fac的介紹 20131018Auto fac的介紹 20131018
Auto fac的介紹 20131018LearningTech
 
Auto fac的介紹 20131018
Auto fac的介紹 20131018Auto fac的介紹 20131018
Auto fac的介紹 20131018LearningTech
 
Using google appengine (2)
Using google appengine (2)Using google appengine (2)
Using google appengine (2)Wei Sun
 
How to avoid check style errors
How to avoid check style errorsHow to avoid check style errors
How to avoid check style errorsGuo Albert
 
拆分初始化负载
拆分初始化负载拆分初始化负载
拆分初始化负载kaven yan
 
Using google appengine_final
Using google appengine_finalUsing google appengine_final
Using google appengine_finalWei Sun
 
簡化 JVM 上雲 - 透過 Azure Spring Cloud 提升開發、發佈及服務監控效率
簡化 JVM 上雲 - 透過 Azure Spring Cloud 提升開發、發佈及服務監控效率簡化 JVM 上雲 - 透過 Azure Spring Cloud 提升開發、發佈及服務監控效率
簡化 JVM 上雲 - 透過 Azure Spring Cloud 提升開發、發佈及服務監控效率Shengyou Fan
 
OpenWebSchool - 11 - CodeIgniter
OpenWebSchool - 11 - CodeIgniterOpenWebSchool - 11 - CodeIgniter
OpenWebSchool - 11 - CodeIgniterHung-yu Lin
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Wade Huang
 
咩星征服計劃 用 Js 征服地球 Part II
咩星征服計劃 用 Js 征服地球 Part II咩星征服計劃 用 Js 征服地球 Part II
咩星征服計劃 用 Js 征服地球 Part II羊 小咩 (lamb-mei)
 
Php Webservers
Php WebserversPhp Webservers
Php Webserverssamon127
 

Ähnlich wie GAE - Using CloudStorage through FileReadChannel (20)

OSGi Small Lab
OSGi Small LabOSGi Small Lab
OSGi Small Lab
 
在雲端上啜飲爪哇
在雲端上啜飲爪哇在雲端上啜飲爪哇
在雲端上啜飲爪哇
 
Java 1(Java概述)
Java 1(Java概述)Java 1(Java概述)
Java 1(Java概述)
 
Tku-網路資料的串接與資料儲存
Tku-網路資料的串接與資料儲存Tku-網路資料的串接與資料儲存
Tku-網路資料的串接與資料儲存
 
Osgi Intro
Osgi IntroOsgi Intro
Osgi Intro
 
初探Maven 3
初探Maven 3初探Maven 3
初探Maven 3
 
跟我一起學 CakePHP
跟我一起學 CakePHP跟我一起學 CakePHP
跟我一起學 CakePHP
 
Using google appengine_1027
Using google appengine_1027Using google appengine_1027
Using google appengine_1027
 
System prereq chin
System prereq chinSystem prereq chin
System prereq chin
 
Auto fac的介紹 20131018
Auto fac的介紹 20131018Auto fac的介紹 20131018
Auto fac的介紹 20131018
 
Auto fac的介紹 20131018
Auto fac的介紹 20131018Auto fac的介紹 20131018
Auto fac的介紹 20131018
 
Using google appengine (2)
Using google appengine (2)Using google appengine (2)
Using google appengine (2)
 
How to avoid check style errors
How to avoid check style errorsHow to avoid check style errors
How to avoid check style errors
 
拆分初始化负载
拆分初始化负载拆分初始化负载
拆分初始化负载
 
Using google appengine_final
Using google appengine_finalUsing google appengine_final
Using google appengine_final
 
簡化 JVM 上雲 - 透過 Azure Spring Cloud 提升開發、發佈及服務監控效率
簡化 JVM 上雲 - 透過 Azure Spring Cloud 提升開發、發佈及服務監控效率簡化 JVM 上雲 - 透過 Azure Spring Cloud 提升開發、發佈及服務監控效率
簡化 JVM 上雲 - 透過 Azure Spring Cloud 提升開發、發佈及服務監控效率
 
OpenWebSchool - 11 - CodeIgniter
OpenWebSchool - 11 - CodeIgniterOpenWebSchool - 11 - CodeIgniter
OpenWebSchool - 11 - CodeIgniter
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有
 
咩星征服計劃 用 Js 征服地球 Part II
咩星征服計劃 用 Js 征服地球 Part II咩星征服計劃 用 Js 征服地球 Part II
咩星征服計劃 用 Js 征服地球 Part II
 
Php Webservers
Php WebserversPhp Webservers
Php Webservers
 

Mehr von Simon Su

Kubernetes Basic Operation
Kubernetes Basic OperationKubernetes Basic Operation
Kubernetes Basic OperationSimon Su
 
Google IoT Core 初體驗
Google IoT Core 初體驗Google IoT Core 初體驗
Google IoT Core 初體驗Simon Su
 
JSDC 2017 - 使用google cloud 從雲到端,動手刻個IoT
JSDC 2017 - 使用google cloud 從雲到端,動手刻個IoTJSDC 2017 - 使用google cloud 從雲到端,動手刻個IoT
JSDC 2017 - 使用google cloud 從雲到端,動手刻個IoTSimon Su
 
GCPUG.TW meetup #28 - GKE上運作您的k8s服務
GCPUG.TW meetup #28 - GKE上運作您的k8s服務GCPUG.TW meetup #28 - GKE上運作您的k8s服務
GCPUG.TW meetup #28 - GKE上運作您的k8s服務Simon Su
 
Google Cloud Platform Special Training
Google Cloud Platform Special TrainingGoogle Cloud Platform Special Training
Google Cloud Platform Special TrainingSimon Su
 
GCPNext17' Extend 開始GCP了嗎?
GCPNext17' Extend   開始GCP了嗎?GCPNext17' Extend   開始GCP了嗎?
GCPNext17' Extend 開始GCP了嗎?Simon Su
 
Google Cloud Computing compares GCE, GAE and GKE
Google Cloud Computing compares GCE, GAE and GKEGoogle Cloud Computing compares GCE, GAE and GKE
Google Cloud Computing compares GCE, GAE and GKESimon Su
 
JCConf 2016 - Google Dataflow 小試
JCConf 2016 - Google Dataflow 小試JCConf 2016 - Google Dataflow 小試
JCConf 2016 - Google Dataflow 小試Simon Su
 
GCPUG meetup 201610 - Dataflow Introduction
GCPUG meetup 201610 - Dataflow IntroductionGCPUG meetup 201610 - Dataflow Introduction
GCPUG meetup 201610 - Dataflow IntroductionSimon Su
 
Brocade - Stingray Application Firewall
Brocade - Stingray Application FirewallBrocade - Stingray Application Firewall
Brocade - Stingray Application FirewallSimon Su
 
使用 Raspberry pi + fluentd + gcp cloud logging, big query 做iot 資料搜集與分析
使用 Raspberry pi + fluentd + gcp cloud logging, big query 做iot 資料搜集與分析使用 Raspberry pi + fluentd + gcp cloud logging, big query 做iot 資料搜集與分析
使用 Raspberry pi + fluentd + gcp cloud logging, big query 做iot 資料搜集與分析Simon Su
 
Docker in Action
Docker in ActionDocker in Action
Docker in ActionSimon Su
 
Google I/O 2016 Recap - Google Cloud Platform News Update
Google I/O 2016 Recap - Google Cloud Platform News UpdateGoogle I/O 2016 Recap - Google Cloud Platform News Update
Google I/O 2016 Recap - Google Cloud Platform News UpdateSimon Su
 
IThome DevOps Summit - IoT、docker與DevOps
IThome DevOps Summit - IoT、docker與DevOpsIThome DevOps Summit - IoT、docker與DevOps
IThome DevOps Summit - IoT、docker與DevOpsSimon Su
 
Google Cloud Platform Introduction - 2016Q3
Google Cloud Platform Introduction - 2016Q3Google Cloud Platform Introduction - 2016Q3
Google Cloud Platform Introduction - 2016Q3Simon Su
 
Google I/O Extended 2016 - 台北場活動回顧
Google I/O Extended 2016 - 台北場活動回顧Google I/O Extended 2016 - 台北場活動回顧
Google I/O Extended 2016 - 台北場活動回顧Simon Su
 
GCS - Access Control Lists (中文)
GCS - Access Control Lists (中文)GCS - Access Control Lists (中文)
GCS - Access Control Lists (中文)Simon Su
 
Google Cloud Platform - for Mobile Solutions
Google Cloud Platform - for Mobile SolutionsGoogle Cloud Platform - for Mobile Solutions
Google Cloud Platform - for Mobile SolutionsSimon Su
 
JCConf 2015 - 輕鬆學google的雲端開發 - Google App Engine入門(下)
JCConf 2015  - 輕鬆學google的雲端開發 - Google App Engine入門(下)JCConf 2015  - 輕鬆學google的雲端開發 - Google App Engine入門(下)
JCConf 2015 - 輕鬆學google的雲端開發 - Google App Engine入門(下)Simon Su
 
JCConf 2015 - 輕鬆學google的雲端開發 - Google App Engine入門(上)
JCConf 2015  - 輕鬆學google的雲端開發 - Google App Engine入門(上)JCConf 2015  - 輕鬆學google的雲端開發 - Google App Engine入門(上)
JCConf 2015 - 輕鬆學google的雲端開發 - Google App Engine入門(上)Simon Su
 

Mehr von Simon Su (20)

Kubernetes Basic Operation
Kubernetes Basic OperationKubernetes Basic Operation
Kubernetes Basic Operation
 
Google IoT Core 初體驗
Google IoT Core 初體驗Google IoT Core 初體驗
Google IoT Core 初體驗
 
JSDC 2017 - 使用google cloud 從雲到端,動手刻個IoT
JSDC 2017 - 使用google cloud 從雲到端,動手刻個IoTJSDC 2017 - 使用google cloud 從雲到端,動手刻個IoT
JSDC 2017 - 使用google cloud 從雲到端,動手刻個IoT
 
GCPUG.TW meetup #28 - GKE上運作您的k8s服務
GCPUG.TW meetup #28 - GKE上運作您的k8s服務GCPUG.TW meetup #28 - GKE上運作您的k8s服務
GCPUG.TW meetup #28 - GKE上運作您的k8s服務
 
Google Cloud Platform Special Training
Google Cloud Platform Special TrainingGoogle Cloud Platform Special Training
Google Cloud Platform Special Training
 
GCPNext17' Extend 開始GCP了嗎?
GCPNext17' Extend   開始GCP了嗎?GCPNext17' Extend   開始GCP了嗎?
GCPNext17' Extend 開始GCP了嗎?
 
Google Cloud Computing compares GCE, GAE and GKE
Google Cloud Computing compares GCE, GAE and GKEGoogle Cloud Computing compares GCE, GAE and GKE
Google Cloud Computing compares GCE, GAE and GKE
 
JCConf 2016 - Google Dataflow 小試
JCConf 2016 - Google Dataflow 小試JCConf 2016 - Google Dataflow 小試
JCConf 2016 - Google Dataflow 小試
 
GCPUG meetup 201610 - Dataflow Introduction
GCPUG meetup 201610 - Dataflow IntroductionGCPUG meetup 201610 - Dataflow Introduction
GCPUG meetup 201610 - Dataflow Introduction
 
Brocade - Stingray Application Firewall
Brocade - Stingray Application FirewallBrocade - Stingray Application Firewall
Brocade - Stingray Application Firewall
 
使用 Raspberry pi + fluentd + gcp cloud logging, big query 做iot 資料搜集與分析
使用 Raspberry pi + fluentd + gcp cloud logging, big query 做iot 資料搜集與分析使用 Raspberry pi + fluentd + gcp cloud logging, big query 做iot 資料搜集與分析
使用 Raspberry pi + fluentd + gcp cloud logging, big query 做iot 資料搜集與分析
 
Docker in Action
Docker in ActionDocker in Action
Docker in Action
 
Google I/O 2016 Recap - Google Cloud Platform News Update
Google I/O 2016 Recap - Google Cloud Platform News UpdateGoogle I/O 2016 Recap - Google Cloud Platform News Update
Google I/O 2016 Recap - Google Cloud Platform News Update
 
IThome DevOps Summit - IoT、docker與DevOps
IThome DevOps Summit - IoT、docker與DevOpsIThome DevOps Summit - IoT、docker與DevOps
IThome DevOps Summit - IoT、docker與DevOps
 
Google Cloud Platform Introduction - 2016Q3
Google Cloud Platform Introduction - 2016Q3Google Cloud Platform Introduction - 2016Q3
Google Cloud Platform Introduction - 2016Q3
 
Google I/O Extended 2016 - 台北場活動回顧
Google I/O Extended 2016 - 台北場活動回顧Google I/O Extended 2016 - 台北場活動回顧
Google I/O Extended 2016 - 台北場活動回顧
 
GCS - Access Control Lists (中文)
GCS - Access Control Lists (中文)GCS - Access Control Lists (中文)
GCS - Access Control Lists (中文)
 
Google Cloud Platform - for Mobile Solutions
Google Cloud Platform - for Mobile SolutionsGoogle Cloud Platform - for Mobile Solutions
Google Cloud Platform - for Mobile Solutions
 
JCConf 2015 - 輕鬆學google的雲端開發 - Google App Engine入門(下)
JCConf 2015  - 輕鬆學google的雲端開發 - Google App Engine入門(下)JCConf 2015  - 輕鬆學google的雲端開發 - Google App Engine入門(下)
JCConf 2015 - 輕鬆學google的雲端開發 - Google App Engine入門(下)
 
JCConf 2015 - 輕鬆學google的雲端開發 - Google App Engine入門(上)
JCConf 2015  - 輕鬆學google的雲端開發 - Google App Engine入門(上)JCConf 2015  - 輕鬆學google的雲端開發 - Google App Engine入門(上)
JCConf 2015 - 輕鬆學google的雲端開發 - Google App Engine入門(上)
 

GAE - Using CloudStorage through FileReadChannel

  • 1. GAE - Using CloudStorage   GAE中使用CloudStorage中的資源可以省略一些認證動作,透過FileReadChannel即可快速的取得 存取CloudStorage的方法,飯粒如下:    Step1: 首先,Library的部分需要import下面藍色部分  import java.io.BufferedReader;  import java.io.FileNotFoundException;  import java.io.IOException;  import java.io.PrintWriter;  import java.nio.ByteBuffer;  import java.nio.channels.Channels;  import java.util.logging.Logger;    import javax.servlet.http.HttpServlet;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletResponse;    import com.google.appengine.api.files.AppEngineFile;  import com.google.appengine.api.files.FileReadChannel;  import com.google.appengine.api.files.FileService;  import com.google.appengine.api.files.FileServiceFactory;  import com.google.appengine.api.files.FileWriteChannel;  import com.google.appengine.api.files.GSFileOptions.GSFileOptionsBuilder;  import com.google.appengine.api.files.LockException;      最後,透過FileReadChannel讀取指定的CloudStorage檔案  public void readFile(HttpServletRequest req, HttpServletResponse resp)         throws FileNotFoundException, LockException, IOException {    // Get the file service    FileService fileService = FileServiceFactory.getFileService();    boolean lockForRead = false;    String filename = "/gs/mitac­cp300­eu/test.txt";    AppEngineFile readableFile = new AppEngineFile(filename);    FileReadChannel readChannel =       fileService.openReadChannel(readableFile, lockForRead);      // Read the file in whichever way you'd like    BufferedReader reader =       new BufferedReader(Channels.newReader(readChannel, "UTF8"));    String line = "";    int i = 0;    while((line = reader.readLine()) != null){      resp.getWriter().println(i + ": " + line);      i++;    }      readChannel.close();  }