PC のスクリーンショットを一定の間隔で取得し、そのスクリーンショットを S3 バケットに送信する Python スクリプトを作成しました。python コマンドでスクリプトを実行すると機能しますが、このスクリプトを pythonw.exe コマンドでバックグラウンド タスクとして実行すると、スクリーンショットのキャプチャ操作は機能しますが、S3 には何もアップロードされません。
これが私のコードです:
import os
import sys
import time
import Image
import ImageGrab
import getpass
import boto3
import threading
from random import randint
s3 = boto3.resource('s3')
username = getpass.getuser()
#---------------------------------------------------------
#User Settings:
SaveDirectory=r'C:\Users\Md.Rezaur\Dropbox\Screepy_App\screenshot'
ImageEditorPath=r'C:\WINDOWS\system32\mspaint.exe'
def capture_and_send():
interval = randint(10,30)
threading.Timer(interval, capture_and_send).start ()
img=ImageGrab.grab()
saveas=os.path.join(SaveDirectory,'ScreenShot_'+time.strftime('%Y_%m_%d_%H_%M_%S')+'.jpg')
fname = 'ScreenShot_'+time.strftime('%Y_%m_%d_%H_%M_%S')+'.jpg'
img.save(saveas, quality=50, optimize=True)
editorstring='""%s" "%s"'% (ImageEditorPath,saveas)
data = open(fname, 'rb')
s3.Bucket('screepy').put_object(Key=username+'/'+fname, Body=data)
capture_and_send()
aws 認証情報を設定していない場合は、aws-cli をインストールして次のコマンドを実行します。
aws configure