简体   繁体   中英

Why is my python script hanging when I try to capture/upload a picture?

Trying to upload photos from a Raspberry Pi to AWS S3, but have encountered a couple of errors. Program will hang on uploading the picture to AWS.

Tried moving it to a separate script and calling it within the program, and putting it as the beginning line of the program. Each resulted in the program hanging at upload. When the script is ran on its own, it will work.

import ssl
import time
import json
import gpiozero
import boto3
import picamera
import pic

from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTShadowClient
def main():
    pic.capture('pi.png')

    myShadowClient = AWSIoTMQTTShadowClient(CLIENT ID)

    myShadowClient.configureEndpoint(ENDPOINT, PORT NUMBER)

       myShadowClient.configureCredentials(ROOT CA,/PRIVATE KEY ,CERTIFICATION)

    myDeviceShadow = myShadowClient.createShadowHandlerWithName("ThingName", True)
    myShadowClient.configureConnectDisconnectTimeout(10)  # 10 sec
    myShadowClient.configureMQTTOperationTimeout(5)  # 5 sec
    myShadowClient.connect(1200) #Print this out on
    #s3.upload_file('pipic.png',bucket_name,'pipic.png')
    #s3.upload_file('test5.png',bucket_name,'test5.png')

    while True:
        myDeviceShadow.shadowGet(customShadowCallback_Update, 5)
        time.sleep(5)

pic.py

import picamera
import boto3

camera = picamera.PiCamera()
def capture(filename):
    camera.capture(filename)


s3 = boto3.client(CLIENTID, ACCESS KEY ID, SECRET ACCESS KEY)
bucket_name = ''
for y in range(20):
    print("before capture")
    capture(str(y)+'pi.png')
    s3.upload_file(str(y)+'pi.png',bucket_name, str(y)+'pi.png')
    print("after upload")
camera.close()

Expect the picture to be uploaded and have the "after upload" statement printed out. So far only the "before capture statement is printed at, and then the program will hang when trying to u

To create a client in boto3 , use:

import boto3

client = boto3.client(
    's3',
    aws_access_key_id=ACCESS_KEY,
    aws_secret_access_key=SECRET_KEY
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM