# Deta.sh Micros Base Drive 使用方法(Deta.sh已停止服務,轉移到Deta.space,此文待修中...) ###### tags: `deta` `heroku` [ToC] 因為我教學的對象是**高中生**,由於年紀太小,他們沒有信用卡, 許多提供雲運算的商家都需要試刷卡(就算提供免費額度),它們也都無法使用。 Heroku對他們來說是個學習的好工具,但現在heroku要收費了, 我也要另外想辦法了!我找到了deta.sh,它有我需要的全部雲服務,太棒了! 目前在上面放了3個line bot - [bot1](https://hackmd.io/tTnueeJiS5yQgDtlE_G2Hw) [bot2](https://hackmd.io/rGg38eMVT5iR-cyzvFVP1g) [bot3](https://hackmd.io/mQAwolZvRKWQw5XSzFwMNg) 與 1隻爬蟲(使用cron定時抓資料) 它有 Micros, Base, Drive 三種服務 ### Micros: Deploy scalable Node & Python apps in seconds. 簡單來說 Deta micros 相當於 heroku dynos Standard 1x 的平替。Drive它提供無限的空間,無限的Micros數量。 可以將你的 node.js或是python程式放在它上面執行,成為微服務器。 這部分使用方法可參閱[將LineBot發布到 Deta.sh 中](https://hackmd.io/@CSL/HJcsPvhOj) 裡面有詳細的說明。 ```bash! deta 常用指令 auth Change auth settings for a deta micro clone Clone a deta micro #從 deta.sh 複製到本機 Example: deta clone --name mymicro cron Change cron settings for a deta micro "下方有詳細說明" deploy Deploy a deta micro #將程式發布: deta deploy ,程式碼修改後要執行的 details Details about a deta micro #顯示 micros的狀況,例如: { "name": "crontest", "id": "b17dxxxxxxxxxxxxxxxx", "project": "default", "runtime": "python3.9", "endpoint": "https://tkxxxx.deta.dev", "region": "ap-southeast-1", "dependencies": [ "deta" ], "visor": "enabled", "http_auth": "disabled", "cron": "2 minutes" } help Help about any command login Login to deta #登入deta.sh網站 new Create a new deta micro projects List deta projects pull Pull the lastest deployed code of a deta micro run Run a deta micro Example: deta run # run from cli update Update a deta micro version Print deta version visor Change visor settings for a deta micro #debug時要enable方便偵錯 watch Deploy changes in real time #執行這個功能時,一儲存程式會自動發布,不用再deploy ``` #### deta.sh micros: cron 功能 deta的micros有cron的功能,這功能相當好用,它可以依照你的需要時間取執行你的程式(**網路爬蟲很需要**),使用方法與linux的crontab相當類似。 執行下列指令就能每10分鐘自動執行main.py mkdir crontest cd crontest deta new --python 新增 main.py 與 requirements.txt(內容在下方) deta deploy deta visor enable deta cron set "10 minutes" ```python main.py ---------------------- from deta import app # define a function to run on a schedule # the function must take an event as an argument @app.lib.cron() def cron_job(event): return "running on a schedule" ---------------------- requirements.txt ---------------------- deta ``` 登入 deta.sh (指令:deta login) 選擇 micros 裡的 crontest 查看 visor ,他每10分鐘執行一次, 如果不要再執行cron功能了,下指令 deta cron remove即可。 ``` 在官方文件中還有許多設定時間的方法,看一下就會使用了,不過要這注意是UTC時間 1. deta cron set "1 minute" : run every minute 2. deta cron set "5 hours" : run every five hours 3. deta cron set "0 10 * * ? *" : run at 10:00 am(UTC) every day 4. deta cron set "30 18 ? * MON-FRI *" : run at 6:00 pm(UTC) Monday through Friday 5. deta cron set "0/5 8-17 ? * MON-FRI *" : run every 5 minutes Monday through Friday between 8:00 am and 5:55 pm(UTC) ``` ### Base: Instantly usable database with a feature-rich API. Deta Base is a super easy to use production-grade NoSQL database that comes with unlimited storage. 它是一個NoSQL資料庫(無限的空間),我使用的感覺是"它與 redis cloud 很相像"。 ```python! from deta import Deta deta = Deta("xxxxxxxxx") users = deta.Base("you_data_base_name") id='001' datajson={"name":"Mary","Gender":"female"} # 寫入資料庫: insert 與 put users.insert(datajson,id) # 後面的id是資料庫的key,前面的參數是data # 如果id已存在,insert時會出現錯誤 # 使用put則不會,會覆蓋原來的資料 users.put(datajson,id) #從資料庫讀取資料 item=users.get(id) #從資料庫將資料刪除 users.delete(id) #將資料庫的資料更新(修改) users.update(datajson,id) ``` ### Drive: Upload, host and serve images and files. The easy to use cloud storage solution by Deta – get 10GB for free. Drive它提供10GB的空間讓你存放影像或其他種類的檔案(如pdf、mp4、...等), ## :memo: deta.sh官方文件 [Deta.sh官方DOC](https://docs.deta.sh/docs/home) ## :rocket: 它的缺點:號稱"Deta is free for ever."且還在beta中。 大家對於這些免費的資源所帶來的後遺症,都經歷不少了! 擔心一但若不能持續,後續搬家維護的問題不少。穩定度如何?也要持續觀察! 但若只是拿來學習、寫一些簡單的webapi,應該相當不錯。 Author - [name=林奇賢]