Python 创建文件时,Lambda和S3权限被拒绝

Python 创建文件时,Lambda和S3权限被拒绝,python,amazon-web-services,amazon-s3,lambda,Python,Amazon Web Services,Amazon S3,Lambda,我的python代码从其他网站获取信息并创建json文件。在本地计算机上工作正常,但当我尝试在Lambda中运行代码时,出现了错误。 我使用了不同的方法创建文件: file = open('test.json', 'w') 及 错误消息: { "stackTrace": [ [ "/var/task/awsewt1.py", 24, "handler", "fdesc = os.open('test.json', os.O_WRONL

我的python代码从其他网站获取信息并创建json文件。在本地计算机上工作正常,但当我尝试在Lambda中运行代码时,出现了错误。
我使用了不同的方法创建文件:

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

错误消息:

{
  "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'"
}
代码:


Lambda函数在计算机上的任何位置都没有本地文件写入权限。尝试写入
/tmp
目录:

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

谢谢,你真是天才!:)
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)
file = open('/tmp/test.json', 'w')