2016-03-03 11 views
5

Il mio codice Python recupera informazioni da altri siti Web e crea file json. Sul computer locale funziona bene, ma quando provo a eseguire il codice in Lambda ho un errore.
Ho usato metodi diversi per file di creazione:
Autorizzazione lambda e S3 negata quando si desidera creare il file

file = open('test.json', 'w') 

e

oldumask = os.umask(0) 
fdesc = os.open('test.json', os.O_WRONLY | os.O_CREAT, 0o600) 
file = os.fdopen(fdesc, "w") 

Messaggio di errore:

{ 
    "stackTrace": [ 
    [ 
     "/var/task/awsewt1.py", 
     24, 
     "handler", 
     "fdesc = os.open('test.json', os.O_WRONLY | os.O_CREAT, 0o600)" 
    ] 
    ], 
    "errorType": "OSError", 
    "errorMessage": "[Errno 13] Permission denied: 'test.json'" 
} 

Codice:

from __future__ import print_function 

import json 
import urllib 
import boto3 
import os, stat 

access_key = 'hide' 
secret_key = 'hide' 

def loadJSONByURL(url,key): 
    response = urllib.urlopen(url) 
    content = response.read() 
    data = json.loads(content) 
    text = {key:data} 
    return text 

def handler(event, context): 
    phattha = "hide" 
    phuket = "hide" 
    koSamui = "hide" 
    oldumask = os.umask(0) 
    fdesc = os.open('test.json', os.O_WRONLY | os.O_CREAT, 0o600) 
    file = os.fdopen(fdesc, "w") 
    json.dump(loadJSONByURL(phattha,'phatthaya'), file) 
    json.dump(loadJSONByURL(phuket,'phuket'), file) 
    json.dump(loadJSONByURL(koSamui,'koSamui'), file) 
    file.close() 
    conn = S3Connection(access_key,secret_key) 
    bucket = conn.get_bucket('ewtbucket') 

    key1 = bucket.get_key('test.json') 
    if key1: 
     key1.delete() 
    key = bucket.new_key('/test.json') 
    key.set_contents_from_filename('test.json') 
    key.set_acl('public-read') 

s3 = boto3.client('s3') 


def lambda_handler(event, context): 
    handler(event,context) 

risposta

11

La tua funzione Lambda non avrà permessi di scrittura file locali ovunque sulla macchina. Prova a scrivere nella directory /tmp:

file = open('/tmp/test.json', 'w') 
+0

Grazie, sei un genio! :) – Iraklii

Problemi correlati