Devs.tw 是讓工程師寫筆記、網誌的平台。歡迎您隨手紀錄、寫作,方便日後搜尋!
正在開發某服務,想撈 Google 地圖上的照片
根據價目表 https://cloud.google.com/maps-platform/pricing/sheet/?hl=zh-tw
places photo 的費用是每千次 $7.00 美元,相當於每開一次照片,就要收費 0.2 新台幣!
這實在是非常貴!
因為流程第二步的新網址可以直接在 html 內放進 img src,資料庫該存跳轉前還是跳轉後的資料?
存跳轉前的話,等於每次用戶打開圖片都透過 photo_reference 去得到圖片一次。
這是正確用法嗎?API 如此昂貴?
存跳轉後的話就好很多,只有第一次解析時會被計費。但是,這跳轉後的網址會不會過期?
Google 是不是本來就想每次讀取圖片都收錢?正確的使用政策很難找,該怎麼辦?
查找官方 library 的使用範例
https://github.com/googlemaps/google-maps-services-python/blob/master/googlemaps/places.py
def places_photo(client, photo_reference, max_width=None, max_height=None):
"""
Downloads a photo from the Places API.
:param photo_reference: A string identifier that uniquely identifies a
photo, as provided by either a Places search or Places detail request.
:type photo_reference: string
:param max_width: Specifies the maximum desired width, in pixels.
:type max_width: int
:param max_height: Specifies the maximum desired height, in pixels.
:type max_height: int
:rtype: iterator containing the raw image data, which typically can be
used to save an image file locally. For example:
f = open(local_filename, 'wb')
for chunk in client.places_photo(photo_reference, max_width=100):
if chunk:
f.write(chunk)
f.close()
"""
if not (max_width or max_height):
raise ValueError("a max_width or max_height arg is required")
params = {"photoreference": photo_reference}
if max_width:
params["maxwidth"] = max_width
if max_height:
params["maxheight"] = max_height
# "extract_body" and "stream" args here are used to return an iterable
# response containing the image file data, rather than converting from
# json.
response = client._request("/maps/api/place/photo", params,
extract_body=lambda response: response,
requests_kwargs={"stream": True})
return response.iter_content()
所以官方範例根本是把圖片載下來,表示 Google 無意讓用戶每次打開你的圖片都要收費,只是透過 photo_reference 查找時要收費而已。這樣合理多了,嚇我一跳,虛驚一場。