Pages

2023年5月11日 星期四

【GCP 】Region HTTP TO HTTPS Load Balance 設定方式(台灣)

目的:在GCP上建立台灣版的HTTP轉HTTPS的負載平衡器

步驟:

1. 進入GCP Console

2. 點選右上角的【啟用Cloud Shell】

3. 點選右下角【開啟編輯器】

4. 點選【File】➔【New File】

5. 貼上以下內容(相關名稱與domain請自行修改)


kind: compute#urlMap
name: example-http-to-https
defaultUrlRedirect:
  redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
  httpsRedirect: True
tests:
- description: Test with no query parameters
  host: example.com
  path: /
  expectedOutputUrl: https://example.com/
  expectedRedirectResponseCode: 301

6. 點選【File】➔【Save As...】➔ 選擇tmp目錄➔存檔名稱為:web-map-http.yaml

7. 點選右上角X ➔ 離開編輯器

8. 到【VPC Network】➔【IP Address】➔尋找HTTPS負載平衡器,建立的HTTPS IP名稱。

9. 點選右上角的【啟用Cloud Shell】

10. 執行以下指令(請依自行設定的區域、IP名稱自行修改)

gcloud compute url-maps import example-http-to-https --source /tmp/web-map-http.yaml --region=asia-east1
gcloud compute url-maps describe example-http-to-https --region=asia-east1
gcloud compute target-http-proxies create http-lb-proxy --url-map=example-http-to-https --region=asia-east1
gcloud compute forwarding-rules create http-content-rule \
           --load-balancing-scheme=EXTERNAL_MANAGED \
           --address=uat-example-https2 \
           --network-tier=STANDARD \
           --region=asia-east1 \
           --target-http-proxy=http-lb-proxy \
           --target-http-proxy-region=asia-east1 \
           --ports=80

11. 完成後,網域站台即可透過HTTP協定轉到HTTPS。

申請SSL證書遇到CAA紀錄問題

 情況描述:

1. 該網域之前在GCP上,有申請SSL證書,但想使用Region的Load Balance需要自已提供證書。

2. 在某網站購買SSL證書,但在認證的時候提示CAA紀錄異常。

3. 使用CAA Lookup Tool查看CAA紀錄,發現Google把CAA紀錄加入DNS。

4. 在DNS管理平台上,添加CAA紀錄,以cloudflare為例

一般 SSL CAA紀錄設定

Wildcard SSL CAA紀錄設定

      


5. 過60分鐘後,使用CAA Lookup Tool查看CAA紀錄,已有新添加的CAA紀錄,在進行Domain認證,就可以認證SSL成功。

2023年5月4日 星期四

mysqldump: Couldn't execute 'SELECT COLUMN_NAME, JSON_EXTRACT(HISTOGRAM, '$."number-of-buckets-specified"') FROM information_schema.COLUMN_STATISTICS

 mysqldump命令:


  导出数据库:mysqldump -h ip -u root -p dbname > db.sql;

  导出数据库中的某个表:mysqldump -h ip -u root -p dbname tablename > tablename.sql;


错误提示:

  mysqldump: Couldn't execute 'SELECT COLUMN_NAME, JSON_EXTRACT(HISTOGRAM, '$."number-of-buckets-specified"') FROM information_schema.COLUMN_STATISTICS


原因:


  因为新版的mysqldump默认启用了一个新标志,通过 --column-statistics=0 来禁用他


解决方法:

  mysqldump --column-statistics=0 -h 192.168.0.1 -u root -p dbname > db.sql;

Java使用Tesseract進行OCR辨識的一些心得

最近有個專案想抓取PDF的檔案內容,進行檔案rename的動作,因此才開始瞭解Tesseract相關的用法,以下是使用的一些心得。

1. Windows上安裝Tesseract 5.0後,如果想轉移到其他台Windows PC,該PC要先安裝 Visual Studio 2015 的 Visual C++ 可轉散發套件,再將Tesseract安裝的目錄整個zip起來後,在其他台Windows PC上解壓縮後,設定Tesseract相關windows環境變數路徑後,Java程式就可以直接使用。


2. Tesseract程式中辨識資料的路徑與繁體中文語言設定如下:

tesseract.setDatapath("D:\\Tesseract-OCR\\tessdata\\");
tesseract.setLanguage("chi_tra");


3. 不要透過抓取PDF文字後再辨識,因為會抓不到PDF內容文字,要先轉成圖檔(TIFF)後,在進行圖片辨識文字,會比較不會出錯,下面是範例的Code。

private boolean pdfToTiffConverter(String inputPdf, String outputTiff) {
  int dpi = 300; // the resolution of the output image in DPI
  boolean result = false;
  PDDocument document = null;
  PDFRenderer pdfRenderer = null;
  BufferedImage image = null;
  try {
  	File pdfFile = new File(inputPdf);
  	Thread.sleep(100); // wait 100ms for loading pdf to memory to avoid NullPointException 
  	document = PDDocument.load(pdfFile);
    pdfRenderer = new PDFRenderer(document);
    image = pdfRenderer.renderImageWithDPI(0, dpi); // render the first page of the PDF document
    ImageIO.write(image, "TIFF", new File(outputTiff)); // write the image to a TIFF file
    result = true;
    document.close();
  } catch (IOException e) {
  	logger.error(e.getStackTrace());
  	result = false;
  } catch (InterruptedException e) {
  	logger.error(e.getStackTrace());
  } finally {
  }
  return result;
}

private String tiffOcr(String outputTiff) {
  String tiffPath = outputTiff;
  Tesseract tesseract = new Tesseract();
  String result = "";
  try {
    tesseract.setDatapath("D:\\Tesseract-OCR\\tessdata\\"); // set the path to the Tesseract data directory
    tesseract.setLanguage("chi_tra");
    result = tesseract.doOCR(new File(tiffPath));
  } catch (TesseractException e) {
    logger.error(e.getStackTrace());
  } finally {
    File imageFile = new File(outputTiff);
    if(imageFile.exists()) imageFile.delete();
  }
  return result;
}

4. import的套件很多,可以參考下面圖片的套件版本。


5. 在Eclipse上運行正常,但匯出成可運行的JAR後,辨識時會出現以下錯誤。

javax.imageio.spi.ImageOutputStreamSpi: Provider com.sun.media.imageioimpl.stream.ChannelImageOutputStreamSpi could not be instantiated

解決方式:Eclipse匯出方式需要變更為【copy required libraries into sub-folder....】。


可運行的JAR在執行前,要先把sub-folder的目錄跟可運行的JAR放在同一層執行,這樣才能順利運行。

這個問題花了我5個小時才解決掉,希望對大家有助。