aws lambda测试控制台

时间:2016-06-07 21:57:12

标签: java eclipse aws-lambda

我有一个AWS lambda示例,使用AWS Toolkit for eclipse创建。我在eclipse的项目中添加了一个config.properties文件。我也正在使用右键单击项目上传 - > Amazon Web Services - >上传

但是当我在aws控制台上测试时,它为config.properties提供了filenotfound。

请帮忙!

这是我的项目结构:我在第33行收到错误,告知找不到config.properties文件。 enter image description here

这是我的lambda函数:

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaFunctionHandler implements RequestHandler<String, WebConnectResponse> {

    @Override
    public void handleRequest(String input, Context context) {

        context.getLogger().log("Input: " + input);

        try {
            new PreviewService().GetPreview(input);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public void GetPreview(String downloadUrl) throws Exception{    

        input = new FileInputStream("config.properties"); //ERROR HERE: FileNotFoundException by aws lambda when testing on aws lambda console. 

        props.load(input);

        //Download File
        downloadFileFromUrl(new URL(downloadUrl));


        return null;

    }

    public void downloadFileFromUrl(URL downloadUrl)throws Exception{

        FileUtils.copyURLToFile(downloadUrl, new File("<filepath>"));

        uploadFileToServer("<filepath>");


    }

    public void uploadFileToServer(String filePath) throws Exception 
    {

        String fileExternalRefId = "id";
        String param = getProperty("param");
        URL uploadUrl = new URL(getProperty("uploadurl")); 

        File contents = new File("<filepath>"); 

        String boundary = Long.toHexString(System.currentTimeMillis());

        String CRLF = "\r\n"; //Line Separator required by multipart/form-data

        URLConnection connection = uploadUrl.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        connection.addRequestProperty("file_name", contents.getName());
        connection.addRequestProperty("id", fileId);

        try(
                OutputStream output = connection.getOutputStream();
                PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"), true);
                ) {

            //Send headers/params
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);
            writer.append("Content-Type: application/xml; charset=UTF-8").append(CRLF);
            writer.append(CRLF).append(param).append(CRLF).flush();

            //Send contents
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"file-content\"; filename=\"" +  contents.getName() + "\"").append(CRLF);
            writer.append("Content-Type: application/xml; charset=UTF-8").append(CRLF);
            writer.append(CRLF).flush();

            Files.copy(contents.toPath(), output);
            //IOUtils.copy(in, output);
            output.flush();
            writer.append(CRLF).flush();//It indicates end of boundary

            writer.append("--" + boundary + "--").append(CRLF).flush();

        }
        int responseCode = ((HttpURLConnection) connection).getResponseCode();

        if(responseCode == 200)
        {
            System.out.println(responseCode);
            String viewUrl = props.getProperty("url") 
            System.out.println(viewUrl);
        }


    }

    public String getProperty(String key)
    {
        return props.getProperty(key);
    }

}

这是我的config.properties,看起来像

key1=value1
key2=value2

2 个答案:

答案 0 :(得分:2)

我对AWS没什么经验,但是当你使用java文件或FileInputStreams时,必须使用文件路径而你只使用文件名。

我认为你的代码应该是:

input = new FileInputStream("/[appDeployPath]/config.properties");

也许更好的方法是使用:

getClass().getClassLoader().getResource("config.properties")

答案 1 :(得分:1)

我的项目中还有 config 文件,这就是我读取此文件内容的方式,我在这里回答了问题 -

https://stackoverflow.com/a/42757653/5892553