LoginSignup
1
0

More than 1 year has passed since last update.

S3から最新ファイルをダウンロードしてスライドショー表示

Last updated at Posted at 2022-09-25

ラズパイでフォトフレームを作る際にS3バケットから最新のファイルのみダウンロードして、
表示させようと試みたのでその時の備忘録。
あくまで個人のメモ程度の情報なので、参考にはならないかもしれません…

S3から最新ファイルを取得するPythonファイルを作る。

まずはラズパイにPythonファイルを作成。
ちなみに、Python環境やS3、AWS-CLIとかの設定は済んでいるものとして今回は割愛します。

import os
import boto3

accesskey="*****"
secretkey="*****"
region = '*****'
bucket_name = '****'


#Bucket list cache
def list_files(bucket_name):
    s3 = boto3.client('s3',aws_access_key_id=accesskey,
                      aws_secret_access_key=secretkey,
                      region_name=region)
    contents = []
    for item in s3.list_objects(Bucket=bucket_name)["Contents"]:
        contents.append(item)
        
    return contents
    
#------------------------------------------------------

s3 = boto3.resource('s3')

s3_list = list_files(bucket_name)
Keylist = []
loop_first = True

for i in s3_list:
    if loop_first:
        dl_target_file = i.get("Key")
        mod_datetime = i.get("LastModified")
        Keylist.append(dl_target_file)
        loop_first = False
    
    else:
        if mod_datetime <= i.get("LastModified") and i.get("Key").endswith("png"):
            mod_datetime = i.get("LastModified")
            dl_target_file = i.get("Key")
            Keylist[0] = dl_target_file

file_name = Keylist[0]

# 今回はDocumentディレクトリのimgフォルダ内にpngファイル作る
output = '/home/pi/Documents/img/'+ file_name
print(file_name)
s3.Bucket(bucket_name).download_file(file_name,output)

スライドショー起動設定

fehというビューアーを使う

sudo apt-get install feh

設定。マニュアル確認

man feh

起動確認

feh -Y -x -q -D 60 -R 3600 -F -Z -r -z ~/Documents/img/

CronでPythonファイルを定期実行させる

crontab -e 

50分おきに実行させる

*/50 * * * * python /home/pi/<pythonファイルの絶対パス>

cron設定内容の確認 crontab -l
実行確認をする less /var/log/syslog | grep cron
cronステータス確認 sudo systemctl stats cron

pythonがなぜか実行されなかったのだが、
実行権限が付与されてなかったのでpythonファイルにchmod 777 <ファイル名>で解決。

ちなみに、画面のスリープ状態になるのを解除したい場合はこちらを参考
https://rikoubou.hatenablog.com/entry/2020/06/11/133716

参考記事

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0